-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.py
100 lines (70 loc) · 2.71 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
90
91
92
93
94
95
96
97
98
99
100
from common_py import input_access
import re
input = input_access.fetch_input(16).split("\n\n")
class Rule:
def __init__(self, inputDict):
self.name = inputDict["fieldName"]
self.lowerFirst = int(inputDict["lowerFirst"])
self.upperFirst = int(inputDict["upperFirst"])
self.lowerSecond = int(inputDict["lowerSecond"])
self.upperSecond = int(inputDict["upperSecond"])
self.indices = []
def print(self):
print(str(self.indices) + " " + self.name)
def is_applicable(self, value):
return (value >= self.lowerFirst and value <= self.upperFirst) or (value >= self.lowerSecond and value <= self.upperSecond)
def find_own_index(self, tickets):
index = 0
while index < len(tickets[0]):
everythingChecks = True
for i in range(len(tickets)):
if not self.is_applicable(tickets[i][index]):
everythingChecks = False
if everythingChecks:
self.indices.append(index + 1)
index = index + 1
def parseTicketToList(ticketString):
return list(map(lambda item: int(item), ticketString.split(",")))
regex = re.compile("(?P<fieldName>.*): (?P<lowerFirst>[0-9]+)-(?P<upperFirst>[0-9]+) or (?P<lowerSecond>[0-9]+)-(?P<upperSecond>[0-9]+)")
rules = list(map(lambda line : Rule(regex.match(line).groupdict()), input[0].splitlines()))
nearbyTickets = list(input[2].splitlines()[1:])
nearbyTickets = list(map(lambda ticketString: parseTicketToList(ticketString), nearbyTickets))
# print(nearbyTickets)
validTickets = []
invalidTicketSum = 0
for i in range(len(nearbyTickets)):
isValid = True
for j in range(len(nearbyTickets[i])):
isAccepted = False
for k in range(len(rules)):
if rules[k].is_applicable(nearbyTickets[i][j]):
isAccepted = True
if not isAccepted:
invalidTicketSum = invalidTicketSum + nearbyTickets[i][j]
isValid = False
if isValid:
validTickets.append(nearbyTickets[i])
print("part1:" , invalidTicketSum)
myticket = parseTicketToList(input[1].splitlines()[1])
print(myticket)
validTickets.append(myticket)
print(len(validTickets))
foundIndices = []
for rule in rules:
rule.find_own_index(validTickets)
rule.print()
foundIndices = []
while len(rules) != 0:
exactRule = list(filter(lambda rule: len(rule.indices) == 1, rules))[0]
# exactRule.print()
foundIndices.append(exactRule)
rules.remove(exactRule)
for rule in rules:
rule.indices.remove(exactRule.indices[0])
departureRules = filter(lambda rule: "departure" in rule.name, foundIndices)
departureRuleIndices = map(lambda rule: rule.indices[0], departureRules)
product = 1
for index in departureRuleIndices:
product = myticket[index - 1] * product
print(product)
# print(list(map(lambda rule: rule.print(), rules)))