-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16_part2.py
73 lines (60 loc) · 1.96 KB
/
Day16_part2.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
import functools
f = open("Inputs/Day16_Tickets.txt", "r")
ticketvals = []
for x in f:
vals = x.rstrip().split(',')
ticketvals.append([int(v) for v in vals])
f = open("Inputs/Day16_rules.txt", "r")
fields = []
for x in f:
rule_data = x.rstrip().split(':')
rule = { 'field': rule_data[0] }
ranges = rule_data[1].split(' or ')
rule['ranges'] = []
for r in ranges:
numbers = r.split('-')
rule_range = int(numbers[0]), int(numbers[1])
rule['ranges'].append(rule_range)
fields.append(rule)
f = open("Inputs/Day16_My_ticket.txt", "r")
my_ticket = []
for x in f:
my_ticket = [int(v) for v in x.split(',')]
def valid_number(num, ranges):
for (min_val, max_val) in ranges:
if min_val <= num and num <= max_val:
return True
return False
valid_tickets = []
for ticket in ticketvals:
valid_ticket = True
for value in ticket:
valid_value = False
for field in fields:
if valid_number(value, field['ranges']):
valid_value = True
break
if not valid_value:
valid_ticket = False
break
if (valid_ticket):
valid_tickets.append(ticket)
for field in fields:
field['possible_index'] = list(range(len(fields)))
field['index'] = -1
for ticket in valid_tickets:
for i in range(len(ticket)):
value = ticket[i]
for field in fields:
if not valid_number(value, field['ranges']):
if i in field['possible_index']:
field['possible_index'].remove(i)
fields_sorted = sorted(fields, key=lambda x: len(x['possible_index']))
for f in fields_sorted:
candidates = [x for x in f['possible_index'] if x not in [f['index'] for f in fields]]
f['index'] = candidates[0]
dep_vals = []
for f in fields:
if f['field'].startswith('departure'):
dep_vals.append(my_ticket[f['index']])
print(functools.reduce(lambda a, b: a * b, dep_vals))