Advent of Code 2026 day 3
Part 1 was simple enough. Dealing with uniqueness in part 2 is less trivial, leading to an incredibly ugly solution. OOP is OTT.
with open("3.txt") as f:
graph = f.read().splitlines()
ROWS = len(graph)
COLS = len(graph[0])
squares = 0
for row in range(ROWS):
for col in range(COLS):
if graph[row][col] == "#":
for i in range(1, ROWS-row):
if (
col+i < COLS and
graph[row+i][col] == "#" and
graph[row+i][col+i] == "#" and
graph[row][col+i] == "#"
):
squares += 1
print(squares)
from itertools import product
squares = set()
hashes = set(
(row, col)
for row, col in product(range(ROWS), range(COLS))
if graph[row][col] == "#"
)
class Square:
"""let's over-engineer"""
def __init__(self, row, col, i, j):
self.points = sorted([
(row, col),
(row+i, col+j),
(row+j, col-i),
(row+i+j, col-i+j)
])
def __eq__(self, other):
return self.__hash__() == other.__hash__()
def __hash__(self):
"""need int, so massively over-engineer"""
return (
2**self.points[0][0] *
3**self.points[0][1] *
5**self.points[1][0] *
7**self.points[1][1] *
11**self.points[2][0] *
13**self.points[2][1] *
17**self.points[3][0] *
19**self.points[3][1]
)
for row, col in hashes:
for i in range(0, ROWS-row):
for j in range(0, COLS-col):
if (
sum([i, j]) > 0 and
col-i >= 0 and
row+j < ROWS and
row+i+j < ROWS and
col-i+j < COLS and
(row+i, col+j) in hashes and
(row+j, col-i) in hashes and
(row+i+j, col-i+j) in hashes
):
square = Square(row, col, i, j)
squares.add(square)
print(len(squares))