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/

Advent of Code day 1 part 2. Could I have condensed those two functions into a single one? Sure, if I thought about it. Will I? Wellll......


input = rawinput.splitlines()
digits = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5,
    "six": 6,
    "seven": 7,
    "eight": 8,
    "nine": 9,
}


def findleft(line):
    number = 0
    found = False
    while not found:
        if line[0].isdigit():
            number += int(line[0]) * 10
            found = True
        else:
            for d in digits:
                if line.startswith(d):
                    number += digits[d] * 10
                    found = True
            if not found:
                line = line.removeprefix(line[0])
    return number


def findright(line):
    number = 0
    found = False
    while not found:
        if line[len(line) - 1].isdigit():
            number += int(line[len(line) - 1])
            found = True
        else:
            for d in digits:
                if line.endswith(d):
                    number += digits[d]
                    found = True
            if not found:
                line = line.removesuffix(line[len(line) - 1])
    return number


def main():
    total = 0
    for line in input:
        number = findleft(line)
        number += findright(line)
        total += number
    print(total)


if __name__ == "__main__":
    main()
syntax highlighting by codehost

You must log in to comment.