-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.py
More file actions
90 lines (73 loc) · 2.05 KB
/
day08.py
File metadata and controls
90 lines (73 loc) · 2.05 KB
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
from collections import defaultdict
def parse(input):
ret = []
input = input.splitlines()
for line in input:
parts = line.split(' | ')
ret.append(
[[set(c) for c in parts[0].split()], [set(d) for d in parts[1].split()]]
)
return ret
def part1(input):
input = parse(input)
ret = 0
for _, output in input:
for i in output:
if len(i) in (2, 4, 3, 7):
ret += 1
return ret
def part2(input):
input = parse(input)
ret = 0
for notes, digits in input:
lens = defaultdict(list)
for n in notes:
lens[len(n)].append(n)
cf = lens[2][0]
bcdf = lens[4][0]
acf = lens[3][0]
abcdefg = lens[7][0]
a = acf - cf
bd = bcdf - cf
eg = abcdefg - a - bd - cf
# 2 = a cde g
# 3 = a cd fg
# 5 = ab d fg
# all contain: adg
dg = (lens[5][0] & lens[5][1] & lens[5][2]) - a
g = dg & eg
e = eg - g
d = dg - g
# 0 = abc efg
# 6 = ab defg
# 9 = abcd fg
# all contain abfg
bf = (lens[6][0] & lens[6][1] & lens[6][2]) - a - g
b = bf - cf
f = bf - b
c = cf - f
s = {
frozenset(a | b | c | f | e | g): 0,
frozenset(c | f): 1,
frozenset(a | c | d | e | g): 2,
frozenset(a | c | d | f | g): 3,
frozenset(b | c | d | f): 4,
frozenset(a | b | d | f | g): 5,
frozenset(a | b | d | f | e | g): 6,
frozenset(a | c | f): 7,
frozenset(a | b | c | d | f | e | g): 8,
frozenset(a | b | c | d | f | g): 9,
}
ds = ''
for d in digits:
ds += str(s[frozenset(d)])
ret += int(ds)
return ret
if __name__ == '__main__':
import sys
import utils
input = sys.stdin.read()
p1, t1 = utils.time(part1, input[:])
print(f'part1: {p1} ({t1:.20f})')
p2, t2 = utils.time(part2, input[:])
print(f'part2: {p2} ({t2:.20f})')