-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
76 lines (73 loc) · 2.04 KB
/
day8.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
signals = open('day8input.txt').read().splitlines()
'''
The letter 8:
1
2 3
4
5 6
7
'''
def getNumberFromWord(word, dictionary):
if len(word) == 2:
return 1
if len(word) == 3:
return 7
if len(word) == 4:
return 4
if len(word) == 7:
return 8
if len(word) == 5:
# Letter 3 = All letters of 7 are in 3 so and 3 is the only length 5 word to have it
hasAll = all([char in word for char in dictionary[7]])
if hasAll:
return 3
# if it intersects letter 4 at 2 places then its 2
elif len(set(word) & set(dictionary[4])) == 2:
return 2
else:
return 5
elif len(word) == 6: #either 6 or 9
# 9 has all the characters of 4
hasAll = all([char in word for char in dictionary[4]])
# if only intersects with 1 at one place only
if len(set(word) & set(dictionary[1])) == 1:
return 6
elif hasAll:
return 9
else:
return 0
'''
fdgacbe cefdb cefbgd gcbe: 8394 c
fcgedb cgb dgebacf gc: 9781 c
cg cg fdcagb cbg: 1197 c
efabcd cedba gadfec cb: 9361 c
gecf egdcabf bgf bfgea: 4873 c
gebdcfa ecba ca fadegcb: 8418 c
cefg dcbef fcge gbcadfe: 4548 c
ed bcgafe cdgba cbgef: 1625 x 1952
gbdfcae bgc cg cgb: 8717 c
fgae cfgab fg bagce: 4315 x 4312
'''
count = 0
total = 0
for signal in signals:
decoded, code = signal.split(' | ')
wordDictionary = {}
# First make this dictionary because we are making other words from it
for word in decoded.split():
if len(word) == 2:
wordDictionary[1] = word
if len(word) == 3:
wordDictionary[7] = word
if len(word) == 4:
wordDictionary[4] = word
if len(word) == 7:
wordDictionary[8] = word
curTotal = 0
for word in code.split():
curTotal = curTotal * 10 + getNumberFromWord(word, wordDictionary)
if len(word) in [2,3,4,7]:
count += 1
total += curTotal
print("Part 1:", count)
print("Part 2:", total)