-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
80 lines (70 loc) · 2.02 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
"""
cadybaltz
12/13/2023
AoC 2023 Day 13
Part 1: 1853, 00:27:11
Part 2: 890, 00:28:41
"""
import sys
def has_vert(graph, part):
for x in range(0, len(graph[0])):
diff = 0
same = True
iter = False
for y in range(0, len(graph)):
if x - y >= 0 and x + y + 1 < len(graph[0]):
for z in range(len(graph)):
iter = True
if graph[z][x - y] != graph[z][x + y + 1]:
diff += 1
same = False
if part == 2 and diff == 1:
return x
elif part == 1 and same and iter:
return x
return None
def has_hor(graph, part):
for x in range(0, len(graph)):
diff = 0
same = True
iter = False
for y in range(0, len(graph)):
if x-y >= 0 and x+y+1 < len(graph):
for z in range(len(graph[0])):
iter = True
if graph[x - y][z] != graph[x + y + 1][z]:
diff += 1
same = False
if part == 2 and diff == 1:
return x
elif part == 1 and same and iter:
return x
return None
def solution(lines, part):
result = 0
patterns = []
curr = []
for x in range(len(lines)):
line = lines[x].strip()
if len(line) == 0:
patterns.append(curr)
curr = []
else:
curr.append(line)
patterns.append(curr)
for pattern in patterns:
vert = has_vert(pattern, part)
hor = has_hor(pattern, part)
if vert is not None:
result += vert + 1
else:
result += (hor + 1) * 100
return result
if __name__ == '__main__':
if sys.argv[1] == 't':
input = open("test.txt", "r")
else:
input = open("input.txt", "r")
lines = input.readlines()
print("Part 1: " + str(solution(lines, 1)))
print("Part 2: " + str(solution(lines, 2)))