diff options
Diffstat (limited to 'lib/day03.ml')
| -rw-r--r-- | lib/day03.ml | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/lib/day03.ml b/lib/day03.ml index 4e535b2..a5fab4a 100644 --- a/lib/day03.ml +++ b/lib/day03.ml @@ -14,18 +14,16 @@ let parse_data ch = - Keep track of the number [k = |bat| - n] of banks that can be discarded Then process banks sequentially according to the following steps: - - While the stack is not empty, [k > 0], and the current bank is bigger - than the one at the top of the stack, pop a bank from the stack and - reduce [k] by 1 + - While the stack is not empty, [k > 0], and the current bank is bigger than + the one at the top of the stack, pop a bank from the stack and reduce [k] + by 1 - Insert the bank into the stack if the size of the stack is less then [n] - Otherwise, don't insert the bank, and reduce [k] by 1 At the end the stack contains the relevant banks, in reverse order. - Since each bank is inserted, and removed, at most once from the - stack, and all operations are [O(1)] the complexity is - [O(|bat|)]. - *) + Since each bank is inserted, and removed, at most once from the stack, and + all operations are [O(1)] the complexity is [O(|bat|)]. *) let joltage n bat = let rec fn s k m l = match Seq.uncons l with @@ -34,13 +32,10 @@ let joltage n bat = let digit c = int_of_char c - zero in List.rev_map digit s |> List.fold_left (fun a v -> (a * 10) + v) 0 | Some (h, t) -> ( - match s with - | sh :: st when k > 0 && sh < h -> - fn st (k - 1) (m - 1) l - | s when m < n -> - fn (h :: s) k (m + 1) t - | s -> - fn s (k - 1) m t ) + match s with + | sh :: st when k > 0 && sh < h -> fn st (k - 1) (m - 1) l + | s when m < n -> fn (h :: s) k (m + 1) t + | s -> fn s (k - 1) m t) in let k = String.length bat - n in fn [] k 0 (String.to_seq bat) @@ -52,5 +47,4 @@ let solve ch n = |> Printf.printf "%d\n" let part1 ch = solve ch 2 - let part2 ch = solve ch 12 |
