diff --git a/2022/Day10/Day10.py b/2022/Day10/Day10.py index e69de29..c40edf3 100644 --- a/2022/Day10/Day10.py +++ b/2022/Day10/Day10.py @@ -0,0 +1,52 @@ +def getSprite(currentX: int) -> str: + if currentX == 0: + middle = "##." + elif currentX == -1: + middle = "#.." + elif currentX < -1: + middle = "..." + else: + middle = "###" + + return "." * (currentX - 1) + middle + "." * (40 - currentX - 2) + + +if __name__ == '__main__': + x = 1 + cycle = 1 + + savedCycleValues = {} + + toSave = [20, 60, 100, 140, 180, 220] + output = "" + with open("input.txt", "r") as f: + operations = [line.replace("\n", "") for line in f.readlines()] + + for operation in operations: + if operation == "noop": + output += getSprite(x)[(cycle - 1) % 40] + cycle += 1 + if cycle % 40 == 1 and cycle != 1: + output += "\n" + else: + output += getSprite(x)[(cycle - 1) % 40] + value = int(operation.split(" ")[1]) + cycle += 1 + if cycle in toSave: + savedCycleValues[cycle] = x + + if cycle % 40 == 1 and cycle != 1: + output += "\n" + + output += getSprite(x)[(cycle - 1) % 40] + cycle += 1 + x += value + if cycle in toSave: + savedCycleValues[cycle] = x + + if cycle % 40 == 1 and cycle != 1: + output += "\n" + + print(f"Solution 1: {sum([key * value for key, value in savedCycleValues.items()])}") + print("Solution 2:") + print(" " + " ".join(output)) # with join its readability is increased by a lot diff --git a/2022/Day10/input.txt b/2022/Day10/input.txt index e69de29..4698f3d 100644 --- a/2022/Day10/input.txt +++ b/2022/Day10/input.txt @@ -0,0 +1,140 @@ +noop +noop +noop +addx 5 +noop +addx 1 +noop +addx 4 +addx 25 +addx -20 +noop +noop +addx 5 +addx 3 +noop +addx 2 +noop +noop +addx -1 +addx 6 +addx 1 +noop +addx 4 +noop +addx -37 +noop +noop +noop +addx 3 +addx 32 +addx -25 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx 2 +addx -24 +addx 25 +addx 5 +addx 2 +addx 8 +addx -23 +addx 18 +addx 5 +addx -39 +addx 11 +addx -9 +addx 6 +addx -2 +addx 5 +addx 4 +addx -4 +addx 3 +addx 5 +addx 2 +noop +addx -1 +addx 6 +addx -21 +addx 22 +addx 3 +addx 1 +addx 5 +noop +noop +addx -35 +noop +noop +noop +noop +addx 37 +addx -33 +noop +addx 6 +addx 2 +addx -1 +addx 3 +addx 1 +addx 5 +addx 2 +addx -19 +addx 21 +addx 1 +addx 5 +addx -31 +addx 36 +noop +addx 3 +addx -2 +addx -38 +noop +noop +addx 7 +addx 14 +addx -4 +addx -7 +addx 5 +addx 2 +addx 12 +addx -15 +addx 6 +addx 2 +addx 5 +addx -27 +addx 25 +addx 5 +noop +addx 7 +addx -2 +addx 5 +addx -40 +noop +addx 7 +noop +addx -1 +addx 2 +addx 5 +addx -1 +addx 1 +addx 2 +addx 7 +noop +addx -2 +noop +addx 3 +addx 2 +addx 7 +noop +noop +addx 1 +noop +noop +addx 3 +addx 1 +noop +noop +noop \ No newline at end of file