2024(4): fixed the first puzzle!

This commit is contained in:
2024-12-19 00:40:02 +01:00
parent c4368b8be2
commit 9d50931fe9

View File

@ -49,28 +49,62 @@ def rotate(list_2d):
return diagonal_list
def rotate_neg(list_2d):
x_max, y_max = len(list_2d), len(list_2d[0])
diagonal_list = []
# Diagonals starting from (0,0), for the first row.
for i in range(x_max):
substring = ""
x = x_max - 1 - i
y = 0
while x >= 0 and y < y_max:
#print(x, x_max, y, y_max)
substring += list_2d[x][y]
x -= 1
y += 1
diagonal_list.append(substring)
# Reverse
diagonal_list = diagonal_list[::-1]
# Diagonals starting from (0,1), for the first column.
for j in range(1, y_max):
substring = ""
x = x_max - 1
y = j
while x >= 0 and y < len(list_2d[x]):
substring += list_2d[x][y]
x -= 1
y += 1
diagonal_list.append(substring)
return diagonal_list
if __name__ == "__main__":
result = 0
with open("input", "r") as fp:
# Skip last line as it is empty
# Skip last line as it is empty.
full_string = fp.read().split("\n")[:-1]
for line in full_string:
result += scan_line(line)
result += scan_line(line[::-1])
# Transpose for vertical hits.
for line in transpose(full_string):
result += scan_line(line)
result += scan_line(line[::-1])
# Rotate 45 degrees to one side for diagonals.
diagonal_list = rotate(full_string)
for line in diagonal_list:
result += scan_line(line)
result += scan_line(line[::-1])
# TODO: Transposion after rotating is ill-defined.
for line in rotate(transpose(full_string)):
diagonal_list = rotate_neg(full_string)
# Rotate the other way round.
for line in diagonal_list:
result += scan_line(line)
result += scan_line(line[::-1])
#transposed_file =
print(f"The sum of all horizontal XMAS's equals {result}.")