Skip to content

Commit

Permalink
feat(aoc2024): day5 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 5, 2024
1 parent a80a746 commit e33af7d
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions visp/examples/aoc2024/day5.visp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
)

(fn rec Rules_IsBeforeAllOf [(before: int32) (rest: PageUpdate) (rules: Rules)]

(match (Map.tryFind before rules)
[(Some afters)
;;
Expand All @@ -90,38 +89,48 @@
(List.forall #(
not (Rules_IsBeforeAllOf %1 (| before |) rules)
)))
]
)
)
]))

(fn UpdateInCorrectOrder [(upd: PageUpdate) (rules: Rules)]
(fn rec loop ([up: PageUpdate] [rules: Rules] (correct: bool))
(cond_
[(not correct) correct]
[_
(match up
[(cur :: rest)
;; todo
;; (printfn "checking %A and %A" cur rest)

(loop rest rules (Rules_IsBeforeAllOf cur rest rules))
]
[(cur :: rest) (loop rest rules (Rules_IsBeforeAllOf cur rest rules)) ]
[[] correct]
) ]))
;; (->> upd
;; (List.pairwise)
;; (List.forall #(begin
;; (let (lhs, rhs) %1)

;; false
;; )))
)]))
(loop upd rules true))

(fn Update_PickMiddle [(upd: PageUpdate)]
(let len (/ (List.length upd) 2))
;; (printfn "valid %A %A" upd len)
(List.item len upd))

(fn Update_Fix [(rules: Rules) (upd: PageUpdate)]
;; (printfn "valid %A %A" upd len)
(fn rec loop ([up: PageUpdate] [rules: Rules] [fixed: PageUpdate])
(match up
[[] (List.rev fixed)]
[(cur :: [])
(-> (cons cur fixed) (List.rev))
]
[(cur :: next :: rest)
(cond_
[(Rules_IsBeforeAllOf cur (cons next rest) rules)
(loop (cons next rest) rules (cons cur fixed))
]
[(Rules_IsBeforeAllOf next (cons cur rest) rules)
(loop (cons cur rest) rules (cons next fixed))
]
[_ (loop (List.concat (| rest (| cur next |)|)) rules fixed)]
)
]
)
)

(loop upd rules (||)))

(fn Part1 (parsedInput)
;; (printfn "%A" parsedInput)
;; Implement part1
Expand All @@ -132,15 +141,19 @@

(fn Part2 (parsedInput)
;; Implement part2
0)
(->> (+updates parsedInput)
(List.filter #(not (UpdateInCorrectOrder %1 (+rules parsedInput))))
(List.map (Update_Fix (+rules parsedInput)))
(List.map Update_PickMiddle)
(List.sum)))

;; Implementation

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

;; Expected results
(let PART1_EXPECTED_RESULT (if IS_EXAMPLE 143 4637))
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE -1 -1))
(let PART2_EXPECTED_RESULT (if IS_EXAMPLE 123 6370))

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

0 comments on commit e33af7d

Please sign in to comment.