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

feat(aoc2024): day4 #52

Merged
merged 1 commit into from
Dec 4, 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
133 changes: 133 additions & 0 deletions visp/examples/aoc2024/day4.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

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

;;
;; day4
;;
;; Include common utlities
(include "./common.visp")
(include "./grid.visp")

;; Functions & types

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

(let res (new ResizeArray<_>))

(Macro_ReadWhileNotEmpty [line lines]
;; Read contents here
(.Add res (->> (.ToArray line)))
())
(array2D (.ToArray res)))

(fn Part1 (parsedInput)
;; (printfn "%A" parsedInput)
(let H (Grid_Height parsedInput))
(let W (Grid_Width parsedInput))

(mut xmas_count 0)

(for/to [y (0 to (dec H))]
(for/to [x (0 to (dec W))]
(match (Grid_Get (x . y) parsedInput)
[(Some #\X)
;; (printfn "X at %A" (x . y))

(for/in [offset POS_OFFSETS]
;; todo
(let next (Pos_Add (x . y) offset))
(match (Grid_Get next parsedInput)
[(Some #\M)
;; (printfn "M at %A" next)

(let next_next (Pos_Add next offset))

(match (Grid_Get next_next parsedInput)
[(Some #\A)
;; (printfn "A at %A" next_next)

(let next_next_next (Pos_Add next_next offset))

(match (Grid_Get next_next_next parsedInput)
[(Some #\S)
;; (printfn "S at %A" next_next_next)
(up! xmas_count inc)
]
[_ ()]
)
]
[_ ()]
)
]
[_ ()]
))
]
[_ ()])
))

;; Implement part1
xmas_count)

(fn IsChar [v ch]
(match v
[(Some x) (= x ch)]
[_ false]))

(fn Part2 (parsedInput)
;; Implement part2
(let H (Grid_Height parsedInput))
(let W (Grid_Width parsedInput))

(mut xmas_count 0)

(for/to [y (0 to (dec H))]
(for/to [x (0 to (dec W))]
(match (Grid_Get (x . y) parsedInput)
[(Some #\A)
(mut m_count 0)
(mut s_count 0)

(let up_left (Grid_Get (Pos_UpLeftOf (x . y)) parsedInput))
(let up_right (Grid_Get (Pos_UpRightOf (x . y)) parsedInput))
(let down_right (Grid_Get (Pos_DownRightOf (x . y)) parsedInput))
(let down_left (Grid_Get (Pos_DownLeftOf (x . y)) parsedInput))

(when_ (and (IsChar up_left #\M) (IsChar down_right #\S))
(up! m_count inc))

(when_ (and (IsChar up_left #\S) (IsChar down_right #\M))
(up! m_count inc))

(when_ (and (IsChar up_right #\M) (IsChar down_left #\S))
(up! s_count inc))

(when_ (and (IsChar up_right #\S) (IsChar down_left #\M))
(up! s_count inc))

(when_ (and (= m_count 1) (= s_count 1))
(up! xmas_count inc))
()
]
[_ ()]
)))

xmas_count)

;; Implementation

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

;; Expected results
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 18 2370))
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 9 1908))

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

()
33 changes: 33 additions & 0 deletions visp/examples/aoc2024/grid.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;; 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:

(include "./pos.visp")

(typedef Grid char[,])

(fn inline Grid_Height ([g: Grid])
(Array2D.length1 g))

(fn inline Grid_Width ([g: Grid])
(Array2D.length2 g))

(fn inline Grid_RowSlice ([g: Grid] [col: int])
(.[*, col] g))

(fn inline Grid_ColSlice ([g: Grid] [row: int])
(.[row, *] g))

(fn inline Grid_Contains? ([(x, y): Pos] [g: Grid])
(and
(and (>= x 0) (< x (Grid_Width g)))
(and (>= y 0) (< y (Grid_Height g)))))

(fn inline Grid_Get ([(x, y): Pos] [g: Grid])
(cond_
[(Grid_Contains? (x, y) g) (Some (.[y, x] g))]
[_ None]
))
10 changes: 10 additions & 0 deletions visp/examples/aoc2024/inputs/day4_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
43 changes: 43 additions & 0 deletions visp/examples/aoc2024/pos.visp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;; 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:

(typedef Pos int * int)

(fn inline Pos_Add ([(x, y): Pos] [(ox, oy): Pos]) ((+ x ox), (+ y oy)))
(fn inline Pos_LeftOf ([(x, y): Pos]) ((dec x), y))
(fn inline Pos_RightOf ([(x, y): Pos]) ((inc x), y))
(fn inline Pos_UpOf ([(x, y): Pos]) (x, (dec y)))
(fn inline Pos_DownOf ([(x, y): Pos]) (x, (inc y)))
(fn inline Pos_UpLeftOf ([(x, y): Pos]) ((dec x), (dec y)))
(fn inline Pos_UpRightOf ([(x, y): Pos]) ((inc x), (dec y)))
(fn inline Pos_DownLeftOf ([(x, y): Pos]) ((dec x), (inc y)))
(fn inline Pos_DownRightOf ([(x, y): Pos]) ((inc x), (inc y)))

(let POS_CARDINALS [|
(0, -1)
(-1, 0)
(1, 0)
(0, 1)
|])

(let POS_OFFSETS [|
(-1, -1)
(0, -1)
(1, -1)
(-1, 0)
(1, 0)
(-1, 1)
(0, 1)
(1, 1)
|])

(let POS_DIAGONALS [|
(-1, -1)
(1, -1)
(-1, 1)
(1, 1)
|])
Loading