Skip to content

Commit

Permalink
feat(aoc2024): day6 (#54)
Browse files Browse the repository at this point in the history
* 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
vipentti authored Dec 6, 2024
1 parent 2d99878 commit e9b6542
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 16 deletions.
10 changes: 5 additions & 5 deletions aoc2024.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ param (
[switch] $All,
[switch] $NoBuild,
[ValidateSet(
"day1", "day2", "day3", "day4", "day5"
#, "day6", "day7", "day8", "day9"
# , "day10", "day11", "day12", "day13", "day14", "day15", "day16"
# , "day17", "day18", "day19", "day20", "day21", "day22"
# , "day23", "day24", "day25"
"day1", "day2", "day3", "day4", "day5", "day6"
, "day7", "day8", "day9"
, "day10", "day11", "day12", "day13", "day14", "day15", "day16"
, "day17", "day18", "day19", "day20", "day21", "day22"
, "day23", "day24", "day25"
)]
[string] $Day
)
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day17.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day18.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day19.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day20.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day21.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day22.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day23.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day24.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2023/day25.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2023 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
12 changes: 12 additions & 0 deletions visp/examples/aoc2024/common.visp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@
(begin
(ignore (.MoveNext enu))
(+Current enu))])

(syntax-macro Macro_TimeExecution
[(_ op body ...)
(begin
(let sw (.StartNew System.Diagnostics.Stopwatch))

(let result (begin body ...))

(.Stop sw)
(printfn "%s took %fms" op (->> sw +Elapsed +TotalMilliseconds))

result)])
1 change: 0 additions & 1 deletion visp/examples/aoc2024/day4.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2024 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2024/day5.visp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;; Copyright 2024 Ville Penttinen
;; Distributed under the MIT License.
;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md
Expand Down
143 changes: 143 additions & 0 deletions visp/examples/aoc2024/day6.visp
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)

()
26 changes: 26 additions & 0 deletions visp/examples/aoc2024/direction.visp
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]))
3 changes: 3 additions & 0 deletions visp/examples/aoc2024/init.visp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(open Flurl.Http)

(fn GetFileTemplate (day)
(->>
$$"""
;; Copyright 2024 Ville Penttinen
;; Distributed under the MIT License.
Expand Down Expand Up @@ -56,6 +57,8 @@
()
"""
#(.TrimStart %1)
)
)

(fn TryGetNamedArg (name)
Expand Down
10 changes: 10 additions & 0 deletions visp/examples/aoc2024/inputs/day6_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

0 comments on commit e9b6542

Please sign in to comment.