-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.py
73 lines (59 loc) · 2.21 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
import re
with open("input/day16", "r") as input:
data = input.read().split("\n\n")
fields = {}
for entry in data[0].split("\n"):
m = re.match(r"(.+): (\d+)-(\d+) or (\d+)-(\d+)", entry)
fields[m[1]] = (int(m[2]), int(m[3]), int(m[4]), int(m[5]))
ownTicket = [int(x) for x in data[1].split("\n")[1].split(",")]
nearbyTickets = []
for entry in data[2].split("\n"):
if entry == "nearby tickets:" or entry == "":
continue
nearbyTickets.append([int(x) for x in entry.split(",")])
def isValueValid(val):
for constraints in fields.values():
if constraints[0] <= val <= constraints[1] or constraints[2] <= val <= constraints[3]:
return True
return False
validTickets = [ownTicket]
def part1():
invalid = 0
for t in nearbyTickets:
isTicketValid = True
for val in t:
if not isValueValid(val):
invalid += val
isTicketValid = False
if isTicketValid:
validTickets.append(t)
return invalid
def removePossibleKey(possibleKeys, idx, keyToRemove):
possibleKeys[idx].remove(keyToRemove)
if len(possibleKeys[idx]) != 1:
return
keyToRemove = list(possibleKeys[idx])[0]
for idx2 in range(len(possibleKeys)):
if idx2 == idx or keyToRemove not in possibleKeys[idx2]:
continue
removePossibleKey(possibleKeys, idx2, keyToRemove)
def part2():
numFields = len(ownTicket)
possibleKeys = [set(fields.keys()) for _ in range(numFields)]
for t in validTickets:
for idx in range(numFields):
keysToRemove = []
for key in possibleKeys[idx]:
constraints = fields[key]
if not constraints[0] <= t[idx] <= constraints[1] and not constraints[2] <= t[idx] <= constraints[3]:
keysToRemove.append(key)
for key in keysToRemove:
removePossibleKey(possibleKeys, idx, key)
possibleKeys = [list(keys)[0] for keys in possibleKeys]
result = 1
for idx in range(numFields):
if (re.match(r"^departure", possibleKeys[idx])):
result *= ownTicket[idx]
return result
print("Part 1: " + str(part1()))
print("Part 2: " + str(part2()))