-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16-2.py
59 lines (52 loc) · 1.5 KB
/
day16-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
rules = {}
def validator(nums, rules):
res = []
for num in nums:
if not any(
[
(l1 <= num <= h1) or (l2 <= num <= h2)
for ((l1, h1), (l2, h2)) in rules.values()
]
):
res.append(num)
return res
def map(nums, rules):
for i, num in enumerate(nums):
for k, ((l1, h1), (l2, h2)) in rules.items():
if not ((l1 <= num <= h1) or (l2 <= num <= h2)):
exclude[k].append(i)
with open("day16-1.txt") as f:
rule, ticket, nearby = f.read().split("\n\n")
for line in rule.split("\n"):
parts = line.strip().split(" ")
l1, h1 = parts[-3].split("-")
l2, h2 = parts[-1].split("-")
rules[line.split(":")[0]] = ((int(l1), int(h1)), (int(l2), int(h2)))
tot = 0
valid = []
for line in nearby.split("\n")[1:]:
l = [int(x) for x in line.split(",")]
if not (c := validator(l, rules)):
valid.append(l)
for i in c:
tot += i
exclude = {x: [] for x in rules.keys()}
pos = set(range(len(rules)))
# print(len(valid))
for v in valid:
map(v, rules)
exclude = dict(sorted(exclude.items(), key=lambda x: len(x[1]), reverse=True))
print(exclude)
res = []
for k, v in exclude.items():
p = pos - set(v)
pos = set(v)
if "departure" in k:
res.append(p.pop())
ticket = [int(x) for x in ticket.split("\n")[1].split(",")]
e = 1
for i in [ticket[x] for x in res]:
e *= i
print(e)
# print(tot)
# print(rules)