Solve day 18 and 19 2017.
This commit is contained in:
parent
5b7b9c87cb
commit
ecda947fb4
109
2017/d18.py
Normal file
109
2017/d18.py
Normal file
@ -0,0 +1,109 @@
|
||||
from lib import LETTERS_LOWER
|
||||
from collections import defaultdict, deque
|
||||
|
||||
|
||||
def get_value(regs, val):
|
||||
if val in LETTERS_LOWER:
|
||||
return regs[val]
|
||||
return int(val)
|
||||
|
||||
|
||||
def part_1(data):
|
||||
regs = defaultdict(int)
|
||||
insts = data.splitlines()
|
||||
last_sound = None
|
||||
pc = 0
|
||||
while pc <= len(insts):
|
||||
cmds = insts[pc].split()
|
||||
match cmds:
|
||||
case ["set", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = val
|
||||
case ["mul", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] * val
|
||||
case ["add", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] + val
|
||||
case ["mod", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] % val
|
||||
case ["rcv", val]:
|
||||
if get_value(regs, val) != 0:
|
||||
print(last_sound)
|
||||
return
|
||||
case ["snd", val]:
|
||||
last_sound = get_value(regs, val)
|
||||
case ["jgz", cnd, off]:
|
||||
if get_value(regs, cnd) > 0:
|
||||
pc += get_value(regs, off) # XXX?
|
||||
continue
|
||||
case _:
|
||||
print(cmds)
|
||||
assert False
|
||||
pc += 1
|
||||
|
||||
|
||||
def part_2(data):
|
||||
progs = []
|
||||
insts = data.splitlines()
|
||||
prog1_counter = 0
|
||||
|
||||
for i in [0, 1]:
|
||||
regs = defaultdict(int)
|
||||
regs["p"] = i
|
||||
pc = 0
|
||||
progs.append([pc, regs, False, deque()])
|
||||
|
||||
while not all(prog[2] for prog in progs):
|
||||
for prog in progs:
|
||||
pc, regs, waiting, queue = prog
|
||||
|
||||
cmds = insts[pc].split()
|
||||
match cmds:
|
||||
case ["set", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = val
|
||||
case ["mul", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] * val
|
||||
case ["add", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] + val
|
||||
case ["mod", reg, val]:
|
||||
val = get_value(regs, val)
|
||||
regs[reg] = regs[reg] % val
|
||||
case ["rcv", reg]:
|
||||
if queue:
|
||||
val = queue.popleft()
|
||||
regs[reg] = val
|
||||
else:
|
||||
prog[2] = True
|
||||
pc -= 1
|
||||
case ["snd", val]:
|
||||
val = get_value(regs, val)
|
||||
if prog == progs[0]:
|
||||
other_prog = progs[1]
|
||||
else:
|
||||
prog1_counter += 1
|
||||
other_prog = progs[0]
|
||||
other_prog[3].append(val)
|
||||
case ["jgz", cnd, off]:
|
||||
if get_value(regs, cnd) > 0:
|
||||
pc += get_value(regs, off) # XXX?
|
||||
pc -= 1
|
||||
case _:
|
||||
print(cmds)
|
||||
assert False
|
||||
prog[0] = pc + 1
|
||||
print(prog1_counter)
|
||||
|
||||
|
||||
def main():
|
||||
data = open(0).read().strip()
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
50
2017/d19.py
Normal file
50
2017/d19.py
Normal file
@ -0,0 +1,50 @@
|
||||
from lib import Grid2D, LETTERS_UPPER, add2, sub2
|
||||
|
||||
|
||||
def part_1(data):
|
||||
g = Grid2D(data)
|
||||
g.print()
|
||||
|
||||
current = None
|
||||
for c in range(g.n_cols):
|
||||
if g[(0, c)] == "|":
|
||||
current = (0, c)
|
||||
|
||||
current_dir = Grid2D.S
|
||||
letters = ""
|
||||
done = False
|
||||
steps = 0
|
||||
|
||||
while not done:
|
||||
while g[current] != " ":
|
||||
current = add2(current, current_dir)
|
||||
steps += 1
|
||||
if g[current] in LETTERS_UPPER:
|
||||
letter = g[current]
|
||||
if letter in letters:
|
||||
done = True
|
||||
break
|
||||
letters += letter
|
||||
|
||||
if g[current] == "+":
|
||||
break
|
||||
|
||||
next_field = None
|
||||
for nb in g.neighbors_ort(current):
|
||||
if nb != sub2(current, current_dir) and (g[nb] in "-|" or g[nb] in LETTERS_UPPER):
|
||||
next_field = nb
|
||||
if next_field is None:
|
||||
done = True
|
||||
else:
|
||||
current_dir = sub2(next_field, current)
|
||||
print(letters)
|
||||
print(steps)
|
||||
|
||||
|
||||
def main():
|
||||
data = open(0).read()
|
||||
part_1(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -27,6 +27,9 @@ def mape(f, xs):
|
||||
def add2(a: tuple[int, int], b: tuple[int, int]) -> tuple[int, int]:
|
||||
return (a[0] + b[0], a[1] + b[1])
|
||||
|
||||
def sub2(a: tuple[int, int], b: tuple[int, int]) -> tuple[int, int]:
|
||||
return (a[0] - b[0], a[1] - b[1])
|
||||
|
||||
class Grid2D:
|
||||
N = (-1, 0)
|
||||
E = (0, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user