From def813adf8ca5bf6f42edf9489ed07f32795355b Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Tue, 19 Aug 2025 10:13:19 +0200 Subject: feat: add WIP solution to day 5, part 1 --- 2024/OCaml/lib/day_05/part_01.ml | 79 ++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 20 deletions(-) (limited to '2024') diff --git a/2024/OCaml/lib/day_05/part_01.ml b/2024/OCaml/lib/day_05/part_01.ml index 92bb105..f32bef1 100644 --- a/2024/OCaml/lib/day_05/part_01.ml +++ b/2024/OCaml/lib/day_05/part_01.ml @@ -1,23 +1,62 @@ (* Day 5: Part 1 *) -(* - 01. Parse pages that need updating into list, e.g. "75,47,61,53,29" -> [ 75; 47; 61; 53; 29; ] - 02. Parse page ordering rules into DTOs, e.g. "47|53" -> {"before": 47, "after": 53} - 03. Find all rules that have pages from the parsed page list on both sides, e.g. "47|53" is valid but "97|47" and "47|13" are not - - filter down to "before" values that are in page list - - filter down to "after" values that are in page list - 04. Map across the page list and ensure that all rules for each page are upheld - 05. Map across the rules for the current page and ensure that all rules are upheld - - filter rules down to rules with "before" equal to current page - - walk the page list and ensure that all "after" values do no appear before the "before" value - - return true if all rules are valid for the current page - 06. Reduce result down to single boolean - 07. IF FALSE, move on to next page in page list - 08. IF TRUE, continue until all rules are validated - 09. IF ALL RULES ARE NOT VALID FOR PAGE LIST, move on to next page list - 10. IF ALL RULES ARE VALID FOR PAGE LIST, save middle number in page list - 11. ONCE ALL PAGE LISTS HAVE BEEN PROCESSED, add all saved middle numbers - 12. Return sum of middle numbers -*) +module StringMap = Map.Make (String) -let solve _ = () +let inverse_page_rule page_rule = + String.split_on_char '|' page_rule |> fun lst -> + match lst with [] -> "" | _ :: _ -> String.concat "|" (List.rev lst) + +(** [parse_aux acc page_rules] is a map of all invalid transitions as defined by + [page_rules] *) +let rec get_invalid_transitions_aux acc page_rules : unit StringMap.t = + match page_rules with + | [] -> acc + | h :: t -> + get_invalid_transitions_aux + (acc |> StringMap.add (inverse_page_rule h) ()) + t + +let get_invalid_transitions page_rules = + get_invalid_transitions_aux StringMap.empty page_rules + +(** [solve page_rules updates] is the solution for this puzzle, given the + [page_rules] and a list of [updates]. + + BELOW DOESN'T WORK: no guarantee that second page comes immediately after + the first + - pass in updates to solve + - make sliding window of 2 elements + - invert element order and concat + - check if key is in map from [parse page_rules], + - if yes, then it is an illegal transition + - if no, then add the list to a list for further processing *) +let solve page_rules _ = + let invalid_transitions = get_invalid_transitions page_rules in + () + +let _ = + solve + [ + "47|53"; + "97|13"; + "97|61"; + "97|47"; + "75|29"; + "61|13"; + "75|53"; + "29|13"; + "97|29"; + "53|29"; + "61|53"; + "97|53"; + "61|29"; + "47|13"; + "75|47"; + "97|75"; + "47|61"; + "75|61"; + "47|29"; + "75|13"; + "53|13"; + ] + [] -- cgit v1.2.3