Solve 2019 day 23
This commit is contained in:
parent
df31b47934
commit
67914a642a
22
2019/d22.py
22
2019/d22.py
@ -9,12 +9,12 @@ for line in data.splitlines():
|
|||||||
if "new stack" in line:
|
if "new stack" in line:
|
||||||
deck = list(reversed(deck))
|
deck = list(reversed(deck))
|
||||||
elif "cut" in line:
|
elif "cut" in line:
|
||||||
n, = str_to_ints(line)
|
(n,) = str_to_ints(line)
|
||||||
deck = deck[n:] + deck[:n]
|
deck = deck[n:] + deck[:n]
|
||||||
elif "increment" in line:
|
elif "increment" in line:
|
||||||
new_deck = [-1] * len(deck)
|
new_deck = [-1] * len(deck)
|
||||||
pos = 0
|
pos = 0
|
||||||
n, = str_to_ints(line)
|
(n,) = str_to_ints(line)
|
||||||
deck = list(reversed(deck))
|
deck = list(reversed(deck))
|
||||||
while deck:
|
while deck:
|
||||||
new_deck[pos] = deck.pop()
|
new_deck[pos] = deck.pop()
|
||||||
@ -37,7 +37,7 @@ for line in data.splitlines():
|
|||||||
assert rev_index == index
|
assert rev_index == index
|
||||||
index = new_index
|
index = new_index
|
||||||
elif "cut" in line:
|
elif "cut" in line:
|
||||||
cut, = str_to_ints(line)
|
(cut,) = str_to_ints(line)
|
||||||
cut = (len + cut) % len
|
cut = (len + cut) % len
|
||||||
if index >= cut:
|
if index >= cut:
|
||||||
new_index = index - cut
|
new_index = index - cut
|
||||||
@ -48,7 +48,7 @@ for line in data.splitlines():
|
|||||||
index = new_index
|
index = new_index
|
||||||
# calculate index from new_index and store in rev_index
|
# calculate index from new_index and store in rev_index
|
||||||
elif "increment" in line:
|
elif "increment" in line:
|
||||||
n, = str_to_ints(line)
|
(n,) = str_to_ints(line)
|
||||||
assert gcd(n, len) == 1
|
assert gcd(n, len) == 1
|
||||||
new_index = (n * index) % len
|
new_index = (n * index) % len
|
||||||
m = mod_inverse(n, len)
|
m = mod_inverse(n, len)
|
||||||
@ -63,11 +63,11 @@ for line in reversed(data.splitlines()):
|
|||||||
if "new stack" in line:
|
if "new stack" in line:
|
||||||
index = -(index - len + 1)
|
index = -(index - len + 1)
|
||||||
elif "cut" in line:
|
elif "cut" in line:
|
||||||
cut, = str_to_ints(line)
|
(cut,) = str_to_ints(line)
|
||||||
cut = (len + cut) % len
|
cut = (len + cut) % len
|
||||||
index = (index + cut) % len
|
index = (index + cut) % len
|
||||||
elif "increment" in line:
|
elif "increment" in line:
|
||||||
n, = str_to_ints(line)
|
(n,) = str_to_ints(line)
|
||||||
assert gcd(n, len) == 1
|
assert gcd(n, len) == 1
|
||||||
m = mod_inverse(n, len)
|
m = mod_inverse(n, len)
|
||||||
index = (index * m) % len
|
index = (index * m) % len
|
||||||
@ -80,6 +80,7 @@ len = 119315717514047
|
|||||||
|
|
||||||
# get expression for one loop using sympy
|
# get expression for one loop using sympy
|
||||||
from sympy import symbols, simplify
|
from sympy import symbols, simplify
|
||||||
|
|
||||||
index = symbols("index")
|
index = symbols("index")
|
||||||
expr = index
|
expr = index
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@ -87,16 +88,16 @@ for line in lines:
|
|||||||
# index = -(index - len + 1)
|
# index = -(index - len + 1)
|
||||||
expr = -(expr - len + 1)
|
expr = -(expr - len + 1)
|
||||||
elif "cut" in line:
|
elif "cut" in line:
|
||||||
cut, = str_to_ints(line)
|
(cut,) = str_to_ints(line)
|
||||||
cut = (len + cut) % len
|
cut = (len + cut) % len
|
||||||
# index = (index + cut) % len
|
# index = (index + cut) % len
|
||||||
expr = (expr + cut)
|
expr = expr + cut
|
||||||
elif "increment" in line:
|
elif "increment" in line:
|
||||||
n, = str_to_ints(line)
|
(n,) = str_to_ints(line)
|
||||||
assert gcd(n, len) == 1
|
assert gcd(n, len) == 1
|
||||||
m = mod_inverse(n, len)
|
m = mod_inverse(n, len)
|
||||||
# index = (index * m) % len
|
# index = (index * m) % len
|
||||||
expr = (expr * m)
|
expr = expr * m
|
||||||
|
|
||||||
# we can see that expression is in the form (a - b * i) % m
|
# we can see that expression is in the form (a - b * i) % m
|
||||||
expr = simplify(expr % len)
|
expr = simplify(expr % len)
|
||||||
@ -115,4 +116,3 @@ term1 = (p_t * r0) % m
|
|||||||
term2 = (a * (1 - p_t) * inv_b1) % m
|
term2 = (a * (1 - p_t) * inv_b1) % m
|
||||||
r_t = (term1 + term2) % m
|
r_t = (term1 + term2) % m
|
||||||
print(r_t)
|
print(r_t)
|
||||||
|
|
||||||
|
47
2019/d23.py
Normal file
47
2019/d23.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from lib import get_data, str_to_ints
|
||||||
|
from d9 import Amp
|
||||||
|
|
||||||
|
xs = str_to_ints(get_data(__file__))
|
||||||
|
first_255 = True
|
||||||
|
nat = None
|
||||||
|
y_prev = None
|
||||||
|
|
||||||
|
amps = []
|
||||||
|
for i in range(50):
|
||||||
|
a = Amp(xs)
|
||||||
|
a.feed(i)
|
||||||
|
a.go()
|
||||||
|
amps.append(a)
|
||||||
|
|
||||||
|
for j in range(1_000_000):
|
||||||
|
was_active = False
|
||||||
|
for i, a in enumerate(amps):
|
||||||
|
a.go()
|
||||||
|
if a.input_required:
|
||||||
|
a.feed(-1)
|
||||||
|
|
||||||
|
if len(a.outputs) > 0:
|
||||||
|
was_active = True
|
||||||
|
while len(a.outputs) < 3:
|
||||||
|
a.go()
|
||||||
|
addr = a.pop()
|
||||||
|
x = a.pop()
|
||||||
|
y = a.pop()
|
||||||
|
if addr == 255:
|
||||||
|
nat = (x, y)
|
||||||
|
if first_255:
|
||||||
|
print(y)
|
||||||
|
first_255 = False
|
||||||
|
else:
|
||||||
|
amps[addr].feed(x)
|
||||||
|
amps[addr].feed(y)
|
||||||
|
|
||||||
|
if not was_active and nat is not None:
|
||||||
|
x, y = nat
|
||||||
|
if y == y_prev:
|
||||||
|
print(y)
|
||||||
|
exit()
|
||||||
|
amps[0].feed(x)
|
||||||
|
amps[0].feed(y)
|
||||||
|
nat = None
|
||||||
|
y_prev = y
|
@ -138,7 +138,8 @@ Solutions and utility script for Advent of Code challenges in Python.
|
|||||||
- Day 20: days (Not actually that hard but I struggled for no reason.)
|
- Day 20: days (Not actually that hard but I struggled for no reason.)
|
||||||
- Day 21: days (But it was super fun!)
|
- Day 21: days (But it was super fun!)
|
||||||
- Day 22: days (Needed some help...)
|
- Day 22: days (Needed some help...)
|
||||||
- Day 23:
|
- Day 23: 23:13 (Still too slow even though my int computer was in good shape...)
|
||||||
|
- Day 14:
|
||||||
|
|
||||||
## AoC 2020
|
## AoC 2020
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user