blob: 2843dcd80b696c807ce2ab569ed6ac33e01f043a (
plain) (
tree)
|
|
#use "topfind";;
#thread;;
#require "core";;
#require "stdio";;
#require "csv";;
open Core
open Stdio
let spreadsheet = Csv.load ~separator:'\t' "day2.txt"
|> List.map ~f:(List.map ~f:int_of_string)
let minmax = List.fold_left
~init:(Int.max_value, Int.min_value)
~f:(fun (lmin, lmax) x -> (min x lmin, max x lmax))
let part1 l = List.map ~f:minmax l
|> List.map ~f:(fun (lmin, lmax) -> lmax - lmin)
|> List.fold_left ~f:(+) ~init:0
let rec dividend row =
let divisible a b =
a mod b = 0 || b mod a = 0 in
let divide a b =
if a mod b = 0 then a/b else b/a in
match row with
| [] -> 0
| h::t ->
match List.find t ~f:(divisible h) with
| Some x -> divide h x
| None -> dividend t
let part2 l = List.map ~f:dividend l
|> List.fold_left ~f:(+) ~init:0
let () =
Printf.printf "%d\n" (part1 spreadsheet);
Printf.printf "%d\n" (part2 spreadsheet)
|