Skip to content

Commit

Permalink
feat(aoc2024): day3 (#51)
Browse files Browse the repository at this point in the history
* feat(aoc2024): day3 part1

* feat(aoc2024): day3 part2

* chore(aoc2024): fix formatting
  • Loading branch information
vipentti authored Dec 3, 2024
1 parent 4855ca9 commit 4419a77
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
4 changes: 2 additions & 2 deletions aoc2024.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion visp/examples/aoc2024/day1.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/day2.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
87 changes: 87 additions & 0 deletions visp/examples/aoc2024/day3.visp
Original file line number Diff line number Diff line change
@@ -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)

()
1 change: 1 addition & 0 deletions visp/examples/aoc2024/inputs/day3_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
3 changes: 3 additions & 0 deletions visp/lib/core.visp
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 4419a77

Please sign in to comment.