My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/10

+52
+52
2022/day10.py
··· 1 + import fileinput 2 + 3 + # Initialize emulator. 4 + cycle = 1 5 + X = 1 6 + 7 + WIDTH = 40 8 + HEIGHT = 6 9 + 10 + def draw_pixel(): 11 + pos = ((cycle - 1) % WIDTH) + 1 12 + if X <= pos < X + 3: 13 + print("#", end="") 14 + else: 15 + print(".", end="") 16 + 17 + if pos == WIDTH: 18 + print() 19 + 20 + 21 + # Part 1 22 + interesting_cycle = [20, 60, 100, 140, 180, 220] 23 + signal_strengths = [] 24 + 25 + for line in fileinput.input(): 26 + ins = line.split() 27 + op = ins[0] 28 + 29 + if op == 'noop': 30 + draw_pixel() 31 + if cycle in interesting_cycle: 32 + signal_strengths.append(X * cycle) 33 + cycle += 1 34 + 35 + elif op == 'addx': 36 + arg = int(ins[1]) 37 + 38 + # Process first cycle of draw_pixelx. 39 + draw_pixel() 40 + if cycle in interesting_cycle: 41 + signal_strengths.append(X * cycle) 42 + cycle += 1 43 + 44 + # Process second cycle of draw_pixelx. 45 + draw_pixel() 46 + X += arg 47 + if cycle in interesting_cycle: 48 + signal_strengths.append(X * cycle) 49 + cycle += 1 50 + 51 + print() 52 + print("Part 1:", sum(signal_strengths))