Skip to content

Instantly share code, notes, and snippets.

@shaharz
Created December 3, 2017 01:42
Show Gist options
  • Save shaharz/6715917c6436b790c048642592f46790 to your computer and use it in GitHub Desktop.
Save shaharz/6715917c6436b790c048642592f46790 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - day 2
// part 1
read_file("Downloads/input.txt")
|> List.map(Str.split(Str.regexp("[ \t]+")))
|> List.map(List.map(int_of_string))
|> List.map(
(l) =>
List.fold_left(
((mn, mx), cur) => (min(mn, cur), max(mx, cur)),
(List.hd(l), List.hd(l)),
l
)
)
|> List.fold_left((acc, (mn, mx)) => acc + (mx - mn), 0);
// part 2
read_file("Downloads/input.txt")
|> List.map(Str.split(Str.regexp("[ \t]+")))
|> List.map(List.map(int_of_string))
|> List.map(
(l) =>
List.map(
(a) => (
a,
try (List.find((b) => a != b && a mod b == 0, l)) {
| Not_found => (-1)
}
),
l
)
|> List.fold_left((acc, (a, b)) => b == (-1) ? acc : a / b, 0)
)
|> List.fold_left((acc, x) => acc + x, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment