Add 2023 solutions
This commit is contained in:
92
2023/d13.py
Normal file
92
2023/d13.py
Normal file
@@ -0,0 +1,92 @@
|
||||
from lib import *
|
||||
|
||||
EXAMPLE = """
|
||||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
||||
""".replace("\n", "", count=1)
|
||||
|
||||
|
||||
def solve(i: Input, second=False):
|
||||
res = 0
|
||||
ps = i.paras()
|
||||
|
||||
for p in ps:
|
||||
g = Input(p).grid2()
|
||||
|
||||
row_res, col_res = [], []
|
||||
rows = g.rows()
|
||||
cols = g.cols()
|
||||
for i_row in range(len(rows) - 1):
|
||||
for i in range(min(i_row + 1, len(rows) - i_row - 1)):
|
||||
if rows[i_row - i] != rows[i_row + i + 1]:
|
||||
break
|
||||
else:
|
||||
if not second:
|
||||
res += (i_row + 1) * 100
|
||||
row_res.append(i_row)
|
||||
|
||||
for i_col in range(len(cols) - 1):
|
||||
for i in range(min(i_col + 1, len(cols) - i_col - 1)):
|
||||
if cols[i_col - i] != cols[i_col + i + 1]:
|
||||
break
|
||||
else:
|
||||
if not second:
|
||||
res += (i_col + 1)
|
||||
col_res.append(i_col)
|
||||
|
||||
if not second:
|
||||
continue
|
||||
|
||||
for c in g.all_coords():
|
||||
g[c] = '.' if g[c] == '#' else '#'
|
||||
rows = g.rows()
|
||||
cols = g.cols()
|
||||
for i_row in range(len(rows) - 1):
|
||||
for i in range(min(i_row + 1, len(rows) - i_row - 1)):
|
||||
if rows[i_row - i] != rows[i_row + i + 1]:
|
||||
break
|
||||
else:
|
||||
if not i_row in row_res:
|
||||
res += (i_row + 1) * 100
|
||||
row_res.append(i_row)
|
||||
|
||||
for i_col in range(len(cols) - 1):
|
||||
for i in range(min(i_col + 1, len(cols) - i_col - 1)):
|
||||
if cols[i_col - i] != cols[i_col + i + 1]:
|
||||
break
|
||||
else:
|
||||
if not i_col in col_res:
|
||||
res += (i_col + 1)
|
||||
col_res.append(i_col)
|
||||
g[c] = '.' if g[c] == '#' else '#'
|
||||
if len(row_res + col_res) != 2:
|
||||
raise Exception("Unexpected amount of mirrors!!")
|
||||
return res
|
||||
|
||||
|
||||
def main():
|
||||
DAY_INPUT = "d13.txt"
|
||||
|
||||
print("Example 1:", solve(Input(EXAMPLE)))
|
||||
print("Solution 1:", solve(Input(DAY_INPUT)))
|
||||
assert 29846 == solve(Input(DAY_INPUT))
|
||||
|
||||
print("Example 2:", solve(Input(EXAMPLE), True))
|
||||
print("Solution 2:", solve(Input(DAY_INPUT), True))
|
||||
assert 25401 == solve(Input(DAY_INPUT), True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user