Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/aoc2024/day2 #49

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
107 changes: 107 additions & 0 deletions visp/examples/aoc2024/day2.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

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

(fn OneRemoved (lst)
(mut lists (||))

(for/in [idx (!r 0 .. 1 .. (dec (List.length lst)))]
(up! lists (cons (List.removeAt idx lst))))

lists)

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

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

(fn Part2 (parsedInput)
;; Implement part2
(-> parsedInput
(List.filter #(begin
(let origSafe (IsSafeList %1))
(if origSafe
true
(begin
(-> (OneRemoved %1)
(List.exists IsSafeList))
))
))
(List.length)
))

;; Implementation

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

;; Expected results
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 2 606))
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 4 644))

(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