aoc2022/bin/d6/main.ml

21 lines
763 B
OCaml

let has_repeating_char s =
let chars = List.init (String.length s) (String.get s) in
let rec aux = function
| x :: xs -> List.exists (fun q -> q = x) xs || aux xs
| [] -> false
in aux chars
exception NotFound
let calculate_offset offset input =
let buf = String.sub input 0 offset in
let rec aux idx buf input =
if not (has_repeating_char buf) then idx + offset - 1
else if idx + offset = String.length input then raise NotFound
else aux (idx + 1) (String.sub input idx offset) input in
aux 1 buf input
let input = Utils.read_file "bin/d6/input.txt" |> List.hd ;;
"\n1: " ^ (string_of_int (calculate_offset 4 input)) ^
"\n2: " ^ (string_of_int ((calculate_offset 14 input)))
|> print_endline