Last active
June 1, 2021 08:12
-
-
Save esehara/87d0ab13da174db1c4d9387390d2099b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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