#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Pascal%27s_triangle/Puzzle
#

# set up triangle
var rows = 5;
var tri = rows.of {|i| i+1 -> of { Hash(x => 0, z => 0, v => 0, rhs => nil) } }
tri[0][0]{:rhs} = 151;
tri[2][0]{:rhs} = 40;
tri[4][0]{:x} = 1;
tri[4][1]{:v} = 11;
tri[4][2]{:x} = 1;
tri[4][2]{:z} = 1;
tri[4][3]{:v} = 4;
tri[4][4]{:z} = 1;

# aggregate from bottom to top
for row in (tri.end -> downto(1)) {
    for col in (tri[row-1].range) {
        [:x, :z, :v].each { |key|
            tri[row-1][col]{key} = (tri[row][col]{key} + tri[row][col+1]{key})
        }
    }
}

# find equations
var eqn = gather {
    for r in tri {
        for c in r {
            take([c{:x}, c{:z}, c{:rhs} - c{:v}]) if defined(c{:rhs})
        }
    }
}

# print equations
say "Equations:";
say " x +  z = y";
for x,z,y in eqn { say "#{x}x + #{z}z = #{y}" }

# solve
var f = (eqn[0][1] / eqn[1][1]);
for i in (0..2) { eqn[0][i] -= (f * eqn[1][i]) }
f = (eqn[1][0] / eqn[0][0]);
for i in (0..2) { eqn[1][i] -= (f * eqn[0][i]) }

# print solution
say "Solution:";
var x = (eqn[0][2] / eqn[0][0]);
var z = (eqn[1][2] / eqn[1][1]);
var y = (x + z);
say "x=#{x}, y=#{y}, z=#{z}";
