-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-2.py
97 lines (78 loc) · 2.76 KB
/
16-2.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
import numpy as np
#Today is a little bit messy
save_tickets = False
ticket_up = False
save_fields = True
tickets = []
fields = dict()
with open("16.in", "r") as file:
for i, l in enumerate(file):
if ticket_up:
main_ticket = [int(x) for x in l.strip().split(",")]
ticket_up = False
elif save_tickets:
l = l.strip()
tickets.append([int(x) for x in l.split(",")])
elif save_fields:
l = l.strip().split(":")
if l[0] is not "":
ranges = l[1].split(" or ")
low_a = int(ranges[0].split("-")[0])
high_a = int(ranges[0].split("-")[1])
low_b = int(ranges[1].split("-")[0])
high_b = int(ranges[1].split("-")[1])
fields[l[0]] = [low_a, high_a, low_b, high_b]
else:
save_fields = False
if l == "nearby tickets:\n":
save_tickets = True
elif l == "your ticket:\n":
ticket_up = True
valid_set = set()
for k in fields.keys():
[valid_set.add(i) for i in range(fields[k][0], fields[k][1]+1)]
[valid_set.add(i) for i in range(fields[k][2], fields[k][3]+1)]
#find invalid tickets
invalid_tickets = []
for i, t in enumerate(tickets):
for j in range(len(t)):
if t[j] not in valid_set:
invalid_tickets.append(i)
#remove invalid tickets
for i in range(len(invalid_tickets)-1, -1, -1):
del tickets[invalid_tickets[i]]
possible = dict()
for k in fields.keys():
possible[k] = np.ones(len(tickets[0]))
#now going through the numbers on each ticket, remove the fields from contention if there is 1 rule break for that
#field
tickets = [main_ticket] + tickets
for i in range(len(tickets)):
for j in range(len(tickets[0])):
for key in fields.keys():
if not (fields[key][0] <= tickets[i][j] <= fields[key][1]) and not (fields[key][2] <= tickets[i][j] <= fields[key][3]):
possible[key][j] = 0
#Now check across all the fields. It there's one option thats the one it must be!
#repeat this process because some options will only become available when others are removed
for h in range(len(tickets[0])):
for i in range(len(tickets[0])):
s = 0
key = ""
for k in fields.keys():
if possible[k][i] == 1:
s += 1
key = k
if s == 1:
possible[key] = np.zeros(len(tickets[0]))
possible[key][i] = 1
#departure field mask
mask = np.zeros(len(tickets[0]))
for key in possible.keys():
if key[:9] == "departure":
mask += possible[key]
#get the numbers from the target fields of the main ticket
prod = 1
for i in range(len(main_ticket)):
if mask[i] == 1:
prod *= main_ticket[i]
print(prod)