47 lines
1.1 KiB
OCaml
47 lines
1.1 KiB
OCaml
(*The spreadsheet consists of rows of apparently-random numbers.*)
|
|
(*To make sure the recovery process is on the right track, they*)
|
|
(*need you to calculate the spreadsheet's checksum. For each*)
|
|
(*row, determine the difference between the largest value and*)
|
|
(*the smallest value; the checksum is the sum of all of these*)
|
|
(*differences.*)
|
|
|
|
(*For example, given the following spreadsheet:*)
|
|
|
|
(*5 1 9 5*)
|
|
(*7 5 3*)
|
|
(*2 4 6 8*)
|
|
|
|
(*The first row's largest and smallest values are 9 and 1, and their*)
|
|
(*difference is 8.*)
|
|
(*The second row's largest and smallest values are 7 and 3, and their*)
|
|
(*difference is 4.*)
|
|
(*The third row's difference is 6.*)
|
|
|
|
open Core
|
|
open Batteries
|
|
|
|
let open_file (filename : string) =
|
|
File.lines_of filename
|
|
|
|
let get_max_min_diff lst =
|
|
(List.max lst) - (List.min lst)
|
|
|
|
let parse_line line =
|
|
let
|
|
split = String.split_on_char '\t'
|
|
in
|
|
split line
|
|
|> List.map int_of_string
|
|
|> get_max_min_diff
|
|
|
|
let parse_input input =
|
|
Enum.map parse_line input
|
|
|> Enum.sum
|
|
|
|
|
|
let () =
|
|
"dec2_input"
|
|
|> open_file
|
|
|> parse_input
|
|
|> printf "%d"
|