From a137df5075858f102194ca15c05b3e3ac255a06d Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 3 Dec 2024 07:20:01 +0200 Subject: [PATCH 1/3] feat(aoc2024): day3 part1 --- aoc2024.ps1 | 4 +- visp/examples/aoc2024/day3.visp | 53 +++++++++++++++++++ visp/examples/aoc2024/inputs/day3_example.txt | 1 + visp/lib/core.visp | 3 ++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 visp/examples/aoc2024/day3.visp create mode 100644 visp/examples/aoc2024/inputs/day3_example.txt 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/day3.visp b/visp/examples/aoc2024/day3.visp new file mode 100644 index 0000000..faedf14 --- /dev/null +++ b/visp/examples/aoc2024/day3.visp @@ -0,0 +1,53 @@ + +;; 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 + +(fn ParseFile ([text: string]) + ;; (mut lines (EnumerateSpanSplitLines text)) + + (let rx (new Regex "mul\((\d{1,3}),(\d{1,3})\)" (.+Compiled RegexOptions))) + + (let matches (.Matches rx text)) + + (mut pairs (||)) + + (for/in [mtch matches] + ;;; (printfn "foo %A %A %A" (.+Value mtch) (->> mtch +Groups .[1]) (->> mtch +Groups .[2])) + (up! pairs (cons ((->> mtch +Groups .[1] +Value int) . (->> mtch +Groups .[2] +Value int))))) + + (List.rev pairs)) + +(fn Part1 (parsedInput) + ;; Implement part1 + ;; (printfn "%A" parsedInput) + (->> parsedInput + (List.fold #(+ %1 (* (fst %2) (snd %2))) 0))) + +(fn Part2 (parsedInput) + ;; Implement part2 + 0) + +;; Implementation + +(let parsed (-> (ReadInput "day3") ParseFile)) + +;; Expected results +(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 161 174561379)) +(let PART2_EXPECTED_RESULT (if IS_EXAMPLE -1 -1)) + +(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..f274bda --- /dev/null +++ b/visp/examples/aoc2024/inputs/day3_example.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)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)) From 1b69904fed0e9ed5f058e82c2887d667784a16e5 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 3 Dec 2024 07:33:25 +0200 Subject: [PATCH 2/3] feat(aoc2024): day3 part2 --- visp/examples/aoc2024/day3.visp | 45 ++++++++++++++++--- visp/examples/aoc2024/inputs/day3_example.txt | 2 +- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/visp/examples/aoc2024/day3.visp b/visp/examples/aoc2024/day3.visp index faedf14..59be7c8 100644 --- a/visp/examples/aoc2024/day3.visp +++ b/visp/examples/aoc2024/day3.visp @@ -13,19 +13,35 @@ (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})\)" (.+Compiled RegexOptions))) + (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 %A %A" (.+Value mtch) (->> mtch +Groups .[1]) (->> mtch +Groups .[2])) - (up! pairs (cons ((->> mtch +Groups .[1] +Value int) . (->> mtch +Groups .[2] +Value int))))) + ;; (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)) @@ -33,11 +49,30 @@ ;; 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 - 0) + ;; (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 @@ -45,7 +80,7 @@ ;; Expected results (let PART1_EXPECTED_RESULT (if IS_EXAMPLE 161 174561379)) -(let PART2_EXPECTED_RESULT (if IS_EXAMPLE -1 -1)) +(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 index f274bda..30032cb 100644 --- a/visp/examples/aoc2024/inputs/day3_example.txt +++ b/visp/examples/aoc2024/inputs/day3_example.txt @@ -1 +1 @@ -xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) From 040f8c4a41f0dd83f9404c8b23dd7f43f09ab680 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 3 Dec 2024 07:36:30 +0200 Subject: [PATCH 3/3] chore(aoc2024): fix formatting --- visp/examples/aoc2024/day1.visp | 1 - visp/examples/aoc2024/day2.visp | 1 - visp/examples/aoc2024/day3.visp | 1 - 3 files changed, 3 deletions(-) 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 index 59be7c8..192cb08 100644 --- a/visp/examples/aoc2024/day3.visp +++ b/visp/examples/aoc2024/day3.visp @@ -1,4 +1,3 @@ - ;; Copyright 2024 Ville Penttinen ;; Distributed under the MIT License. ;; https://github.com/vipentti/visp-fs/blob/main/LICENSE.md