diff --git a/Aoc2024/Day04/Solve.lean b/Aoc2024/Day04/Solve.lean index c33ffbf..e58d40c 100644 --- a/Aoc2024/Day04/Solve.lean +++ b/Aoc2024/Day04/Solve.lean @@ -3,9 +3,12 @@ import Aoc2024.Day04.Examples import Aoc2024.Day04.Parser import Batteries +-- Part 1 + private def countXmasOccurrencesInRow : List Char -> Int | [] => 0 - | 'X' :: 'M' :: 'A' :: 'S' :: rest => 1 + countXmasOccurrencesInRow rest + | 'X' :: 'M' :: 'A' :: 'S' :: rest => 1 + countXmasOccurrencesInRow ('S' :: rest) -- continue scan from S as it could start a SAMX + | 'S' :: 'A' :: 'M' :: 'X' :: rest => 1 + countXmasOccurrencesInRow ('X' :: rest) -- continue scan from X as it could start a XMAS | _ :: rest => countXmasOccurrencesInRow rest #guard countXmasOccurrencesInRow "ABXMASXMASXMA".toList == 2 @@ -43,22 +46,22 @@ private def diagonals (grid : List (List α)) : List (List α) := private def allGridTransformations : List (Grid -> Grid) := [ id, - flipHorizontal, List.transpose, - flipHorizontal ∘ List.transpose, diagonals, diagonals ∘ flipHorizontal, - diagonals ∘ List.transpose, - diagonals ∘ List.transpose ∘ flipHorizontal, ] def solvePart1 (s : String): Int := let grid := parseGrid s allGridTransformations.sumBy (λ transformation => transformation grid |> countXmasOccurrencesInRows) +#eval solvePart1 exampleInput1 + #guard solvePart1 exampleInput1 = 4 #guard solvePart1 exampleInput2 = 18 +-- Part 2 + def solvePart2 (things : s) : Int := sorry -- #guard solvePart2 exampleInput2 = 9