fn get_score(const x y : int, a : array(byte, [x, y]), i j : int) : int [ var score := 0; var t := mktuple3(a[i, j], i, j); var set := treeset_from_list([ t ]); while true do [ var first := treeset_first(set); if first is n then break; var k := first.j; set := treeset_clear(set, k); if k.v1 = '9' then [ score += 1; continue; ] for d in [ [0, 1], [0, -1], [1, 0], [-1, 0] ] do [ var xp := k.v2 + d[0]; var yp := k.v3 + d[1]; if xp >= 0, xp < x, yp >= 0, yp < y, a[xp, yp] = k.v1 + 1 then set := treeset_set(set, mktuple3(a[xp, yp], xp, yp)); ] ] return score; ] fn main [ var lines := list_break_to_lines(read_lazy(h[0])); lines := list_flatten(lines); const x := len(lines[0]); const y := len(lines); var a := list_to_array([x, y], list_join(lines, "")); a := array_flatten(a); var score := 0; for i := 0 to x do [ for j := 0 to y do [ if a[i, j] = '0' then score += get_score(a, i, j); ] ] write(h[1], ntos(score) + nl); ]