-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.py
executable file
·143 lines (124 loc) · 3.43 KB
/
solve.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
#!/bin/env python3
"""
.zzzz.
y....w
y....w
.xxxx.
v....t
v....t
.uuuu.
0: tuvw yz group 6
1: t w group 2 *
2: uvwx z group 5
3: tu wx z group 5
4: t wxy group 4 *
5: tu xyz group 5
6: tuv xyz group 6
7: t w z group 3 *
8: tuvwxyz group 7 *
9: tu wxyz group 6
"""
with open('input') as f:
'''each input line looks like:
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf\n'''
inputs = [[[''.join(sorted(word))
for word in half.split()]
for half in line.strip().split(' | ')]
for line in f.readlines()]
def solve_a(inputs):
counter = 0
for line in inputs:
for display in line[1]:
if len(display) in (2, 3, 4, 7):
counter += 1
return counter
def find_zero(line):
six = find_six(line)
four = find_four(line)
for piece in line:
if len(piece) == 6:
if piece != six:
for letter in four:
if letter not in piece:
return piece
def find_one(line):
for piece in line:
if len(piece) == 2:
return piece
def find_two(line):
three = find_three(line)
six = find_six(line)
for piece in line:
if len(piece) == 5 and piece != three:
differences = 0
for letter in six:
if letter not in piece:
differences += 1
if differences == 2:
return piece
def find_three(line):
one = find_one(line)
for piece in line:
if len(piece) == 5:
for letter in one:
if letter not in piece:
break
else:
return piece
def find_four(line):
for piece in line:
if len(piece) == 4:
return piece
def find_five(line):
three = find_three(line)
six = find_six(line)
for piece in line:
if len(piece) == 5:
differences = 0
for letter in six:
if letter not in piece:
differences += 1
if differences == 1:
return piece
def find_six(line):
one = find_one(line)
for piece in line:
if len(piece) == 6:
for letter in one:
if letter not in piece:
return piece
def find_seven(line):
for piece in line:
if len(piece) == 3:
return piece
def find_eight(line):
for piece in line:
if len(piece) == 7:
return piece
def find_nine(line):
zero = find_zero(line)
six = find_six(line)
for piece in line:
if len(piece) == 6 and piece != zero and piece != six:
return piece
def get_four_digit_number(line):
zero = find_zero(line[0])
one = find_one(line[0])
two = find_two(line[0])
three = find_three(line[0])
four = find_four(line[0])
five = find_five(line[0])
six = find_six(line[0])
seven = find_seven(line[0])
eight = find_eight(line[0])
nine = find_nine(line[0])
numbers = [zero, one, two, three, four, five, six, seven, eight, nine]
answer = 1000*numbers.index(line[1][0])
answer += 100*numbers.index(line[1][1])
answer += 10*numbers.index(line[1][2])
answer += numbers.index(line[1][3])
return answer
def solve_b(lines):
return sum([get_four_digit_number(line) for line in lines])
print(f"Part 1: {solve_a(inputs)}")
print(f"Part 2: {solve_b(inputs)}")