-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.krk
82 lines (67 loc) · 2.04 KB
/
day8.krk
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
#!/usr/bin/env kuroko
'''
Day 8, Part 2
Part 1 removed, mostly because I didn't keep the code around for it.
'''
## Common template, load data
import fileio, kuroko
let lines
with fileio.open(kuroko.argv[1] if len(kuroko.argv) > 1 else kuroko.argv[0].replace('.krk','.txt'),'r') as f:
lines = [line.strip() for line in f.readlines()]
def bitand(c, m):
for x in m:
if x not in c:
return False
return True
def minus(candidate,mask):
let out = []
for c in candidate:
if c not in mask:
out.append(c)
return out
let output = 0
for line in lines:
let left, right = [x.strip() for x in line.split(' | ')]
let stuff = left.split(' ')
let outs = right.split(' ')
let digits = {}
let comb = [''.join(sorted(x)) for x in stuff]
comb.extend([''.join(sorted(x)) for x in outs])
# 1, 4, 7, 8 have Unique lengths
for x in comb:
if len(x) == 2:
digits[1] = x
else if len(x) == 3:
digits[7] = x
else if len(x) == 4:
digits[4] = x
else if len(x) == 7:
digits[8] = x
# 0, 9, and 6 have 6 segments and unique overlaps against 4 and 1
for x in comb:
if len(x) == 6:
if bitand(x, digits[4]):
digits[9] = x
else if bitand(x, digits[1]):
digits[0] = x
else:
digits[6] = x
# 5 is the only 5-segment that overlaps with 6
for x in comb:
if len(x) == 5 and bitand(digits[6], x):
digits[5] = x
# 2 and 3 have different numbers of strokes that aren't on the right
for x in comb:
if len(x) == 5 and x != digits[5]:
let count = len(minus(x,digits[1]))
if count == 3:
digits[3] = x
else if count == 4:
digits[2] = x
let backwards = {}
for k,v in digits.items():
backwards[v] = str(k)
let digit = ''.join(backwards[''.join(sorted(i))] for i in outs)
output += int(digit)
# Part 2
print(output)