diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2022-03-05 12:02:25 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2022-03-05 12:02:25 +0100 |
| commit | 557a972b771355c7d9ad6d35181a0e2b605ff4c7 (patch) | |
| tree | 8432da187828c9731bbddfbfc85cc272866f9074 /julia/081-path_sum_two_ways.jl | |
| parent | 325a7d29a4bd4665c741aa0497eabc77a926844d (diff) | |
| download | project_euler-557a972b771355c7d9ad6d35181a0e2b605ff4c7.tar.gz project_euler-557a972b771355c7d9ad6d35181a0e2b605ff4c7.tar.bz2 project_euler-557a972b771355c7d9ad6d35181a0e2b605ff4c7.zip | |
problem 81 in julia
Diffstat (limited to 'julia/081-path_sum_two_ways.jl')
| -rw-r--r-- | julia/081-path_sum_two_ways.jl | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/julia/081-path_sum_two_ways.jl b/julia/081-path_sum_two_ways.jl new file mode 100644 index 0000000..77a90a4 --- /dev/null +++ b/julia/081-path_sum_two_ways.jl @@ -0,0 +1,34 @@ +# matrix = [ +# [131, 673, 234, 103, 18], +# [201, 96, 342, 965, 150], +# [630, 803, 746, 422, 111], +# [537, 699, 497, 121, 956], +# [805, 732, 524, 37, 331], +# ] + +matrix = open(f -> read(f, String), "data/081_matrix.txt") |> + split |> + m -> map(l -> map(x -> parse(Int, x), split(l, ",")), m) + +for (i, _) in enumerate(matrix) + for (j, _) in enumerate(matrix[i]) + if i == 1 && j == 1 + continue + end + if i == 1 + up = Inf + else + up = matrix[i - 1][j] + end + if j == 1 + left = Inf + else + left = matrix[i][j - 1] + end + matrix[i][j] += min(up, left) + end +end + +result = matrix[length(matrix)][length(matrix[1])] + +println(result) |
