diff --git a/aoc2024.ps1 b/aoc2024.ps1 index bd6c791..c0e31c5 100644 --- a/aoc2024.ps1 +++ b/aoc2024.ps1 @@ -6,8 +6,8 @@ param ( [switch] $All, [switch] $NoBuild, [ValidateSet( - "day1" , "day2" - # , "day3", "day4", "day5", "day6", "day7", "day8", "day9" + "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" diff --git a/visp/examples/aoc2024/day1.visp b/visp/examples/aoc2024/day1.visp index ce0e1b4..85401cf 100644 --- a/visp/examples/aoc2024/day1.visp +++ b/visp/examples/aoc2024/day1.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/day2.visp b/visp/examples/aoc2024/day2.visp index 5f41220..88c5d97 100644 --- a/visp/examples/aoc2024/day2.visp +++ b/visp/examples/aoc2024/day2.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/day3.visp b/visp/examples/aoc2024/day3.visp new file mode 100644 index 0000000..192cb08 --- /dev/null +++ b/visp/examples/aoc2024/day3.visp @@ -0,0 +1,87 @@ +;; 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: + +;; +;; day3 +;; +;; Include common utlities +(include "./common.visp") + +;; Functions & types +(union Ins + [Mul [lhs: int] [rhs: int]] + Do + Dont) + +(fn ParseFile ([text: string]) + ;; (mut lines (EnumerateSpanSplitLines text)) + + (let rx (new Regex "mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)" (.+Compiled RegexOptions))) + + (let matches (.Matches rx text)) + + (mut pairs (||)) + + (for/in [mtch matches] + ;; (printfn "foo %A" (.+Value mtch)) + (let val (+Value mtch)) + (cond_ + [(.StartsWith val "mul") + () + (up! pairs (cons (Ins.Mul ((->> mtch +Groups .[1] +Value int) . (->> mtch +Groups .[2] +Value int))))) + ] + [(.StartsWith val "don't") + (up! pairs (cons Ins.Dont)) + ] + [(.StartsWith val "do") + (up! pairs (cons Ins.Do)) + ] + )) + + (List.rev pairs)) + +(fn Part1 (parsedInput) + ;; Implement part1 + ;; (printfn "%A" parsedInput) + (->> parsedInput + (List.map #(match %1 + [(Ins.Mul (lhs . rhs)) (lhs . rhs)] + [_ (0 . 0)] + )) + (List.fold #(+ %1 (* (fst %2) (snd %2))) 0))) + +(fn Part2 (parsedInput) + ;; Implement part2 + ;; (printfn "%A" parsedInput) + (->> parsedInput + (List.fold #(begin + (let [(enabled, total) : bool * int] %1) + (match %2 + [(Ins.Mul (lhs . rhs)) + (if enabled + (enabled . (+ total (* lhs rhs))) + (enabled . total)) + ] + [Ins.Do (true . total)] + [Ins.Dont (false . total)] + ) + ) (true . 0)) + snd + )) + +;; Implementation + +(let parsed (-> (ReadInput "day3") ParseFile)) + +;; Expected results +(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 161 174561379)) +(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 48 106921067)) + +(WriteResult "part1" (-> parsed Part1) PART1_EXPECTED_RESULT) +(WriteResult "part2" (-> parsed Part2) PART2_EXPECTED_RESULT) + +() diff --git a/visp/examples/aoc2024/inputs/day3_example.txt b/visp/examples/aoc2024/inputs/day3_example.txt new file mode 100644 index 0000000..30032cb --- /dev/null +++ b/visp/examples/aoc2024/inputs/day3_example.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) diff --git a/visp/lib/core.visp b/visp/lib/core.visp index 0ebb910..a0adf03 100644 --- a/visp/lib/core.visp +++ b/visp/lib/core.visp @@ -12,6 +12,9 @@ (failwithf "not valid number '%s'" it) (. result +Value))) +(fn string->int32 [(it: string)] + (System.Int32.Parse it)) + (fn string->int [(it: string)] (System.Int64.Parse it))