-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.py
89 lines (78 loc) · 2.19 KB
/
day16.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
import time
from parse import parse
def part1(input):
def inRanges(num, ranges):
for i in range(0, len(ranges), 2):
if ranges[i] <= num <= ranges[i+1]:
return True
return False
ranges=[]
for x in input[0]:
ranges += x[1:]
rez = 0
pop = []
for i, ticket in enumerate(input[2]):
for val in ticket:
if not inRanges(val, ranges):
rez += val
if i not in pop:
pop.append(i)
# print(len(pop), len(input[2]))
for i in reversed(pop):
input[2].pop(i)
return rez, input
def part2(input):
def condition(ticket, field):
for x in ticket:
for i in range(0, len(field), 2):
if not (field[0] <= x <= field[1] or field[2] <= x <= field[3]):
return False
return True
nearbyTickets = list(map(list, zip(*input[2])))
fields = [(x[0], x[1:]) for x in input[0]]
fieldsOrdered = []
for ticket in nearbyTickets:
temp = []
for field in fields:
if condition(ticket, field[1]):
temp.append(field[0])
fieldsOrdered.append(temp)
found = [x[0] for x in fieldsOrdered if len(x)==1]
for p in range(20):
for i in range(len(fieldsOrdered)):
if len(fieldsOrdered[i]) != 1:
fieldsOrdered[i] = [x for x in fieldsOrdered[i] if x not in found]
if len(fieldsOrdered[i])==1:
found.append(fieldsOrdered[i][0])
rez = 1
for i, x in enumerate(fieldsOrdered):
if "departure" in x[0]:
rez *= input[1][i]
return rez
def day16(input, day):
rez, input = part1(input)
print("Day", day, "part1: ", rez)
start = time.time()
print("Day", day, "part2: ", part2(input))
# print(time.time()-start)
if __name__ == "__main__":
from nrby import *
day = 16
test = False
# test = True
input=[]
file = "day%d/input.txt" % day
if test:
file = "test.txt"
myTicket = [113,197,59,167,151,107,79,73,109,157,199,193,83,53,89,71,149,61,67,163]
input = [[]]
for line in open(file, "r").readlines():
input[0].append(list(parse("{}: {:d}-{:d} or {:d}-{:d}", line)))
if test:
input.append([11,12,13])
input.append([[3,9,18],[15,1,5],[5,14,9]])
else:
input.append(myTicket)
input.append(nearbyTickets)
# print(input[0])
day16(input, day)