-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
360 lines (277 loc) · 8.64 KB
/
Copy pathmain.py
File metadata and controls
360 lines (277 loc) · 8.64 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import re
def day1_1():
input = open("day1-input.txt", "r")
lines = input.readlines()
numbers = set()
for line in lines:
number = int(line)
if 2020 - number in numbers:
print(2020 - number, number, (2020 - number) * number)
numbers.add(number)
def day1_2():
input = open("day1-input.txt", "r")
lines = input.readlines()
numbers = set()
for line in lines:
number = int(line)
numbers.add(number)
numberPairs = set()
for number1 in numbers:
for number2 in numbers:
numberPairs.add((number1, number2))
for pair in numberPairs:
if 2020 - (pair[0] + pair[1]) in numbers:
print(pair[0], pair[1], 2020 - (pair[0] + pair[1]), pair[0] * pair[1] * (2020 - (pair[0] + pair[1])))
def day2_1():
input = open("day2-input.txt", "r")
lines = input.readlines()
valid = 0
for line in lines:
values = line.split()
cnt = values[2].count(values[1][0])
min = int(values[0].split(sep="-")[0])
max = int(values[0].split(sep="-")[1])
if min <= cnt <= max:
valid += 1
print(valid)
def day2_2():
input = open("day2-input.txt", "r")
lines = input.readlines()
valid = 0
for line in lines:
values = line.split()
char = values[1][0]
p1 = int(values[0].split(sep="-")[0]) - 1
p2 = int(values[0].split(sep="-")[1]) - 1
string = values[2]
text = string[p1] + string[p2]
if text.count(char) == 1:
valid += 1
print(valid)
def day3_12():
input = open("day3-input.txt", "r")
lines = input.readlines()
numbers = set()
t1 = t2 = t3 = t4 = t5 = 0
x1 = x2 = x3 = x4 = x5 = 0
y = True
for line in lines:
if line[x1 % (len(line) - 1)] == "#":
t1 += 1
if line[x2 % (len(line) - 1)] == "#":
t2 += 1
if line[x3 % (len(line) - 1)] == "#":
t3 += 1
if line[x4 % (len(line) - 1)] == "#":
t4 += 1
x1 += 1
x2 += 3
x3 += 5
x4 += 7
if y:
if line[x5 % (len(line) - 1)] == "#":
t5 += 1
x5 += 1
y = not y
print(t2)
print(t1 * t2 * t3 * t4 * t5)
def day4_1():
input = open("day4-input.txt", "r")
lines = input.readlines()
d = {}
valid = 0
for line in lines:
line = line.rstrip('\n')
if line == "":
if set(["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]).issubset(set(d.keys())):
valid += 1
d = {}
continue
pairs = line.split()
for pair in pairs:
key, value = pair.split(":")
d[key] = value
print(valid)
def day4_2():
input = open("day4-input.txt", "r")
lines = input.readlines()
d = {}
valid = 0
for line in lines:
line = line.rstrip('\n')
if line == "":
if not {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}.issubset(set(d.keys())):
d = {}
continue
if not 1920 <= int(d["byr"]) <= 2002:
d = {}
continue
if not 2010 <= int(d["iyr"]) <= 2020:
d = {}
continue
if not 2020 <= int(d["eyr"]) <= 2030:
d = {}
continue
if not (len(d["hcl"]) == 7 and d["hcl"][0] == "#"):
d = {}
continue
try:
int(d["hcl"][1:8], 16)
except:
print("found bad color")
d = {}
continue
if not ((d["hgt"].endswith("cm") and len(d["hgt"]) == 5 and 150 <= int(d["hgt"][0:3]) <= 193) or
(d["hgt"].endswith("in") and len(d["hgt"]) == 4 and 59 <= int(d["hgt"][0:2]) <= 76)):
d = {}
continue
if d["ecl"][0:3] not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
d = {}
continue
if not len(d["pid"]) == 9:
d = {}
continue
try:
int(d["pid"])
except:
d = {}
continue
valid += 1
d = {}
pairs = line.split()
for pair in pairs:
key, value = pair.split(":")
d[key] = value
print(valid)
def day5_1():
input = open("day5-input.txt", "r")
lines = input.readlines()
max_id = 0
for line in lines:
row_code = line[0:7]
col_code = line[7:10]
row_bytes = row_code.replace("F", "0").replace("B", "1")
row_num = int(row_bytes, base=2)
col_bytes = col_code.replace("L", "0").replace("R", "1")
col_num = int(col_bytes, base=2)
seat_id = row_num * 8 + col_num
max_id = max(max_id, seat_id)
print(max_id)
def day5_2():
input = open("day5-input.txt", "r")
lines = input.readlines()
s = set()
p = set()
for line in lines:
row_code = line[0:7]
col_code = line[7:10]
row_bytes = row_code.replace("F", "0").replace("B", "1")
row_num = int(row_bytes, base=2)
col_bytes = col_code.replace("L", "0").replace("R", "1")
col_num = int(col_bytes, base=2)
seat_id = row_num * 8 + col_num
s.add(seat_id)
if seat_id in p:
p.remove(seat_id)
if seat_id + 1 not in s and seat_id + 2 in s:
p.add(seat_id + 1)
if seat_id - 1 not in s and seat_id - 2 in s:
p.add(seat_id - 1)
print(p)
def day6_1():
input = open("day6-input.txt", "r")
lines = input.readlines()
suma = 0
s = set()
for line in lines:
line = line.rstrip('\n')
if line == "":
suma += len(s)
s = set()
for letter in line:
s.add(letter)
print(suma)
def day6_2():
input = open("day6-input.txt", "r")
lines = input.readlines()
suma = 0
s = set()
first = True
for line in lines:
line = line.rstrip('\n')
if line == "":
suma += len(s)
s = set()
first = True
continue
user_set = set()
for letter in line:
user_set.add(letter)
if first:
first = False
s = user_set
continue
else:
s = s.intersection(user_set)
print(suma)
def day7_1():
input = open("day7-input.txt", "r")
lines = input.readlines()
bagjacency_list = {}
for line in lines:
line = line.rstrip('\n')
key, contents = line.split(sep=" bags contain ")
for sub in ["no other bags", " bags", " bag", "."]:
contents = contents.replace(sub, "")
contents = contents.split(sep=", ")
for bag_type in contents:
bag_split = bag_type.split()
if len(bag_split) == 0:
continue
count = int(bag_split[0])
type = " ".join(bag_split[1:])
if type not in bagjacency_list:
bagjacency_list[type] = set()
if key not in bagjacency_list:
bagjacency_list[key] = set()
bagjacency_list[type].add(key)
visited = set()
to_visit = ["shiny gold"]
while len(to_visit) > 0:
current = to_visit.pop()
visited.add(current)
for neighbor in bagjacency_list[current]:
if neighbor not in visited:
to_visit.append(neighbor)
print(len(visited)-1)
def day7_2():
input = open("day7-input.txt", "r")
lines = input.readlines()
bagjacency_list = {}
for line in lines:
line = line.rstrip('\n')
key, contents = line.split(sep=" bags contain ")
for sub in ["no other bags", " bags", " bag", "."]:
contents = contents.replace(sub, "")
contents = contents.split(sep=", ")
if key not in bagjacency_list:
bagjacency_list[key] = set()
for bag_type in contents:
bag_split = bag_type.split()
if len(bag_split) == 0:
continue
count = int(bag_split[0])
type = " ".join(bag_split[1:])
if type not in bagjacency_list:
bagjacency_list[type] = set()
bagjacency_list[key].add((type, count))
def visit(to_visit, count):
if len(bagjacency_list[to_visit]) == 0:
return count
sum = 0
for neighbor, i in bagjacency_list[to_visit]:
sum += visit(neighbor, i)
return sum * count + count
print(visit("shiny gold", 1) - 1)
if __name__ == '__main__':
day7_2()