-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-1.py
50 lines (41 loc) · 1.33 KB
/
16-1.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
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
print(fields)
print(main_ticket)
print(tickets)
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)]
invalids = []
for t in tickets:
for i in range(len(t)):
if t[i] not in valid_set:
invalids.append(t[i])
print(sum(invalids))