-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
304 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def exampleInput1 := | ||
"..X... | ||
.SAMX. | ||
.A..A. | ||
XMAS.S | ||
.X...." | ||
|
||
def exampleInput2 := | ||
"MMMSXXMASM | ||
MSAMXMSMSA | ||
AMXSXMAAMM | ||
MSAMASMSMX | ||
XMASAMXAMM | ||
XXAMMXXAMA | ||
SMSMSASXSS | ||
SAXAMASAAA | ||
MAMMMXMMMM | ||
MXMXAXMASX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Aoc2024.Day04.Solve | ||
import Aoc2024.CustomMonadLift | ||
|
||
def main : IO Unit := do | ||
IO.println "Day 04" | ||
IO.println "" | ||
IO.println "Part 1" | ||
let exampleInput <- IO.FS.readFile "Aoc2024/Day04/inputs/example.txt" | ||
let puzzleInput <- IO.FS.readFile "Aoc2024/Day04/inputs/input.txt" | ||
IO.println s!"Example: {solvePart1 exampleInput}" | ||
IO.println s!"Puzzle: {solvePart1 puzzleInput}" -- | ||
IO.println "" | ||
IO.println "Part 2" | ||
IO.println s!"Example: {solvePart2 exampleInput}" | ||
IO.println s!"Puzzle: {solvePart2 puzzleInput}" -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Aoc2024.Day04.Examples | ||
import Aoc2024.Day04.Types | ||
import Aoc2024.Utils | ||
import Std | ||
|
||
def parseGrid (s : String) : Grid := s.lines.map (·.toList) | ||
|
||
-- #guard parseReports exampleInput == Except.ok 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Aoc2024.Utils | ||
import Aoc2024.Day04.Examples | ||
import Aoc2024.Day04.Parser | ||
import Batteries | ||
|
||
private def countXmasOccurrencesInRow : List Char -> Int | ||
| [] => 0 | ||
| 'X' :: 'M' :: 'A' :: 'S' :: rest => 1 + countXmasOccurrencesInRow rest | ||
| _ :: rest => countXmasOccurrencesInRow rest | ||
|
||
#guard countXmasOccurrencesInRow "ABXMASXMASXMA".toList == 2 | ||
|
||
private def countXmasOccurrencesInRows (grid : Grid): Int := | ||
grid.sumBy countXmasOccurrencesInRow | ||
|
||
private def gridToString (g : Grid) : String := g.map List.asString |> String.intercalate "\n" | ||
|
||
#guard gridToString [['A', 'B', 'C'], ['D', 'E', 'F']] = "ABC\nDEF" | ||
|
||
private def flipHorizontal (grid : Grid) : Grid := grid.map (·.reverse) | ||
|
||
private def diagonals (grid : List (List α)) : List (List α) := | ||
let rec go (b : List (List α)) (es : List (List α)) : List (List α) := | ||
let diagonal := b.filterMap List.head? | ||
let ts := b.filterMap List.tail? | ||
diagonal :: match es with | ||
| [] => ts.transpose | ||
| e :: es' => go (e :: ts) es' | ||
(go [] grid).tail! | ||
|
||
#guard diagonals [ | ||
["A", "B", "C", "D"], | ||
["E", "F", "G", "H"], | ||
["I", "J", "K", "L"], | ||
] == [ | ||
["A"], | ||
["E", "B"], | ||
["I", "F", "C"], | ||
["J", "G", "D"], | ||
["K", "H"], | ||
["L"], | ||
] | ||
|
||
private def allGridTransformations : List (Grid -> Grid) := [ | ||
id, | ||
flipHorizontal, | ||
List.transpose, | ||
flipHorizontal ∘ List.transpose, | ||
diagonals, | ||
diagonals ∘ List.transpose, | ||
diagonals ∘ flipHorizontal, | ||
diagonals ∘ List.transpose ∘ flipHorizontal, | ||
] | ||
|
||
-- def g := parseGrid exampleInput1 | ||
-- #eval g |> id | ||
-- #eval g |> flipHorizontal | ||
-- #eval g |> List.transpose | ||
-- #eval g |> List.transpose |> flipHorizontal | ||
-- #eval g |> diagonals | ||
-- #eval g |> List.transpose |> diagonals | ||
-- #eval g |> flipHorizontal |> diagonals | ||
-- #eval g |> flipHorizontal |> List.transpose |> diagonals | ||
|
||
def solvePart1 (s : String): Int := | ||
let grid := parseGrid s | ||
allGridTransformations.sumBy (λ transformation => transformation grid |> countXmasOccurrencesInRows) | ||
|
||
#guard solvePart1 exampleInput1 = 4 | ||
#guard solvePart1 exampleInput2 = 18 | ||
|
||
def solvePart2 (things : s) : Int := sorry | ||
|
||
-- def parseAndSolvePart2 (s : String): Except String Int := parseGrid s |>.map solvePart2 | ||
|
||
-- #guard parseAndSolvePart2 exampleInput == Except.ok 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
abbrev Grid := List (List Char) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MMMSXXMASM | ||
MSAMXMSMSA | ||
AMXSXMAAMM | ||
MSAMASMSMX | ||
XMASAMXAMM | ||
XXAMMXXAMA | ||
SMSMSASXSS | ||
SAXAMASAAA | ||
MAMMMXMMMM | ||
MXMXAXMASX |
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Aoc2024.DayXX.Solve | ||
import Aoc2024.CustomMonadLift | ||
|
||
def main : IO Unit := do | ||
IO.println "Day XX" | ||
IO.println "" | ||
IO.println "Part 1" | ||
let exampleInput <- IO.FS.readFile "Aoc2024/DayXX/inputs/example.txt" | ||
let puzzleInput <- IO.FS.readFile "Aoc2024/DayXX/inputs/input.txt" | ||
IO.println s!"Example: {parseAndSolvePart1 exampleInput}" | ||
IO.println s!"Puzzle: {parseAndSolvePart1 puzzleInput}" -- | ||
IO.println "" | ||
IO.println "Part 2" | ||
IO.println s!"Example: {parseAndSolvePart2 exampleInput}" | ||
IO.println s!"Puzzle: {parseAndSolvePart2 puzzleInput}" -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters