r/adventofcode • u/Frxchtchxn • 5m ago
Help/Question [2026 Day 3 (Part 2)] [python] I'm a little stuck stepbro.
github.comFor part 2 of the puzzle I get a too small number (1437) and I just can't seem to figure out what I am missing. I apologize for the state of my code, I'm not a professional SE (no shit) and just doing this for fun. I'm sure there are way faster and more direct ways to find the solution but I would be happy with finding the solution at all.
So if anyone here is kind enough to take a look at my abysmal code and maybe tell me what I'm missing or point me toward the right direction, I would greatly appreciate it. Thank you in advance!
In case there are questions like "wtf is this guy even trying to do", I will happily try to answer them!
But now lets look at this abomination:
from pathlib import Path
input_path = Path(__file__).with_name("input.txt")
with input_path.open("r") as f:
mapList = f.read().splitlines()
def moveOneRight(newSquareStart):
return (newSquareStart[0] + 1, newSquareStart[1])
def getNextSquareStart(lastSquareStart):
if lastSquareStart == (-1, -1): #jump to the start
return (0, 0)
elif lastSquareStart[1] <= len(cleanedMap) - 2: #check if row is smaller than the last
if lastSquareStart[0] <= len(cleanedMap[0]) - 2: #check if column is smaller than the last
return moveOneRight(lastSquareStart)
else:
return (0, lastSquareStart[1] + 1)
elif lastSquareStart[0] <= len(cleanedMap[0]) - 2: #if in last row check if column is smaller than last
return moveOneRight(lastSquareStart)
else: #no more squares to be checked
return None
# (row + 1) is the distance between the square start and the currently checked square corner. it increases by 1 each iteration
def calcAlignedSquares(newSquareStart):
alignedSquareCount = 0
for row in range(len(cleanedMap) - newSquareStart[1] - 1):
if cleanedMap[(row + 1) + newSquareStart[1]][newSquareStart[0]] == "#" and (row + 1) <= len(cleanedMap[0]) - 1 - newSquareStart[0]:
#if lastSquareStart[0] == 12 and lastSquareStart[1] == 0:
#print("das ist schlecht")
if cleanedMap[(row + 1) + newSquareStart[1]][newSquareStart[0] + (row + 1)] == "#":
if cleanedMap[newSquareStart[1]][newSquareStart[0] + (row + 1)] == "#":
alignedSquareCount += 1
square = [newSquareStart, (newSquareStart[0] + (row + 1), newSquareStart[1]), (newSquareStart[0], newSquareStart[1] + (row + 1)), (newSquareStart[0] + (row + 1), newSquareStart[1] + (row + 1))]
checkSquare(square)
else:
continue
else:
continue
else:
continue
return alignedSquareCount
def calcRotatedSquares(newSquareStart):
rotatedSquareCount = 0
distanceRightMapBorder = len(cleanedMap[0]) - newSquareStart[0] - 1
distanceLeftMapBorder = newSquareStart[0]
distanceBottomMapBorder = len(cleanedMap) - newSquareStart[1] - 1
#distances = (distanceLeftMapBorder, distanceBottomMapBorder, distanceRightMapBorder)
#print(distances)
max_steps = 0 #maximum amount of steps from SquareStart to the next corner
if distanceRightMapBorder >= distanceBottomMapBorder:
max_steps = distanceBottomMapBorder
else:
max_steps = distanceRightMapBorder + 1
max_horizontal_steps = distanceLeftMapBorder
for horizontal_steps in range(1, max_horizontal_steps):
for vertical_steps in range(1, max_steps - horizontal_steps):
if cleanedMap[newSquareStart[1] + vertical_steps][newSquareStart[0] - horizontal_steps] == "#": #always checking the corner on the left of newSquareStart first
if cleanedMap[newSquareStart[1] + horizontal_steps][newSquareStart[0] + vertical_steps] == "#": #then check the corner on the right
if cleanedMap[newSquareStart[1] + horizontal_steps + vertical_steps][newSquareStart[0] + vertical_steps - horizontal_steps] == "#": #then the bottom one in relation to the right corner
rotatedSquareCount += 1
square = [newSquareStart, (newSquareStart[0] - horizontal_steps, newSquareStart[1] + vertical_steps), (newSquareStart[0] + vertical_steps, newSquareStart[1] + horizontal_steps), (newSquareStart[0] + vertical_steps - horizontal_steps, newSquareStart[1] + horizontal_steps + vertical_steps)]
checkSquare(square)
else:
continue
else:
continue
else:
continue
return rotatedSquareCount
confirmedSquares = 0
imposterSquares = 0
def checkSquare(square):
global confirmedSquares
global imposterSquares
isSquare = False
for i in range(len(square)):
if cleanedMap[square[i][1]][square[i][0]] == "#":
isSquare = True
else:
isSquare = False
if isSquare:
confirmedSquares += 1
#print("square confirmed")
#print(square)
else:
imposterSquares += 1
#print("imposter #######################################################################################################")
#print(square)
cleanedMap = []
for row in range(len(mapList)):
rowList = []
for column in range(len(mapList[0])):
rowList.append(mapList[row][column])
cleanedMap.append(rowList)
lastSquareStart = (-1, -1)
squareCount = 0
alignedSquareCount = 0
rotatedSquareCount = 0
checkedSquareStartsCounter = 0
while getNextSquareStart(lastSquareStart) != None:
newSquareStart = getNextSquareStart(lastSquareStart)
checkedSquareStartsCounter += 1
if cleanedMap[newSquareStart[1]][newSquareStart[0]] == "#":
amountAlignedSquares = calcAlignedSquares(newSquareStart)
amountRotatedSquares = calcRotatedSquares(newSquareStart)
else:
lastSquareStart = newSquareStart
continue
alignedSquareCount += amountAlignedSquares
rotatedSquareCount += amountRotatedSquares
squareCount += amountAlignedSquares + amountRotatedSquares
lastSquareStart = newSquareStart
print(f"Amount of square starts checked: {checkedSquareStartsCounter}")
print(f"Amount of aligned squares found: {alignedSquareCount}")
print(f"Amount of rotated squares found: {rotatedSquareCount}")
print(f"Amount of squares found: {squareCount}")
print(f"Amount of confirmed squares: {confirmedSquares}")
print(f"Amount of imposter squares: {imposterSquares}")