-
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.
* feat(aoc2024): day6 part1 * chore: Remove extra whitespace from start of files * feat(aoc2024): day6 part2 * feat(aoc2024): Add Macro_TimeExecution * feat(2024): day6 part 2 slight optimization using Array.Parallel
- Loading branch information
Showing
17 changed files
with
199 additions
and
16 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
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
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
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
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
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
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,143 @@ | ||
;; 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: | ||
|
||
;; | ||
;; day6 | ||
;; | ||
;; Include common utlities | ||
(include "./common.visp") | ||
(include "./grid.visp") | ||
(include "./direction.visp") | ||
|
||
(fn inline Direction_GetPosFun ([d: Direction]) | ||
(match d | ||
[Up Pos_UpOf] | ||
[Down Pos_DownOf] | ||
[Left Pos_LeftOf] | ||
[Right Pos_RightOf] | ||
)) | ||
|
||
;; 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 FindStart (parsedInput) | ||
(let H (Grid_Height parsedInput)) | ||
(let W (Grid_Width parsedInput)) | ||
|
||
(mut start_pos (0 . 0)) | ||
|
||
(for/to [y (0 to (dec H))] | ||
(for/to [x (0 to (dec W))] | ||
(match (Grid_Get (x . y) parsedInput) | ||
[(Some #\^) (set! start_pos (x . y)) ] | ||
[_ ()]))) | ||
start_pos) | ||
|
||
(fn FindVisited (parsedInput) | ||
|
||
(mut start_pos (FindStart parsedInput)) | ||
|
||
(mut visited (Set.singleton start_pos)) | ||
(mut start_dir Direction.Up) | ||
(mut looping true) | ||
|
||
(while looping | ||
(let next ((Direction_GetPosFun start_dir) start_pos)) | ||
|
||
(match (Grid_Get next parsedInput) | ||
[None (set! looping false)] | ||
[(Some #\#) | ||
;; Found wall | ||
(set! start_dir (Direction_TurnRight start_dir)) | ||
] | ||
[(Some _) | ||
;; (printfn "next %A" next) | ||
(up! visited (Set.add next)) | ||
(set! start_pos next) | ||
] | ||
)) | ||
visited) | ||
|
||
(fn Part1 (parsedInput) | ||
;; Implement part1 | ||
(->> parsedInput | ||
FindVisited | ||
Set.count)) | ||
|
||
(fn Part2 (parsedInput) | ||
;; Implement part2 | ||
(fn RunScenario [start obstruction grid] | ||
(mut looping true) | ||
|
||
(mut pos start) | ||
(mut dir Direction.Up) | ||
(mut visited (Set.singleton (pos . dir))) | ||
(mut looping true) | ||
(mut result false) | ||
|
||
(fn Add_Visited [pos dir] | ||
(if (Set.contains (pos . dir) visited) | ||
false | ||
(begin (up! visited (Set.add (pos . dir))) true))) | ||
|
||
(while looping | ||
(let next ((Direction_GetPosFun dir) pos)) | ||
(cond_ | ||
[(= next obstruction) | ||
(set! dir (Direction_TurnRight dir)) | ||
(unless (Add_Visited pos dir) | ||
(set! result true) | ||
(set! looping false)) | ||
] | ||
[_ | ||
(match (Grid_Get next grid) | ||
[None (set! looping false)] | ||
[(Some #\#) | ||
(set! dir (Direction_TurnRight dir)) | ||
(unless (Add_Visited pos dir) | ||
(set! result true) | ||
(set! looping false)) | ||
] | ||
[(Some _) | ||
(set! pos next) | ||
(unless (Add_Visited pos dir) | ||
(set! result true) | ||
(set! looping false)) | ||
])] | ||
)) | ||
result | ||
) | ||
|
||
(let start (FindStart parsedInput)) | ||
(let tests (->> (FindVisited parsedInput) (Set.remove start))) | ||
(->> tests | ||
(Array.ofSeq) | ||
(Array.Parallel.filter #(RunScenario start %1 parsedInput)) | ||
(Array.length))) | ||
|
||
;; Implementation | ||
|
||
(let parsed (-> (ReadInput "day6") ParseFile)) | ||
|
||
;; Expected results | ||
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 41 5404)) | ||
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 6 1984)) | ||
|
||
(WriteResult "part1" (Macro_TimeExecution "part1" (-> parsed Part1)) PART1_EXPECTED_RESULT) | ||
(WriteResult "part2" (Macro_TimeExecution "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,26 @@ | ||
;; 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: | ||
|
||
(union Direction | ||
Up | ||
Left | ||
Down | ||
Right) | ||
|
||
(fn inline Direction_TurnLeft ([d: Direction]) | ||
(match d | ||
[Up Left] | ||
[Left Down] | ||
[Down Right] | ||
[Right Up])) | ||
|
||
(fn inline Direction_TurnRight ([d: Direction]) | ||
(match d | ||
[Up Right] | ||
[Left Up] | ||
[Down Left] | ||
[Right Down])) |
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,10 @@ | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... |