From e9b654296f55d6ad68f88e927d407e8fd49ee184 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 6 Dec 2024 07:40:17 +0100 Subject: [PATCH] feat(aoc2024): day6 (#54) * 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 --- aoc2024.ps1 | 10 +- visp/examples/aoc2023/day17.visp | 1 - visp/examples/aoc2023/day18.visp | 1 - visp/examples/aoc2023/day19.visp | 1 - visp/examples/aoc2023/day20.visp | 1 - visp/examples/aoc2023/day21.visp | 1 - visp/examples/aoc2023/day22.visp | 1 - visp/examples/aoc2023/day23.visp | 1 - visp/examples/aoc2023/day24.visp | 1 - visp/examples/aoc2023/day25.visp | 1 - visp/examples/aoc2024/common.visp | 12 ++ visp/examples/aoc2024/day4.visp | 1 - visp/examples/aoc2024/day5.visp | 1 - visp/examples/aoc2024/day6.visp | 143 ++++++++++++++++++ visp/examples/aoc2024/direction.visp | 26 ++++ visp/examples/aoc2024/init.visp | 3 + visp/examples/aoc2024/inputs/day6_example.txt | 10 ++ 17 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 visp/examples/aoc2024/day6.visp create mode 100644 visp/examples/aoc2024/direction.visp create mode 100644 visp/examples/aoc2024/inputs/day6_example.txt diff --git a/aoc2024.ps1 b/aoc2024.ps1 index a595b85..2771a23 100644 --- a/aoc2024.ps1 +++ b/aoc2024.ps1 @@ -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 ) diff --git a/visp/examples/aoc2023/day17.visp b/visp/examples/aoc2023/day17.visp index f619e4c..fe3aef4 100644 --- a/visp/examples/aoc2023/day17.visp +++ b/visp/examples/aoc2023/day17.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day18.visp b/visp/examples/aoc2023/day18.visp index ecf0d2e..61353f3 100644 --- a/visp/examples/aoc2023/day18.visp +++ b/visp/examples/aoc2023/day18.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day19.visp b/visp/examples/aoc2023/day19.visp index a0e8698..99cde3e 100644 --- a/visp/examples/aoc2023/day19.visp +++ b/visp/examples/aoc2023/day19.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day20.visp b/visp/examples/aoc2023/day20.visp index 7685edc..7000a56 100644 --- a/visp/examples/aoc2023/day20.visp +++ b/visp/examples/aoc2023/day20.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day21.visp b/visp/examples/aoc2023/day21.visp index 650a3d9..5a969dc 100644 --- a/visp/examples/aoc2023/day21.visp +++ b/visp/examples/aoc2023/day21.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day22.visp b/visp/examples/aoc2023/day22.visp index 2fe0025..bceb902 100644 --- a/visp/examples/aoc2023/day22.visp +++ b/visp/examples/aoc2023/day22.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day23.visp b/visp/examples/aoc2023/day23.visp index 8ced881..bfaa8c0 100644 --- a/visp/examples/aoc2023/day23.visp +++ b/visp/examples/aoc2023/day23.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day24.visp b/visp/examples/aoc2023/day24.visp index 896a330..5fde34a 100644 --- a/visp/examples/aoc2023/day24.visp +++ b/visp/examples/aoc2023/day24.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2023/day25.visp b/visp/examples/aoc2023/day25.visp index 59231b9..c02b37c 100644 --- a/visp/examples/aoc2023/day25.visp +++ b/visp/examples/aoc2023/day25.visp @@ -1,4 +1,3 @@ - ;; Copyright 2023 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2024/common.visp b/visp/examples/aoc2024/common.visp index 70d4ed9..492750f 100644 --- a/visp/examples/aoc2024/common.visp +++ b/visp/examples/aoc2024/common.visp @@ -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)]) diff --git a/visp/examples/aoc2024/day4.visp b/visp/examples/aoc2024/day4.visp index 238ae78..1047018 100644 --- a/visp/examples/aoc2024/day4.visp +++ b/visp/examples/aoc2024/day4.visp @@ -1,4 +1,3 @@ - ;; Copyright 2024 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2024/day5.visp b/visp/examples/aoc2024/day5.visp index 588d0ab..8e1089a 100644 --- a/visp/examples/aoc2024/day5.visp +++ b/visp/examples/aoc2024/day5.visp @@ -1,4 +1,3 @@ - ;; Copyright 2024 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md diff --git a/visp/examples/aoc2024/day6.visp b/visp/examples/aoc2024/day6.visp new file mode 100644 index 0000000..fb14950 --- /dev/null +++ b/visp/examples/aoc2024/day6.visp @@ -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) + +() diff --git a/visp/examples/aoc2024/direction.visp b/visp/examples/aoc2024/direction.visp new file mode 100644 index 0000000..f117eff --- /dev/null +++ b/visp/examples/aoc2024/direction.visp @@ -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])) diff --git a/visp/examples/aoc2024/init.visp b/visp/examples/aoc2024/init.visp index 10dd171..e58c348 100644 --- a/visp/examples/aoc2024/init.visp +++ b/visp/examples/aoc2024/init.visp @@ -11,6 +11,7 @@ (open Flurl.Http) (fn GetFileTemplate (day) + (->> $$""" ;; Copyright 2024 Ville Penttinen ;; Distributed under the MIT License. @@ -56,6 +57,8 @@ () """ + #(.TrimStart %1) + ) ) (fn TryGetNamedArg (name) diff --git a/visp/examples/aoc2024/inputs/day6_example.txt b/visp/examples/aoc2024/inputs/day6_example.txt new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/visp/examples/aoc2024/inputs/day6_example.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...