-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
73 lines (55 loc) · 1.63 KB
/
day10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 12/10/2022
# https://adventofcode.com/2022/day/10
import sys
def part1(code, duringCycleFn = lambda cc, x: None):
cycleCounter = 0
xRegister = 1
recordCycles = [20, 60, 100, 140, 180, 220]
totalStrength = 0
def incrementCycle():
nonlocal cycleCounter
nonlocal totalStrength
cycleCounter += 1
if cycleCounter in recordCycles:
strength = cycleCounter * xRegister
totalStrength += strength
# print("cycle %d: x=%d, strength=%d, totalStrength=%d" % (cycleCounter, xRegister, strength, totalStrength))
duringCycleFn(cycleCounter, xRegister)
for op, operand in code:
if op == "noop":
incrementCycle()
elif op == "addx":
incrementCycle()
incrementCycle()
xRegister += operand
# print("x %d -> %d at cycle %d" % (xRegister-operand, xRegister, cycleCounter))
return totalStrength
def part2(code):
pixels = []
nPixels = 240
def duringCycle(cycleCounter, xRegister):
if len(pixels) >= nPixels:
return
crtPosition = len(pixels) % 40
pixel = '#' if crtPosition in list(range(xRegister-1,xRegister+2)) else '.'
pixels.append(pixel)
part1(code, duringCycle)
ret = "\n"
for i in range(0, len(pixels), 40):
ret += "".join(pixels[i:i+40])
ret += "\n"
return ret
def main(fname):
code = []
with open(fname, 'r') as f:
for line in f:
parts = line.strip().split(" ")
op = parts[0]
operand = None
if parts[0] == 'addx':
operand = int(parts[1])
code.append((op, operand))
print("Part 1: %s" % (part1(code),))
print("Part 2: %s" % (part2(code),))
if __name__ == '__main__':
main(sys.argv[1])