1
0
Fork 0
advent-of-code-2022/ocaml/6.ml

36 lines
952 B
OCaml

#use "lib/utils.ml" ;;
type cursor = {ring: char List.t; pos: int} ;;
let find_marker seq width =
let find_marker' cur i c =
if cur.pos > 0 then cur
else
let rec chars_repeat = function
| [] -> false
| hd :: tl -> List.exists ((=) hd) tl || chars_repeat tl
in
let ring =
if List.length cur.ring = width
then List.tl cur.ring @ [c]
else cur.ring @ [c]
in
let pos =
if List.length cur.ring = width && not(chars_repeat cur.ring)
then i + 1
else 0
in
{ring = ring; pos = pos}
in
let cur = {ring = []; pos = 0} in
let cur = Seq.fold_lefti find_marker' cur seq in
cur.pos
in
let contents = read_file "inputs/6.txt" in
let reader = String.to_seq contents in
let marker_pos = find_marker reader 4 in
let message_pos = find_marker reader 14 in
Printf.printf "Solution 1: %d\n" marker_pos;
Printf.printf "Solution 2: %d\n" message_pos