zenithstar95

I love you Aymeric

  • he/him

28 - queer dude - irish - πŸ³οΈβ€πŸŒˆπŸ³οΈβ€βš§οΈπŸ΄
__
Software dev.
I draw suggestive things sometimes.
__
Boyfriend: @hephaistos


CSS Adventurer Plate Generator
stellarzenith.github.io/adventurer-plate/

This is so janky but fuck it. I'm not aiming for efficiency, I'm aiming to get an answer. I am slightly intimidated by Part 2.


raw_input = open("input.txt", "r")
input = raw_input.read().splitlines()
raw_input.close()


def is_part(line, i):
    is_part = False
    start_line = line - 1
    end_line = line + 1
    if line == total_lines - 1:
        end_line -= 1
    elif line == 0:
        start_line += 1

    searching = True
    first_pass = True
    while searching and i >= 0:
        for line_index in range(start_line, end_line + 1):
            if not input[line_index][i].isdigit() and input[line_index][i] != ".":
                is_part = True
                searching = False
        i -= 1
        if not first_pass:
            if not input[line][i + 1].isdigit():
                searching = False
        first_pass = False
    return is_part


total_lines = len(input)
line_width = len(input[0])
total = 0

for line in range(total_lines):
    current_number = 0
    for i in range(line_width):
        if input[line][i].isdigit():
            current_number *= 10
            current_number += int(input[line][i])
            if i == line_width - 1:
                if is_part(line, i):
                    total += current_number
        elif current_number > 0:
            if is_part(line, i):
                total += current_number
            current_number = 0
print(total)
syntax highlighting by codehost

You must log in to comment.