-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
94 lines (75 loc) · 1.82 KB
/
day13.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
import unittest
import re
def parseInput(input):
result = []
for str in re.split(r'\r?\n\r?\n', input):
lines = re.split(r'\r?\n', str.strip())
lines_rev = ['' for i in range(0, len(lines[0]))]
for y in range(0, len(lines)):
for x in range(0, len(lines_rev)):
lines_rev[x] += lines[y][x]
result.append((lines, lines_rev))
return result
def diff(a, b):
return sum(1 for i in range(0, len(a)) if a[i] != b[i])
def findReflection(lines, goal):
for sp in range(1, len(lines)):
diffs = 0
for i in range(0, sp):
mirror = sp + sp - i - 1
if mirror < len(lines) and lines[i] != lines[mirror]:
diffs += diff(lines[i], lines[mirror])
if goal == diffs:
return sp
return None
def parse(input, goal):
data = parseInput(input)
result = 0
for lines, lines_rev in data:
res = findReflection(lines, goal)
if res != None:
result += 100 * res
res = findReflection(lines_rev, goal)
if res != None:
result += res
return result
def parse1(input):
return parse(input, 0)
def parse2(input):
return parse(input, 1)
class Test(unittest.TestCase):
def test_part1(self):
self.assertEqual(parse1("""#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#"""), 405)
def test_part2(self):
self.assertEqual(parse2("""#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#"""), 400)
unittest.main(exit=False)
with open('input13.txt') as file:
data = file.read()
print("Part 1:", parse1(data))
print("Part 2:", parse2(data))