diff --git a/2024/d4.py b/2024/d4.py new file mode 100644 index 0000000..98975bb --- /dev/null +++ b/2024/d4.py @@ -0,0 +1,44 @@ +from lib import get_data +from lib import Grid2D + +data = get_data(__file__) +g = Grid2D(data) + +DIRS = { + "N": (-1, 0), + "E": (0, 1), + "S": (1, 0), + "W": (0, -1), + "NE": (-1, 1), + "SE": (1, 1), + "SW": (1, -1), + "NW": (-1, -1), +} + +t1, t2 = 0, 0 +for row in range(g.n_rows): + for col in range(g.n_cols): + for d in DIRS.values(): + coords = [(row + d[0] * i, col + d[1] * i) for i in range(len("XMAS"))] + for (r, c), z in zip(coords, "XMAS"): + if not (0 <= r < g.n_rows and 0 <= c < g.n_cols and g[(r, c)] == z): + break + else: + t1 += 1 + + coords = [ + (row - 1, col - 1), + (row - 1, col + 1), + (row, col), + (row + 1, col - 1), + (row + 1, col + 1), + ] + for s in ["MMASS", "MSAMS", "SSAMM", "SMASM"]: + for (r, c), z in zip(coords, s): + if not (0 <= r < g.n_rows and 0 <= c < g.n_cols and g[(r, c)] == z): + break + else: + t2 += 1 + +print(t1) +print(t2) diff --git a/README.md b/README.md index 05a881c..3348db0 100644 --- a/README.md +++ b/README.md @@ -294,5 +294,5 @@ and focus. Of course, learning more algorithms and techniques helps. - Day 1: `00:01:30 124 0 00:02:49 141 0` - Day 2: `00:05:34 686 0 00:08:21 531 0` - Day 3: `00:01:48 165 0 00:02:40 56 45` -- Day 4: - +- Day 4: `00:07:47 1101 0 00:24:07 2410 0` +- Day 5: