blob: 2843dcd80b696c807ce2ab569ed6ac33e01f043a (
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
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#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)
|