-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
61 lines (47 loc) · 1.7 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
import fileinput
import re
import numpy as np
def validity(x, field):
if (field[0] <= x <= field[1]) or (field[2] <= x <= field[3]):
return True
else:
return False
def findError(nearby):
error = 0
discard = []
t_idx = dict((i, []) for i in range(len(nearby[0])))
for i, ticket in enumerate(nearby):
assert len(ticket) == len(fields)
for j, t in enumerate(ticket):
n = 0
for k, m in enumerate(fields):
if not validity(t, m):
n += 1
t_idx[j].append(k)
if n == len(fields):
error += t
discard.append(i)
print(error)
return t_idx, discard
d = [i.split('\n') for i in "".join(fileinput.input()).split('\n\n')]
pattern = ': ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)' #([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)
fields = [[int(i) for i in re.search(pattern, i).groups()] for i in d[0]]
myticket = np.array([float(i) for i in d[1][1].split(',')])
nearby = [[int(j) for j in i.split(',')] for i in d[2][1:]]
# part 1
_, discard = findError(nearby)
nearby = [x for i, x in enumerate(nearby) if i not in discard]
# part 2
d_notin, _ = findError(nearby)
lens = np.array([(k,len(d_notin[k])) for k in d_notin])
lens = lens[np.argsort(lens[:,1])]
arr = np.arange(0, len(fields))
struct = []
mapping = []
for k in reversed(lens[:,0]):
output = [i for i in arr if i not in d_notin[k] and i not in struct]
struct.append(output)
mapping.append((k, output[0]))
mapping = np.array(mapping)
mapping = mapping[np.argsort(mapping[:,1])]
print(np.prod(myticket[mapping[:,0]][:6]))