Skip to content

Commit

Permalink
feat(aoc2024): day2 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 2, 2024
1 parent c38bd7b commit b405fe0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 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
3 changes: 3 additions & 0 deletions visp/examples/aoc2024/common.visp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
(fn EnumareteSpanSplitChars ([ch: array<char>] [text: ReadOnlySpan<char>])
(.EnumerateSplitSubstrings text ch commonSplitOptions))

(fn EnumerateSpaceSeparated ([text: ReadOnlySpan<char>])
(.EnumerateSplitSubstrings text [| #\space |] (bor StringSplitOptions.TrimEntries StringSplitOptions.RemoveEmptyEntries)))

(fn ReadInput ([day: string])
(let filepath (String.concat "" [| "./inputs/" day (if IS_EXAMPLE "_example" "") ".txt" |]))
(printfn "file: %s" filepath)
Expand Down
92 changes: 92 additions & 0 deletions visp/examples/aoc2024/day2.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

;; 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:

;;
;; day2
;;
;; Include common utlities
(include "./common.visp")

;; Functions & types

(fn ParseFile ([text: string])
(mut lines (EnumerateSpanSplitLines text))

(mut data (||))

(Macro_ReadWhileNotEmpty [line lines]
;; Read contents here
(mut parts (EnumerateSpaceSeparated line))

(mut items (||))

(Macro_ReadWhileNotEmpty [part parts]
(up! items (cons (span->int32 part))))

(up! data (cons (List.rev items)))

())

(List.rev data))

(fn Part1 (parsedInput)
;; (printfn "%A" parsedInput)

(fn IsSafeList (lst)
(-> lst
(List.pairwise)
(List.fold
#(begin
(let [(valid, validDiff) : bool * int32] %1)

(if (not valid)
(false . 0)
(begin
(let [(lhs, rhs) : int32 * int32] %2)
(let diff (- rhs lhs))
(let diff_sign (int32->sign diff))
(let abs_diff (abs diff))
(let safe (and (>= abs_diff 1) (<= abs_diff 3)))

(cond_
[(and safe (= validDiff 0))
(true . diff_sign)]

[(and safe (= validDiff diff_sign))
(true . diff_sign)
]
[_ (false . 0)])
))


) (true . 0))
fst
))

;; Implement part1
(-> parsedInput
(List.filter IsSafeList)
(List.length)
))

(fn Part2 (parsedInput)
;; Implement part2
0)

;; Implementation

(let parsed (-> (ReadInput "day2") ParseFile))

;; Expected results
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 2 606))
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE -1 -1))

(WriteResult "part1" (-> parsed Part1) PART1_EXPECTED_RESULT)
(WriteResult "part2" (-> parsed Part2) PART2_EXPECTED_RESULT)

()
6 changes: 6 additions & 0 deletions visp/examples/aoc2024/inputs/day2_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
3 changes: 3 additions & 0 deletions visp/lib/core.visp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
[(x, y) (gcd64 y (rem x y))]
))

(fn inline int32->sign [(a: int32)] (System.Math.Sign a))
(fn inline int64->sign [(a: int64)] (System.Math.Sign a))

(fn inline lcm64 (a b)
(/ (* a b) (gcd64 a b)))

Expand Down

0 comments on commit b405fe0

Please sign in to comment.