-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.py
More file actions
250 lines (188 loc) · 8.04 KB
/
Copy pathinput.py
File metadata and controls
250 lines (188 loc) · 8.04 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import urllib3
import regex as re
import os
from mytypes.manual import Manual
from mytypes.vent import Vent
from mytypes.grid import Point, Cuboid
from mytypes.grid import Grid
from mytypes.grid import Point3D
# SESSION_ID = '53616c7465645f5f15f30b143c26686c48bf4ab3aa46ec6f810e146560d7cee82909b260f8866efba45905ac20cb4508743fa9fe1285a794f8be95b23a10bcd7'
SESSION_ID = '53616c7465645f5ffb81f373fd1118fe93c48e795157655f20f885efd6b86cf378de18a0d893b8bf5f2d936208b63c9b3b2673fb888050eb281cb6b5f63b70d3'
def download_input(day, year: int = 2025):
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = f"{dir_path}/input/{year}/day{day}.txt"
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
return file.read().strip()
session_id = SESSION_ID
url: str = "https://adventofcode.com/{}/day/{}/input".format(year, day)
response = urllib3.PoolManager().request("get", url, headers={'Cookie': f'session={session_id}'})
text = str(response.data, 'utf-8').strip()
with open(file_path, "w+") as text_file:
print(f"{text}", file=text_file)
return text
def read_ints(day: int, **kwargs):
return [int(v) for v in read_strings(day, **kwargs)]
def read_list_of_ints(day: int, **kwargs):
return [list(map(int, row)) for row in read_lists(day, **kwargs)]
def read_lists(day, separator=None, **kwargs):
return [line.split(separator) for line in read_strings(day, **kwargs)]
def read_strings(day: int, from_file: bool = False, filename: str = None, year: int = 2024, test: bool = False, strip: bool = True):
return read_strings_from_file(day, filename, test=test, strip=strip) if from_file else [line.strip() if strip else line for line in
download_input(day, year=year).split('\n')]
def read_strings_from_file(day: int, filename: str = None, year: int = 2025, test: bool = False, strip: bool = True):
if filename is None:
filename = f"day{day}{'test' if test else ''}.txt"
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(f"{dir_path}/input/{year}/{filename}", 'r') as file:
values = [line.strip() if strip else line for line in file.readlines()]
return values
def read_bingo(day: int = 4):
lines = read_strings(day)
numbers = [int(s.strip()) for s in lines[0].split(',')]
cards = []
current = []
for line in lines[2:]:
if len(line.strip()) == 0:
cards.append(current)
current = []
else:
current.append([int(s.strip()) for s in line.split()])
return numbers, cards
def read_vents(day: int = 5):
lines = read_strings(day)
vents = []
for line in lines:
coords = line.split('->')
start = Point(*coords[0].strip().split(','))
end = Point(*coords[1].strip().split(','))
vent = Vent(start, end)
vents.append(vent)
return vents
def read_lanternfish(day: int = 6):
return read_csv(day)
def read_csv(day, **kwargs):
return list(map(int, read_strings(day, **kwargs)[0].split(',')))
def read_csv_strings(day, **kwargs):
return list(read_strings(day, **kwargs)[0].split(','))
# Returns a list of lists, one list per column
def read_columns_as_lists(day, **kwargs):
return [list(col) for col in zip(*[line.split() for line in read_strings(day, **kwargs)])]
# Returns a list of lists of ints, one list per column
def read_columns_as_int_lists(day, **kwargs):
return [list(col) for col in zip(*read_list_of_ints(day, **kwargs))]
def read_digits(day: int = 8):
lines = read_strings(day)
output = []
for line in lines:
splits = line.split('|')
inputs = splits[0].strip().split(' ')
outputs = splits[1].strip().split(' ')
output.append((inputs, outputs))
return output
def read_grid(day: int = 9, year=2021, from_file: bool = False, filename: str = None, as_ints: bool = True, **kwargs) -> Grid:
lines = read_strings(day, from_file, filename, year=year, **kwargs)
return Grid(lines, as_ints=as_ints)
def parse_grid(lines):
grid = []
for line in lines:
row = []
for char in line:
row.append(int(char))
grid.append(row)
return grid
def read_graph(day: int = 12, year: int = 2021, from_file: bool = False, filename: str = None) -> dict:
lines = read_strings(day, from_file, filename)
graph = dict()
for line in lines:
add_vertex(graph, tuple(line.split('-')))
return graph
def add_vertex(graph: dict, vertex: tuple):
if not vertex[0] in graph:
graph[vertex[0]] = set()
if not vertex[1] in graph:
graph[vertex[1]] = set()
graph[vertex[0]].add(vertex[1])
graph[vertex[1]].add(vertex[0])
def read_manual(day: int = 13, from_file: bool = False, filename: str = None):
lines = read_strings(day, from_file, filename)
return Manual(lines)
def read_polymer(day: int = 14, from_file: bool = False, filename: str = None):
lines = read_strings(day, from_file, filename)
chain = lines[0]
template = dict()
for line in lines[2:]:
key, value = line.split('->')
template[key.strip()] = value.strip()
return chain, template
def read_scanners(day: int = 19, from_file=False, filename=None):
from day19 import Scanner, Point3D
lines = read_strings(day, from_file, filename)
scanner = None
scanners = []
for line in lines:
if len(line.strip()) == 0:
continue
if line.startswith('---'):
match = re.match('--- scanner (.*) ---', line)
index = match.group(1)
if scanner is not None:
scanners.append(scanner)
scanner = Scanner(index)
else:
scanner.add_beacon(*[int(coord.strip()) for coord in line.split(',')])
scanners.append(scanner)
scanners[0].set_location(Point3D(0, 0, 0))
return scanners
def read_image_data(day: int = 20, from_file=False, filename=None):
lines = read_strings(day, from_file, filename)
enhancement = lines[0]
input_image = []
for line in lines[2:]:
input_image.append(line)
return enhancement, input_image
def read_cuboids(day: int = 22, from_file=False, filename=None):
lines = read_strings(day, from_file, filename)
regex = '(on|off) x=(-?[0-9]+)\\.\\.(-?[0-9]+),y=(-?[0-9]+)\\.\\.(-?[0-9]+),z=(-?[0-9]+)\\.\\.(-?[0-9]+)'
operations = []
for line in lines:
match = re.match(regex, line)
state = match.group(1) == 'on'
cuboid = Cuboid(Point3D(
min(int(match.group(2)), int(match.group(3))),
min(int(match.group(4)), int(match.group(5))),
min(int(match.group(6)), int(match.group(7)))),
Point3D(
max(int(match.group(2)), int(match.group(3))),
max(int(match.group(4)), int(match.group(5))),
max(int(match.group(6)), int(match.group(7)))))
operations.append((state, cuboid))
return operations
def read_alu(day: int = 24, from_file=False, filename=None):
lines = read_strings(day, from_file, filename)
instructions = []
current = None
for line in lines:
if line.startswith('inp'):
if current is not None:
instructions.append(current)
current = []
line += ' '
current.append(tuple(line.split(' ')))
instructions.append(current)
return instructions
def read_cucumbers(day: int = 25, from_file=False, filename=None):
grid = []
for line in read_strings(day, from_file, filename, year=2021):
grid.append(['.>v'.index(c) for c in line])
return grid
def read_points(day: int, year: int, from_file=False, filename=None) -> list[Point]:
return [Point(*line.split(',')) for line in read_strings(day, from_file, filename, year)]
if __name__ == '__main__':
# cuboids = read_cuboids()
# print(cuboids)
# alu = read_alu()
# print(alu)
# print(len(alu))
# print(read_cucumbers(from_file=True, filename='day25test1.txt'))
download_input(1, 2024)