Roderic Day 1 anno fa
parent
commit
4817bac686
2 ha cambiato i file con 43 aggiunte e 24 eliminazioni
  1. +1
    -1
      VERSION
  2. +42
    -23
      y2023/p21.py

+ 1
- 1
VERSION Vedi File

@@ -1 +1 @@
🦁
🐄

+ 42
- 23
y2023/p21.py Vedi File

@@ -1,35 +1,54 @@
"""
5000 steps, he can reach 16733044 garden plots.
def bfs_twinkle(start, graph, cycles):
seen = [set()]
state = {start}
while len(seen) <= cycles:
twinkle = seen[len(seen) - 2]
state = {
p + s
for p in state
for s in [1, -1, 1j, -1j]
if wrap(p + s) in graph
if p + s not in twinkle
}
seen.append(twinkle | state)
return seen


def extrapolate_quadratic(y0, y1, y2):
a, b, c = (y2 - 2 * y1 + y0) / 2, (-y2 + 4 * y1 - 3 * y0) / 2, y0
return lambda x: a * x ** 2 + b * x + c


26501365?
"""
text = open(0).read()
graph = {
complex(x, y): val
for y, row in enumerate(text.splitlines())
for x, val in enumerate(row)
if val in {'S', '.'}
}

width = max(int(p.real) for p in graph) + 1
height = max(int(p.imag) for p in graph) + 1
block = lambda p: complex(p.real // width, p.imag // height)
wrap = lambda p: complex(p.real % width, p.imag % height)
wrap = lambda p: complex(int(p.real % width), int(p.imag % height))

start, = (k for k, v in graph.items() if v == 'S')
seen = [set(), set()]
state = {start}
bops = set()
for step in range(130):
twinkle = seen[step % 2]
twinkle |= state

if step == 64:
print(len(twinkle)) # ans1

state = {
p + s
for p in state
for s in [1, -1, 1j, -1j]
if graph[wrap(p + s)] in {'.', 'S'}
if p + s not in twinkle
}
seen = bfs_twinkle(start, graph, 327)
if width < 100:
check = len({wrap(p) for p in seen[6]})
print(check)
exit()

ans1 = len({wrap(p) for p in seen[64]})
print(ans1)

goal = 26501365
size, = {height, width}
offset = goal % size

xs = [offset + size * i for i in range(3)]
ys = [len(seen[x]) for x in xs]
yf = extrapolate_quadratic(ys[0], ys[1], ys[2])

x = (goal - offset) / size
ans2 = int(yf(x))
print(ans2)

Loading…
Annulla
Salva