-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_8.py
145 lines (121 loc) · 4.01 KB
/
day_8.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import input_8
from aoc import advent_of_code
from collections import defaultdict, Counter
def part_one(input):
count = 0
for _, output_values in input:
lengths = map(len, output_values)
count += len(list(filter(lambda x: x in [2, 3, 4, 7], lengths)))
return count
def flatten(list):
return [item for sublist in list for item in sublist]
def part_two(input):
count = 0
for signal_patterns, output_values in input:
symbols_by_length = defaultdict(list)
for pattern in signal_patterns:
symbols_by_length[len(pattern)].append(set(pattern))
t = LettersAndPositions(symbols_by_length)
# Perform sodoku given what we know
t.is_definitely("top", t.letters[7] - t.letters[1])
t.is_the_most_common_within(
"left_upper",
t.letters[4] - t.letters[1], # -> left_upper, mid
t.letters["0,6,9"], # mid is not in 0
)
t.is_definitely(
"mid",
t.letters[4] - t.letters[1] - set(t.at["left_upper"]),
)
t.is_the_most_common_within(
"right_lower",
t.letters[1], # -> right_upper, right_lower
t.letters["0,6,9"], # right_upper is not in 6
)
t.is_definitely(
"right_upper",
t.letters[1] - set(t.at["right_lower"]),
)
t.is_the_most_common_within(
"bottom",
t.letters[8] - t.letters[7] - t.letters[4], # -> left_lower, bottom
t.letters["0,6,9"], # left_lower is not in 9
)
t.is_definitely(
"left_lower",
t.letters[8] - t.letters[7] - t.letters[4] - {t.at["bottom"]},
)
# Figure out the six unknown numbers now we have the pattern
t.letters[0] = next(filter(lambda x: t.at["mid"] not in x, t.letters["0,6,9"]))
t.letters[6] = next(
filter(lambda x: t.at["right_upper"] not in x, t.letters["0,6,9"])
)
t.letters[9] = next(
filter(lambda x: t.at["left_lower"] not in x, t.letters["0,6,9"])
)
t.letters[2] = next(
filter(
lambda x: t.at["right_upper"] in x and t.at["left_lower"] in x,
t.letters["2,3,5"],
)
)
t.letters[3] = next(
filter(
lambda x: t.at["right_upper"] in x and t.at["right_lower"] in x,
t.letters["2,3,5"],
)
)
t.letters[5] = next(
filter(
lambda x: t.at["left_upper"] in x and t.at["right_lower"] in x,
t.letters["2,3,5"],
)
)
digits = ""
for value in output_values:
digits += t.output_to_digit(value)
count += int(digits)
return count
class LettersAndPositions:
def __init__(self, symbols_by_length):
self.letters = {
1: symbols_by_length[2][0],
4: symbols_by_length[4][0],
7: symbols_by_length[3][0],
8: symbols_by_length[7][0],
"0,6,9": symbols_by_length[6],
"2,3,5": symbols_by_length[5],
}
self.at = {}
def is_definitely(self, position, letter):
self.at[position] = list(letter)[0]
def is_the_most_common_within(self, position, possible, letters):
always_present = Counter(
filter(lambda x: x in possible, flatten(letters))
).most_common(1)
self.is_definitely(position, always_present[0])
def output_to_digit(self, value):
for number, letters in self.letters.items():
if set(value) == letters:
return f"{number}"
return ""
advent_of_code(
{
"day": 8,
"part": 1,
"fn": part_one,
"sample": input_8.sample(),
"expected": 26,
"real": input_8.real(),
}
)
advent_of_code(
{
"day": 8,
"part": 2,
"fn": part_two,
"sample": input_8.sample(),
"expected": 61229,
"real": input_8.real(),
}
)