-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.py
133 lines (119 loc) · 2.85 KB
/
13.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
import sys
from collections import defaultdict
from itertools import permutations, combinations
import functools
import itertools
import math
import re
import heapq
import time
from aoctools import dijkstra
from aoctools import get_input, submit_result
from math import *
day = 13
year = 2023
inp = get_input(day, year)
inp1 = """
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
"""
inp = inp.strip().split('\n')
patterns = []
pattern = []
for l in inp:
l = l.strip()
if l == "":
if pattern:
patterns.append(pattern)
pattern = []
else:
pattern.append(l)
patterns.append(pattern)
def get_score(pattern, old):
# horizontal
h = None
l = len(pattern)
for x in range(1, l):
found = True
for w in range(min(x,l-x+1)):
if x-w-1 < 0:
break
if x+w >= l:
break
if pattern[x-w-1] != pattern[x+w]:
found = False
break
if found:
h = 100*x
if h != old:
return h
# vertical
nc = len(pattern[0])
L = list(itertools.chain(*pattern))
pattern = [L[i::nc] for i in range(nc)]
pattern = ["".join(x) for x in pattern]
h = None
l = len(pattern)
for x in range(1, l):
found = True
for w in range(min(x, l-x+1)):
if x-w-1 < 0:
break
if x+w >= l:
break
if pattern[x-w-1] != pattern[x+w]:
found = False
break
if found:
h = x
if h != old:
return h
return None
def part_1():
s = 0
for pattern in patterns:
s += get_score(pattern, None)
return s
def part_2():
s = 0
for pattern in patterns:
oc = get_score(pattern, None)
pcopy = [[x for x in p] for p in pattern]
done = False
m = set()
for i, l in enumerate(pattern):
for j, v in enumerate(l):
orig = pcopy[i][j]
if orig == '#':
pcopy[i][j] = '.'
else:
pcopy[i][j] = '#'
pat = ["".join(x) for x in pcopy]
v = get_score(pat, oc)
pcopy[i][j] = orig
if v and v != oc:
m.add(v)
done = True
break
if done:
break
s += m.pop()
return s
result_1 = part_1()
result_2 = part_2()
print("Part 1:", result_1)
print("Part 2:", result_2)
#submit_result(day, year, 1, result_1)
#submit_result(day, year, 2, result_2)