-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.py
More file actions
151 lines (139 loc) · 3.94 KB
/
day18.py
File metadata and controls
151 lines (139 loc) · 3.94 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#AoC Day 18 -- Duet
import collections
with open('day18.txt', 'r') as f:
file = list(f)
def getval(regs, val):
try:
return int(val)
except:
return regs[val]
def part1(file):
regs = collections.Counter()
lastsound = 0
i = 0
while i < len(file):
line = file[i]
cmd, *rest = line.split()
if cmd == 'snd':
x, = rest
lastsound = getval(regs, x)
elif cmd == 'set':
x, y = rest
regs[x] = getval(regs, y)
elif cmd == 'add':
x, y = rest
regs[x] += getval(regs, y)
elif cmd == 'mul':
x, y = rest
regs[x] *= getval(regs, y)
elif cmd == 'mod':
x, y = rest
regs[x] %= getval(regs, y)
elif cmd == 'rcv':
x, = rest
if getval(regs, x):
return lastsound
elif cmd == 'jgz':
x, y = rest
if getval(regs, x) > 0:
i += getval(regs, y)
continue
else:
raise Exception('Nikhi your input is wrong', line)
i += 1
def part2(file):
s = 0
regs1 = collections.Counter()
regs2 = collections.Counter({'p': 1})
i = 0
j = 0
q1 = collections.deque()
q2 = collections.deque()
lock = 0
ln = len(file)
while lock != 3:
lock = 0
if i < ln:
line = file[i]
cmd, *rest = line.split()
if cmd == 'snd':
x, = rest
q2.append(getval(regs1, x))
i += 1
elif cmd == 'set':
x, y = rest
regs1[x] = getval(regs1, y)
i += 1
elif cmd == 'add':
x, y = rest
regs1[x] += getval(regs1, y)
i += 1
elif cmd == 'mul':
x, y = rest
regs1[x] *= getval(regs1, y)
i += 1
elif cmd == 'mod':
x, y = rest
regs1[x] %= getval(regs1, y)
i += 1
elif cmd == 'rcv':
x, = rest
if q1:
regs1[x] = q1.popleft()
i += 1
else:
lock |= 1
elif cmd == 'jgz':
x, y = rest
if getval(regs1, x) > 0:
i += getval(regs1, y)
else:
i += 1
else:
raise Exception(line)
else:
lock |= 1
if j < ln:
line = file[j]
cmd, *rest = line.split()
if cmd == 'snd':
x, = rest
q1.append(getval(regs2, x))
j += 1
s += 1
elif cmd == 'set':
x, y = rest
regs2[x] = getval(regs2, y)
j += 1
elif cmd == 'add':
x, y = rest
regs2[x] += getval(regs2, y)
j += 1
elif cmd == 'mul':
x, y = rest
regs2[x] *= getval(regs2, y)
j += 1
elif cmd == 'mod':
x, y = rest
regs2[x] %= getval(regs2, y)
j += 1
elif cmd == 'rcv':
x, = rest
if q2:
regs2[x] = q2.popleft()
j += 1
else:
lock |= 2
elif cmd == 'jgz':
x, y = rest
if getval(regs2, x) > 0:
j += getval(regs2, y)
else:
j += 1
else:
raise Exception(line)
else:
lock |= 2
return s
print(part1(file))
print(part2(file))