blob: 10d5e63ed767b0d4c57a541e1050e3328e11623e (
plain) (
tree)
|
|
#use "topfind";;
#thread;;
#require "core";;
#require "stdio";;
open Core
open Stdio
let memory = In_channel.read_all "day6.txt"
|> String.strip |> String.split ~on:'\t'
|> Array.of_list |> Array.map ~f:int_of_string
let len = Array.length memory
let seen = Caml.Hashtbl.create 5000
let array_max xs =
let f index (maxInd, maxVal) value =
if value > maxVal then (index, value) else (maxInd, maxVal) in
Array.foldi ~init:(0, 0) ~f:f xs
let rec redist xs n i =
match n with
| 0 -> ()
| _ ->
let pos = (i + 1) mod len in
xs.(pos) <- xs.(pos) + 1;
redist xs (n-1) pos
let rec cycle count banks =
match Caml.Hashtbl.find_opt seen banks with
| Some x ->
Printf.printf "%d\n" count;
Printf.printf "%d\n" (count - x)
| None ->
Caml.Hashtbl.add seen (Array.copy banks) count;
let mi, mv = array_max banks in
banks.(mi) <- 0;
redist banks mv mi;
cycle (count+1) banks
let () = cycle 0 memory
|