aoc-2022-ocaml/src/day6.ml
2023-10-09 14:14:13 -07:00

36 lines
780 B
OCaml

open Containers
module CharSet = Set.Make (Char)
let window = 4
let stream =
match Utils.lines_of_input 6 with
| [ line ] -> line
| _ -> failwith "more than one line in input"
;;
let find_start window =
let length = String.length stream in
let start_index =
List.Infix.(0 --^ (length - window))
|> List.drop_while (fun index ->
let chunk = String.take window (String.drop index stream) in
window <> (chunk |> String.to_list |> CharSet.of_list |> CharSet.cardinal))
|> List.hd
in
start_index + window
;;
let part1 () = find_start 4
let part2 () = find_start 14
let%expect_test "Day 6.1" =
Printf.printf "%d" @@ part1 ();
[%expect {| 1833 |}]
;;
let%expect_test "Day 6.2" =
Printf.printf "%d" @@ part2 ();
[%expect {| 3425 |}]
;;