Implement days 20 and 21.

This commit is contained in:
2024-01-09 18:43:27 -05:00
parent 24cd18c21d
commit b34b8fce66
3 changed files with 180 additions and 0 deletions

View File

@@ -24,3 +24,5 @@
LEADERBOARD?!?!?!?!?!?!?!
- Day 17: Second one was fun with having to detect the repetition.
- Day 18: 12:00 and 32:00; really straightforward and of course way too slow.
- Day 19: Slow.
- Day 20: Struggled way too much.

73
d20.py Normal file
View File

@@ -0,0 +1,73 @@
from lib import *
EXAMPLE = """1
2
-3
3
-2
0
4
"""
def move(xs, i, move):
move = move % (len(xs) - 1)
ni = (i + move + 1) % len(xs)
xsi, xs[i] = xs[i], None
xs.insert(ni, xsi)
xs.remove(None)
return xs
def asserts():
assert move([1, 2, 3], 0, 0) == [1, 2, 3]
assert move([1, 2, 3], 0, 1) == [2, 1, 3]
assert move([1, 2, 3], 0, 2) == [1, 2, 3]
assert move([1, 2, 3], 0, 3) == [2, 1, 3]
assert move([1, 2, 3], 0, 4) == [1, 2, 3]
assert move([1, 2, 3], 0, 5) == [2, 1, 3]
assert move([1, 2, 3], 0, 2) == [1, 2, 3]
assert move([3, 2, 1], 2, 1) == [3, 1, 2]
assert move([3, 2, 1], 2, 2) == [1, 3, 2]
assert move([4, -2, 5, 6, 7, 8, 9,], 1, -2) == [4, 5, 6, 7, 8, -2, 9]
assert move([1, 2, -2, -3, 0, 3, 4], 2, -2) == [-2, 1, 2, -3, 0, 3, 4]
def solve(input: Input, second=False):
xs = list(map(int, input.lines()))
if second:
xs = list(map(lambda n: n * 811589153, xs))
ys = list(range(len(xs)))
oys = ys[:]
repeat = 1
if second:
repeat = 10
for _ in range(repeat):
for o in oys:
i = ys.index(o)
movev = xs[o]
move(ys, i, movev)
xs = [xs[i] for i in ys]
s = 0
i0 = xs.index(0)
for i in range(1, 4):
ni = (i0 + i * 1000) % len(ys)
s += xs[ni]
return s
def main():
DAY_INPUT = "i20.txt"
print("Example 1:", solve(Input(EXAMPLE)))
print("Solution 1:", solve(Input(DAY_INPUT)))
print("Example 2:", solve(Input(EXAMPLE), True))
print("Solution 2:", solve(Input(DAY_INPUT), True))
assert solve(Input(DAY_INPUT), True) == 6704537992933
if __name__ == "__main__":
asserts()
main()

105
d21.py Normal file
View File

@@ -0,0 +1,105 @@
from lib import *
EXAMPLE = """root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32
"""
def resolve(monkeys, s):
equ = monkeys[s]
try:
return int(equ)
except ValueError:
pass
l, op, r = equ.split(" ")
l = resolve(monkeys, l)
r = resolve(monkeys, r)
res = eval(f"{l}{op}{r}")
monkeys[s] = res
return res
def resolve2(monkeys, s):
if s == "humn":
return tuple()
equ = monkeys[s]
try:
return int(equ)
except ValueError:
pass
l, op, r = equ.split(" ")
if s == "root":
op = "=="
l = resolve2(monkeys, l)
r = resolve2(monkeys, r)
if isinstance(l, tuple) or isinstance(r, tuple):
return (l, op, r)
if op == "/":
op = "//"
res = eval(f"{l}{op}{r}")
monkeys[s] = res
return res
def simplify(equ, exp):
if equ == tuple():
return exp
l, op, r = equ
if op == '/' and isinstance(r, int):
return simplify(l, exp * r)
elif op == '+' and isinstance(l, int):
return simplify(r, exp - l)
elif op == '+' and isinstance(r, int):
return simplify(l, exp - r)
elif op == '-' and isinstance(l, int):
return simplify(r, -(exp - l))
elif op == '-' and isinstance(r, int):
return simplify(l, exp + r)
elif op == '*' and isinstance(r, int):
return simplify(l, exp // r)
elif op == '*' and isinstance(l, int):
return simplify(r, exp // l)
def solve(input: Input, second=False):
res = 0
monkeys = {}
for line in input.lines():
l, r = line.split(": ")
monkeys[l] = r
if not second:
return int(resolve(monkeys, "root"))
else:
equ = resolve2(monkeys, "root")
assert equ[1] == '=='
return simplify(equ[0], equ[2])
def main():
DAY_INPUT = "i21.txt"
print("Example 1:", solve(Input(EXAMPLE)))
print("Solution 1:", solve(Input(DAY_INPUT)))
print("Example 2:", solve(Input(EXAMPLE), True))
print("Solution 2:", solve(Input(DAY_INPUT), True))
assert solve(Input(DAY_INPUT), True) == 3243420789721
if __name__ == "__main__":
main()