-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode2020.py
More file actions
342 lines (288 loc) · 11.1 KB
/
code2020.py
File metadata and controls
342 lines (288 loc) · 11.1 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
"""https://adventofcode.com/2020"""
from argparse import ArgumentParser
from time import time
from re import match
from math import ceil
class Day1:
"""https://adventofcode.com/2020/day/1"""
def __init__(self, filename="input/2020/day_1.txt", result=2020):
self.filename = filename
self.result = result
res = self.iterate_file(units=2)
print("CHALLENGE 2020.1.1: "+str(res))
res = self.iterate_file(units=3)
print("CHALLENGE 2020.1.2: "+str(res))
def find_added(self, data, partial_sum, partial_prod, units):
counter = 0
for x in data:
if units > 1:
match = self.find_added(data[counter:], partial_sum + x, partial_prod * x, units - 1)
if match:
return match
if self.result == partial_sum + x:
return partial_prod * x
counter += 1
return
def iterate_file(self, units):
"""CHALLENGE 1.1 - 1016131
CHALLENGE 1.2 - 276432018"""
data = [int(x.replace("\n","")) for x in open(self.filename, "r")]
return self.find_added(data, 0, 1, units)
class Day2:
"""https://adventofcode.com/2020/day/2"""
def __init__(self, filename="input/2020/day_2.txt"):
self.filename = filename
res = self.count_occurrences()
print("CHALLENGE 2020.2.1: "+str(res))
res = self.check_one_occurrence()
print("CHALLENGE 2020.2.2: "+str(res))
@staticmethod
def clean_line(line):
occurrences, character, password = line.split(" ")
minimum, maximum = occurrences.split("-")
return int(minimum), int(maximum), character.replace(":", "").strip(), password.strip()
def count_occurrences(self):
"""CHALLENGE 2.1 - 414"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
valid = 0
for line in data:
minimum, maximum, character, password = self.clean_line(line)
if maximum >= password.count(character) >= minimum:
valid += 1
return valid
def check_one_occurrence(self):
"""CHALLENGE 2.2 - 413"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
valid = 0
for line in data:
first, second, character, password = self.clean_line(line)
if password[first - 1] == character and password[second - 1] != character:
valid += 1
if password[first - 1] != character and password[second - 1] == character:
valid += 1
return valid
class Day3:
"""https://adventofcode.com/2020/day/3"""
def __init__(self, filename="input/2020/day_3.txt"):
self.filename = filename
res = self.count_slop(down=1, right=3)
print("CHALLENGE 2020.3.1: "+str(res))
res = self.check_slops()
print("CHALLENGE 2020.3.2: "+str(res))
def count_slop(self, down, right):
"""CHALLENGE 3.1 - 200"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
position, trees, jump = right, 0, down
count_line = 1
for line in data[1:]:
if jump > 1:
jump -= 1
count_line += 1
continue
count_line += 1
if position >= len(line):
position -= len(line)
if line[position] == "#":
trees += 1
position += right
jump = down
return trees
def check_slops(self):
"""CHALLENGE 3.2 - 3737923200"""
result = self.count_slop(down=1, right=1)
result *= self.count_slop(down=1, right=3)
result *= self.count_slop(down=1, right=5)
result *= self.count_slop(down=1, right=7)
result *= self.count_slop(down=2, right=1)
return result
class Day4:
"""https://adventofcode.com/2020/day/4"""
def __init__(self, filename="input/2020/day_4.txt"):
self.filename = filename
self.required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
self.optional_fields = ["cid"]
res = self.valid_passports()
print("CHALLENGE 2020.4.1: "+str(res))
res = self.valid_passports(check_content=True)
print("CHALLENGE 2020.4.2: "+str(res))
@staticmethod
def clean_data(line):
data = {}
for pair in line.split(" "):
key, value = pair.split(":")
data[key] = value
return data
@staticmethod
def validate_key(key, value):
if key == "cid":
return True
elif key == "byr":
if 2002 >= int(value) >= 1920:
return True
elif key == "iyr":
if 2020 >= int(value) >= 2010:
return True
elif key == "eyr":
if 2030 >= int(value) >= 2020:
return True
elif key == "hgt":
if "cm" in value:
if 193 >= int(value.replace("cm", "")) >= 150:
return True
if "in" in value:
if 76 >= int(value.replace("in", "")) >= 59:
return True
elif key == "hcl":
return True if match(r"#[0-9a-f]{6}", value) else None
elif key == "ecl":
if value.strip() in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth"):
return True
elif key == "pid":
if len(value.strip()) == 9 and int(value.strip()):
return True
def valid_passports(self, check_content=False):
"""CHALLENGE 4.1 - 204
CHALLENGE 4.2 - 179"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
data.append("")
missing_fields = self.required_fields.copy()
valid = 0
for x in data:
if x == "":
if not missing_fields:
valid += 1
missing_fields = self.required_fields.copy()
continue
key_pairs = self.clean_data(x)
for key in key_pairs.keys():
if check_content and not self.validate_key(key, key_pairs[key]):
continue
try:
missing_fields.remove(key)
except ValueError:
pass
return valid
class Day5:
"""https://adventofcode.com/2020/day/5"""
def __init__(self, filename="input/2020/day_5.txt"):
self.filename = filename
self.rows = 127
self.columns = 7
res = self.highest_seat()
print("CHALLENGE 2020.5.1: "+str(res))
res = self.check_seat()
print("CHALLENGE 2020.5.2: "+str(res))
def compute_seat(self, seat):
row, column = [0, self.rows], [0, self.columns]
for x in seat:
if x == "F":
row[1] -= ceil((row[1] - row[0]) / 2)
elif x == "B":
row[0] += ceil((row[1] - row[0]) / 2)
elif x == "L":
column[1] -= ceil((column[1] - column[0]) / 2)
elif x == "R":
column[0] += ceil((column[1] - column[0]) / 2)
return row[0] * 8 + column[0]
def highest_seat(self):
"""CHALLENGE 5.1 - 848"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
highest = data[0]
for seat in data[1:]:
for pos, label in enumerate(seat):
if label == highest[pos]:
continue
if label in ("B", "R"):
highest = seat
break
return self.compute_seat(highest)
def check_seat(self):
"""CHALLENGE 5.2 - 682"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
all_seats = [self.compute_seat(seat) for seat in data]
all_seats.sort()
for pos, seat in enumerate(all_seats):
if seat + 1 != all_seats[pos + 1]:
return seat + 1
class Day6:
"""https://adventofcode.com/2020/day/6"""
def __init__(self, filename="input/2020/day_6.txt"):
self.filename = filename
res = self.sum_yes_anyone()
print("CHALLENGE 2020.6.1: "+str(res))
res = self.sum_yes_everyone()
print("CHALLENGE 2020.6.2: "+str(res))
def sum_yes_anyone(self):
"""CHALLENGE 6.1 - 6249"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
data.append("")
result, answers = 0, []
for user in data:
if user == "":
result += len(set(answers))
answers = []
continue
[answers.append(x) for x in user]
return result
def sum_yes_everyone(self):
"""CHALLENGE 6.2 - 3103"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
data.append("")
result, answers = 0, None
for user in data:
if user == "":
result += len(set(answers))
answers = None
continue
latest = [x for x in user]
if answers is None:
answers = latest
continue
updated_answers = []
for x in answers:
if x in latest:
updated_answers.append(x)
answers = updated_answers
return result
class Day7:
"""https://adventofcode.com/2020/day/7"""
def __init__(self, filename="input/2020/day_7.txt"):
self.filename = filename
res = len(self.count_outer_bags(bag="shiny gold"))
print("CHALLENGE 2020.7.1: "+str(res))
res = self.count_inner_bags(bag="shiny gold")
print("CHALLENGE 2020.7.2: "+str(res))
def count_outer_bags(self, bag, bags=[]):
"""CHALLENGE 7.1 - 378"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
for x in data:
if not x.startswith(bag) and bag in x:
if x.split("bags")[0].strip() not in bags:
bags.append(x.split("bags")[0].strip())
bags = self.count_outer_bags(x.split("bags")[0].strip(), bags)
return bags
def count_inner_bags(self, bag, outer_count=1, total=0):
"""CHALLENGE 7.2 - 27526"""
data = [x.replace("\n","") for x in open(self.filename, "r")]
for x in data:
if x.startswith(bag):
content = x.split("contain")[1]
for each in content.split(","):
each = each.replace("bags", "").replace("bag", "").replace(".", "").strip()
inner_count, inner_bag = each.split(" ")[0].strip(), " ".join(each.split(" ")[1:]).strip()
if not inner_count in ("no", "0"):
added = int(inner_count) * outer_count
total += self.count_inner_bags(inner_bag, added, added)
return total
if __name__ == "__main__":
"""python -m code2020 -d 1"""
parser = ArgumentParser()
parser.add_argument("--day", "-d", type=int, default=1)
args = parser.parse_args()
start_time = time()
try:
eval("Day"+str(args.day))()
except NameError:
print("Day Not Implemented")
time_taken = time() - start_time
print("TIME TAKEN %s sec" % time_taken)