-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
153 lines (135 loc) · 3.92 KB
/
solution.py
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
152
153
#!/usr/bin/env pypy3
from sys import stdin, exit
from copy import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
import numpy as np
import os
import sys
import requests
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
def submit(part: int, ans):
res = input(f"Submit answer {ans}? [Y/n]")
if 'N' in res or 'n' in res:
return
day = int(os.path.basename(os.path.dirname(os.path.realpath(__file__))))
r = requests.post(f"https://adventofcode.com/2022/day/{day}/answer", data={
"level": part, "answer": ans}, cookies={"session": get_session()})
res = r.text
print(res)
print(res.split("\n")[-16])
with open("input/input.txt", "r") as f:
data = f.read().split('\n')[:-1]
rock_data = """
####
.#.
###
.#.
..#
..#
###
#
#
#
#
##
##
""".strip().split("\n")
rocks: list[list[tuple[int, int]]] = []
rocks_h: list[int] = []
cur = 0
while cur < len(rock_data):
if rock_data[cur] == "":
cur += 1
cur_rock: list[tuple[int, int]] = []
r = 0
while cur < len(rock_data) and rock_data[cur] != "":
for c, x in enumerate(rock_data[cur]):
if x == "#":
cur_rock.append((r, c))
r -= 1
cur += 1
rocks.append(cur_rock)
rocks_h.append(-r + 1)
print(rocks)
def check_rock(filled: set[tuple[int, int]], rock: list[tuple[int, int]], r: int, c: int):
for coord in rock:
if coord[0] + r <= 0:
return False
if not 0 <= coord[1] + c < 7:
return False
if (coord[0] + r, coord[1] + c) in filled:
return False
return True
def part1():
ans = 0
filled: set[tuple[int, int]] = set()
cur = 0
for i in range(2022):
rock = rocks[i % 5]
r = ans + rocks_h[i % 5] + 2
c = 2
assert check_rock(filled, rock, r, c)
while True:
left = data[0][cur % len(data[0])] == "<"
cur += 1
if left and check_rock(filled, rock, r, c - 1):
c -= 1
elif not left and check_rock(filled, rock, r, c + 1):
c += 1
if not check_rock(filled, rock, r - 1, c):
break
r -= 1
assert check_rock(filled, rock, r, c)
for coord in rock:
filled.add((coord[0] + r, coord[1] + c))
ans = max(ans, coord[0] + r)
# submit(1, ans)
print(ans)
def part2():
ans = 0
skipped_ans = 0
filled: set[tuple[int, int]] = set()
cur = 0
hist: dict[tuple[int, int], tuple[int, int]] = {}
i = 0
timeskipping = True
target = 1000000000000
while i < target:
rock = rocks[i % 5]
r = ans + rocks_h[i % 5] + 2
c = 2
assert check_rock(filled, rock, r, c)
while True:
left = data[0][cur % len(data[0])] == "<"
cur += 1
if left and check_rock(filled, rock, r, c - 1):
c -= 1
elif not left and check_rock(filled, rock, r, c + 1):
c += 1
if not check_rock(filled, rock, r - 1, c):
break
r -= 1
assert check_rock(filled, rock, r, c)
for coord in rock:
filled.add((coord[0] + r, coord[1] + c))
ans = max(ans, coord[0] + r)
if i > 2022 and timeskipping:
if (i % 5, cur % len(data[0])) in hist:
x = hist[i % 5, cur % len(data[0])]
drops = i - x[1]
cycles = (target - i) // drops
i += cycles * drops + 1
skipped_ans = (ans - x[0]) * cycles
timeskipping = False
continue
hist[i % 5, cur % len(data[0])] = (ans, i)
i += 1
# submit(2, ans + skipped_ans)
print(ans + skipped_ans)
if __name__ == "__main__":
part1()
part2()