-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
177 lines (129 loc) · 3.83 KB
/
day8.py
File metadata and controls
177 lines (129 loc) · 3.83 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import adventutils
def part1(file):
print("Part 1")
filecontents = adventutils.file_contents(file)
displays = getdisplays(filecontents)
outputs = [display[1] for display in displays]
print(scanoutput(outputs))
def part2(file):
print("Part 2")
filecontents = adventutils.file_contents(file)
displays = getdisplays(filecontents)
total = 0
for display in displays:
mapping = getmapping(display[0])
total += int(displayvalue(mapping, display[1]))
print(total)
def scanoutput(output):
count = 0
for line in output:
for num in line:
if isunique(num):
count += 1
return count
def isunique(num):
return len(num) in [2, 3, 4, 7]
def getdisplays(filecontents):
displays = list()
for line in filecontents:
splitline = line.split(" | ")
displays.append((splitline[0].split(" "), splitline[1].split(" ")))
return displays
def getmapping(inputs):
mapping = dict()
mapping[1] = get1(inputs)
mapping[4] = get4(inputs)
mapping[7] = get7(inputs)
mapping[8] = get8(inputs)
mapping["a"] = geta(mapping[7], mapping[1])
mapping[9] = get9(mapping[4], mapping["a"], inputs)
mapping[3] = get3(mapping[9], mapping[1], inputs)
mapping[5] = get5(mapping[9], mapping[3], inputs)
mapping[2] = get2(mapping[5], mapping[3], inputs)
mapping["c"] = getc(mapping[9], mapping[5])
mapping[6] = get6(mapping["c"], inputs)
mapping[0] = get0(mapping[6], mapping[9], inputs)
return mapping
def get1(inputs):
for num in inputs:
if len(num) == 2:
return sort(num)
def get4(inputs):
for num in inputs:
if len(num) == 4:
return sort(num)
def get7(inputs):
for num in inputs:
if len(num) == 3:
return sort(num)
def get8(inputs):
for num in inputs:
if len(num) == 7:
return sort(num)
def geta(map7, map1):
for char in map7:
if char not in map1:
return [char]
def get9(map4, mapa, inputs):
include = map4 + mapa
for num in inputs:
if len(num) == 6 and valid9(include, num):
return sort(num)
def valid9(include, num):
matches = 0
for char in num:
if char in include:
matches += 1
return matches == len(include)
def get5(map9, map3, inputs):
for num in inputs:
if len(num) == 5 and valid5(map9, map3, num):
return sort(num)
def valid5(map9, map3, num):
for char in num:
if char not in map9:
return False
return sort(num) != map3
def get3(map9, map1, inputs):
for num in inputs:
if len(num) == 5 and valid3(map9, map1, num):
return sort(num)
def valid3(map9, map1, num):
for char in num:
if char not in map9:
return False
for char in map1:
if char not in num:
return False
return True
def getc(map9, map5):
for char in map9:
if char not in map5:
return [char]
def get2(map5, map3, inputs):
for num in inputs:
if len(num) == 5 and sort(num) not in [map5, map3]:
return sort(num)
def get6(mapc, inputs):
for num in inputs:
if len(num) == 6 and mapc[0] not in sort(num):
return sort(num)
def get0(map6, map9, inputs):
for num in inputs:
if len(num) == 6 and sort(num) not in [map6, map9]:
return sort(num)
def displayvalue(mapping, output):
value = ""
for num in output:
for key in mapping.keys():
if sort(num) == mapping[key]:
value += str(key)
break
return value
def sort(numstr):
sorted = [char for char in numstr]
sorted.sort()
return sorted
if __name__ == "__main__":
part1("./data/day8part1.txt")
part2("./data/day8part1.txt")