-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.py
118 lines (96 loc) · 3.67 KB
/
day08.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typing import List, Dict
import unittest
from input import read_and_solve
segments_per_digit = {
0: 6,
1: 2, # unique
2: 5,
3: 5,
4: 4, # unique
5: 5,
6: 6,
7: 3, # unique
8: 7, # unique
9: 6,
}
def parse_input(lines):
result = []
for line in lines:
line = line.strip()
left, right = line.split(" | ")
left_seqs = left.split(" ")
right_seqs = right.split(" ")
result.append([left_seqs, right_seqs])
return result
def extract_right_seqs(seqs):
return list(map(lambda s: s[1], seqs))
def count_unique_digits(seqs):
occurrence = {
0: 0,
1: len(list(filter(lambda s: len(s) == segments_per_digit[1], seqs))),
2: 0,
3: 0,
4: len(list(filter(lambda s: len(s) == segments_per_digit[4], seqs))),
5: 0,
6: 0,
7: len(list(filter(lambda s: len(s) == segments_per_digit[7], seqs))),
8: len(list(filter(lambda s: len(s) == segments_per_digit[8], seqs))),
9: 0,
}
return sum(occurrence.values())
def part1(lines):
seq_pairs = parse_input(lines) # [[left_seq_1, right_seqs_1], [ls2, rs2], …]
right_seqs = extract_right_seqs(seq_pairs) # [right_seq_1, rs2, …]
acc = 0
for seq in right_seqs:
acc += count_unique_digits(seq)
return acc
def part2(lines):
return 0
sample_lines = [
"be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe",
"edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc",
"fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg",
"fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb",
"aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea",
"fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb",
"dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe",
"bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef",
"egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb",
"gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce",
]
class TestDay8(unittest.TestCase):
def test_parse_input(self):
result = parse_input(sample_lines[:1])
self.assertEqual(result,
[
[
["be", "cfbegad", "cbdgef", "fgaecd", "cgeb",
"fdcge", "agebfd", "fecdb", "fabcd", "edb"],
["fdgacbe", "cefdb", "cefbgd", "gcbe"],
]
])
def test_extract_right_seqs(self):
result = extract_right_seqs(
[
[
["be", "cfbegad", "cbdgef", "fgaecd", "cgeb",
"fdcge", "agebfd", "fecdb", "fabcd", "edb"],
["fdgacbe", "cefdb", "cefbgd", "gcbe"],
]
])
self.assertEqual(result,
[
["fdgacbe", "cefdb", "cefbgd", "gcbe"],
])
def test_count_unique_digits(self):
result = count_unique_digits(["fdgacbe", "cefdb", "cefbgd", "gcbe"])
self.assertEqual(result, 2)
# def test_part_1_sample(self):
# self.assertEqual(part1(self.sample_lines), 37)
# def test_part_2_sample(self):
# self.assertEqual(part2(self.sample_lines), 168)
if __name__ == '__main__':
unittest.main(exit=False)
print()
read_and_solve(__file__, part1, part2)