aoc-all

My solutions to all Advent of Code
git clone https://git.instinctive.eu/aoc-all.git
Log | Files | Refs | README | LICENSE

commit a717a4d451ab79253b1f501247cfccc7756adb0a
parent ef3b58cb5b357dae05a05cc6964d55f6ffbb2cb8
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Wed,  4 Dec 2024 20:35:37 +0000

Day 4 reference and solutions
Diffstat:
A2024/day04.nim | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2024/ref/day04a.txt | 5+++++
A2024/ref/day04b.txt | 10++++++++++
A2024/ref/day04c.txt | 10++++++++++
4 files changed, 105 insertions(+), 0 deletions(-)

diff --git a/2024/day04.nim b/2024/day04.nim @@ -0,0 +1,80 @@ +# Copyright (c) 2024, Natacha Porté +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +from std/sequtils import toSeq + +# Read input file + +let data = stdin.lines().toSeq() +let nlines = len(data) +let ncols = len(data[0]) + +# Puzzle 1 + +proc isXmas(x, y, dx, dy: int): bool = + x + 3*dx >= 0 and + x + 3*dx < ncols and + y + 3*dy >= 0 and + y + 3*dy < ncols and + data[y][x] == 'X' and + data[y+dy][x+dx] == 'M' and + data[y+2*dy][x+2*dx] == 'A' and + data[y+3*dy][x+3*dx] == 'S' + +const dirs = @[(1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1, -1)] + +proc countXmasAt(x, y: int): int = + result = 0 + if data[y][x] == 'X': + for d in dirs: + if isXmas(x, y, d[0], d[1]): + result += 1 + +proc countXmas(): int = + result = 0 + for y in 0 ..< nlines: + for x in 0 ..< ncols: + result += countXmasAt(x, y) + +echo "Puzzle 1: ", countXmas() + +# Puzzle 2 + +const dirs2 = @[(1,1), (-1,1), (-1,-1), (1,-1)] + +proc isMas(x, y, dx, dy: int): bool = + data[y-dy][x-dx] == 'M' and + data[y+dy][x+dx] == 'S' + +proc is2Mas(x, y, i: int): bool = + let + i2 = (i + 1) mod len(dirs2) + d1 = dirs2[i] + d2 = dirs2[i2] + result = isMas(x, y, d1[0], d1[1]) and isMas(x, y, d2[0], d2[1]) + +proc countMasAt(x, y: int): int = + result = 0 + if data[y][x] == 'A': + for i in 0 ..< len(dirs2): + if is2Mas(x, y, i): + result += 1 + +proc countMas(): int = + result = 0 + for y in 1 .. nlines-2: + for x in 1 .. ncols-2: + result += countMasAt(x, y) + +echo "Puzzle 2: ", countMas() diff --git a/2024/ref/day04a.txt b/2024/ref/day04a.txt @@ -0,0 +1,5 @@ +..X... +.SAMX. +.A..A. +XMAS.S +.X.... diff --git a/2024/ref/day04b.txt b/2024/ref/day04b.txt @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX diff --git a/2024/ref/day04c.txt b/2024/ref/day04c.txt @@ -0,0 +1,10 @@ +.M.S...... +..A..MSMS. +.M.S.MAA.. +..A.ASMSM. +.M.S.M.... +.......... +S.S.S.S.S. +.A.A.A.A.. +M.M.M.M.M. +..........