Skip to content

Instantly share code, notes, and snippets.

@esehara
Last active June 1, 2021 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/87d0ab13da174db1c4d9387390d2099b to your computer and use it in GitHub Desktop.
Save esehara/87d0ab13da174db1c4d9387390d2099b to your computer and use it in GitHub Desktop.
type results = {
ys: list(list(int)),
xs: array(int),
};
let sum_xs_ys = (line, r) => {
let y_list = Str.split(Str.regexp(" +"), line) |> List.map(int_of_string);
let y_simple_sum = List.fold_left((+), 0, y_list);
{
ys: r.ys @ [List.map((s) => y_simple_sum - s, y_list)],
xs: Array.mapi((i, x) => x + r.xs[i], Array.of_list(y_list))
}
};
let plus_xs_ys: results => list(list(int)) = (r) => {
List.map((xs) => List.mapi((i, x) => r.xs[i] + x, xs), r.ys);
};
let print_llist: list(list(int)) => unit = (rs) => {
List.iter((r) => {
print_string(String.concat(" ", List.map(string_of_int, r)));
print_newline()
}, rs)
}
let solve = () => {
let args = Str.split(Str.regexp(" +"), read_line())
|> List.map(int_of_string)
|> Array.of_list
let rec sum_lines = (line_n, r) => {
switch (line_n) {
| 0 => r
| _ => sum_lines(line_n - 1, sum_xs_ys(read_line(), r))
};
};
sum_lines(args[0], {ys: [], xs: Array.make(args[1])(0)})
|> plus_xs_ys
|> print_llist
}
solve();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment