summary refs log tree commit diff stats
path: root/day5.ml
blob: e0e1aa0da63d9d3a23ac1fc8fdf3986b398f9220 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#use "topfind";;
#thread;;
#require "core";;
#require "stdio";;

open Core
open Stdio

let program = In_channel.read_lines "day5.txt"
              |> List.map ~f:(fun x -> String.strip x |> int_of_string)

let execute insts part2 =
  let rec helper insts index steps part2 =
    if 0 > index || index > (Array.length insts - 1) then steps
    else
      let jump = insts.(index) in
      insts.(index) <- (if part2 && jump >= 3 then jump - 1 else jump + 1);
      helper insts (index + jump) (steps + 1) part2 in
  helper insts 0 0 part2

let () =
  let program1 = Array.of_list program in
  Printf.printf "%d\n" (execute program1 false);

  let program2 = Array.of_list program in
  Printf.printf "%d\n" (execute program2 true);