From 04c1a5259a17fb6530477f731c57f2fffd94515c Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Wed, 18 Dec 2024 23:53:19 +0100 Subject: [PATCH] 2024(4): getting there, but not yet --- 2024/4/puzzle1.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/2024/4/puzzle1.py b/2024/4/puzzle1.py index 3735e62..384a0a4 100644 --- a/2024/4/puzzle1.py +++ b/2024/4/puzzle1.py @@ -1,3 +1,5 @@ +import itertools + def scan_line(line): to_find = "XMAS" @@ -5,9 +7,19 @@ def scan_line(line): return line.count(to_find) def transpose(list_2d): - return list(zip(*list_2d)) + #return list(zip(*list_2d)) + #return list(map(list, zip(*list_2d))) + #return list(map(list, itertools.zip_longest(*list_2d, fillvalue=None))) + outlist = [] + for j in range(len(list_2d[0])): + outlist.append([]) + for i in range(len(list_2d)): + outlist[j].append(list_2d[i][j]) + outlist[j] = "".join(outlist[j]) + return outlist def rotate(list_2d): + #print(list_2d) x_max, y_max = len(list_2d), len(list_2d[0]) diagonal_list = [] @@ -16,21 +28,22 @@ def rotate(list_2d): substring = "" x = i y = 0 - #while x < x_max and y < y_max: - # TODO: Snapperniksvan -- Kees - while x < x_max and y < len(list_2d[x]): - print(x, x_max, y, y_max) + while x < x_max 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 = 0 y = j - while x < x_max and y < y_max: + while x < x_max and y < len(list_2d[x]): substring += list_2d[x][y] x += 1 y += 1 @@ -42,7 +55,8 @@ if __name__ == "__main__": result = 0 with open("input", "r") as fp: - full_string = fp.read().split("\n") + # 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]) @@ -54,7 +68,8 @@ if __name__ == "__main__": for line in full_string: result += scan_line(line) result += scan_line(line[::-1]) - for line in transpose(full_string): + print(result) + for line in rotate(transpose(full_string)): result += scan_line(line) result += scan_line(line[::-1])