-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
221 additions
and
2 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
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,133 @@ | ||
|
||
;; Copyright 2024 Ville Penttinen | ||
;; Distributed under the MIT License. | ||
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md | ||
;; | ||
;; for basic syntax highlighting | ||
;; vim: set syntax=clojure: | ||
|
||
;; | ||
;; day4 | ||
;; | ||
;; Include common utlities | ||
(include "./common.visp") | ||
(include "./grid.visp") | ||
|
||
;; Functions & types | ||
|
||
(fn ParseFile ([text: string]) | ||
(mut lines (EnumerateSpanSplitLines text)) | ||
|
||
(let res (new ResizeArray<_>)) | ||
|
||
(Macro_ReadWhileNotEmpty [line lines] | ||
;; Read contents here | ||
(.Add res (->> (.ToArray line))) | ||
()) | ||
(array2D (.ToArray res))) | ||
|
||
(fn Part1 (parsedInput) | ||
;; (printfn "%A" parsedInput) | ||
(let H (Grid_Height parsedInput)) | ||
(let W (Grid_Width parsedInput)) | ||
|
||
(mut xmas_count 0) | ||
|
||
(for/to [y (0 to (dec H))] | ||
(for/to [x (0 to (dec W))] | ||
(match (Grid_Get (x . y) parsedInput) | ||
[(Some #\X) | ||
;; (printfn "X at %A" (x . y)) | ||
|
||
(for/in [offset POS_OFFSETS] | ||
;; todo | ||
(let next (Pos_Add (x . y) offset)) | ||
(match (Grid_Get next parsedInput) | ||
[(Some #\M) | ||
;; (printfn "M at %A" next) | ||
|
||
(let next_next (Pos_Add next offset)) | ||
|
||
(match (Grid_Get next_next parsedInput) | ||
[(Some #\A) | ||
;; (printfn "A at %A" next_next) | ||
|
||
(let next_next_next (Pos_Add next_next offset)) | ||
|
||
(match (Grid_Get next_next_next parsedInput) | ||
[(Some #\S) | ||
;; (printfn "S at %A" next_next_next) | ||
(up! xmas_count inc) | ||
] | ||
[_ ()] | ||
) | ||
] | ||
[_ ()] | ||
) | ||
] | ||
[_ ()] | ||
)) | ||
] | ||
[_ ()]) | ||
)) | ||
|
||
;; Implement part1 | ||
xmas_count) | ||
|
||
(fn IsChar [v ch] | ||
(match v | ||
[(Some x) (= x ch)] | ||
[_ false])) | ||
|
||
(fn Part2 (parsedInput) | ||
;; Implement part2 | ||
(let H (Grid_Height parsedInput)) | ||
(let W (Grid_Width parsedInput)) | ||
|
||
(mut xmas_count 0) | ||
|
||
(for/to [y (0 to (dec H))] | ||
(for/to [x (0 to (dec W))] | ||
(match (Grid_Get (x . y) parsedInput) | ||
[(Some #\A) | ||
(mut m_count 0) | ||
(mut s_count 0) | ||
|
||
(let up_left (Grid_Get (Pos_UpLeftOf (x . y)) parsedInput)) | ||
(let up_right (Grid_Get (Pos_UpRightOf (x . y)) parsedInput)) | ||
(let down_right (Grid_Get (Pos_DownRightOf (x . y)) parsedInput)) | ||
(let down_left (Grid_Get (Pos_DownLeftOf (x . y)) parsedInput)) | ||
|
||
(when_ (and (IsChar up_left #\M) (IsChar down_right #\S)) | ||
(up! m_count inc)) | ||
|
||
(when_ (and (IsChar up_left #\S) (IsChar down_right #\M)) | ||
(up! m_count inc)) | ||
|
||
(when_ (and (IsChar up_right #\M) (IsChar down_left #\S)) | ||
(up! s_count inc)) | ||
|
||
(when_ (and (IsChar up_right #\S) (IsChar down_left #\M)) | ||
(up! s_count inc)) | ||
|
||
(when_ (and (= m_count 1) (= s_count 1)) | ||
(up! xmas_count inc)) | ||
() | ||
] | ||
[_ ()] | ||
))) | ||
|
||
xmas_count) | ||
|
||
;; Implementation | ||
|
||
(let parsed (-> (ReadInput "day4") ParseFile)) | ||
|
||
;; Expected results | ||
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 18 2370)) | ||
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 9 1908)) | ||
|
||
(WriteResult "part1" (-> parsed Part1) PART1_EXPECTED_RESULT) | ||
(WriteResult "part2" (-> parsed Part2) PART2_EXPECTED_RESULT) | ||
|
||
() |
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,33 @@ | ||
;; Copyright 2024 Ville Penttinen | ||
;; Distributed under the MIT License. | ||
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md | ||
;; | ||
;; for basic syntax highlighting | ||
;; vim: set syntax=clojure: | ||
|
||
(include "./pos.visp") | ||
|
||
(typedef Grid char[,]) | ||
|
||
(fn inline Grid_Height ([g: Grid]) | ||
(Array2D.length1 g)) | ||
|
||
(fn inline Grid_Width ([g: Grid]) | ||
(Array2D.length2 g)) | ||
|
||
(fn inline Grid_RowSlice ([g: Grid] [col: int]) | ||
(.[*, col] g)) | ||
|
||
(fn inline Grid_ColSlice ([g: Grid] [row: int]) | ||
(.[row, *] g)) | ||
|
||
(fn inline Grid_Contains? ([(x, y): Pos] [g: Grid]) | ||
(and | ||
(and (>= x 0) (< x (Grid_Width g))) | ||
(and (>= y 0) (< y (Grid_Height g))))) | ||
|
||
(fn inline Grid_Get ([(x, y): Pos] [g: Grid]) | ||
(cond_ | ||
[(Grid_Contains? (x, y) g) (Some (.[y, x] g))] | ||
[_ None] | ||
)) |
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 |
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,43 @@ | ||
;; Copyright 2024 Ville Penttinen | ||
;; Distributed under the MIT License. | ||
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md | ||
;; | ||
;; for basic syntax highlighting | ||
;; vim: set syntax=clojure: | ||
|
||
(typedef Pos int * int) | ||
|
||
(fn inline Pos_Add ([(x, y): Pos] [(ox, oy): Pos]) ((+ x ox), (+ y oy))) | ||
(fn inline Pos_LeftOf ([(x, y): Pos]) ((dec x), y)) | ||
(fn inline Pos_RightOf ([(x, y): Pos]) ((inc x), y)) | ||
(fn inline Pos_UpOf ([(x, y): Pos]) (x, (dec y))) | ||
(fn inline Pos_DownOf ([(x, y): Pos]) (x, (inc y))) | ||
(fn inline Pos_UpLeftOf ([(x, y): Pos]) ((dec x), (dec y))) | ||
(fn inline Pos_UpRightOf ([(x, y): Pos]) ((inc x), (dec y))) | ||
(fn inline Pos_DownLeftOf ([(x, y): Pos]) ((dec x), (inc y))) | ||
(fn inline Pos_DownRightOf ([(x, y): Pos]) ((inc x), (inc y))) | ||
|
||
(let POS_CARDINALS [| | ||
(0, -1) | ||
(-1, 0) | ||
(1, 0) | ||
(0, 1) | ||
|]) | ||
|
||
(let POS_OFFSETS [| | ||
(-1, -1) | ||
(0, -1) | ||
(1, -1) | ||
(-1, 0) | ||
(1, 0) | ||
(-1, 1) | ||
(0, 1) | ||
(1, 1) | ||
|]) | ||
|
||
(let POS_DIAGONALS [| | ||
(-1, -1) | ||
(1, -1) | ||
(-1, 1) | ||
(1, 1) | ||
|]) |