fn solve_task(m : list(bytes)) : int [ var mj := list_join(m, ""); for i := 0 to len(mj) do if mj[i] < '0' or mj[i] > '9' then mj[i] := ' '; var n := map(list_break_whitespace(mj), ston); var x, y := n[4], n[5]; var cost := treemap_init(tuple2(int, int), int); var set := treeset_from_list([ mktuple3(0, 0, 0) ]); while treeset_is_nonempty(set) do [ var k := treeset_first(set).j; if k.v2 = x, k.v3 = y then return k.v1; set := treeset_clear(set, k); for c := 0 to 2 do [ var nx := k.v2 + n[0 + 2 * c]; var ny := k.v3 + n[1 + 2 * c]; if nx <= x, ny <= y then [ var cst := 3 - 2 * c; var xcost := treemap_search(cost, mktuple2(nx, ny)); if xcost is n or xcost.j > k.v1 + cst then [ if xcost is j then set := treeset_clear(set, mktuple3(xcost.j, nx, ny)); set := treeset_set(set, mktuple3(k.v1 + cst, nx, ny)); cost := treemap_insert(cost, mktuple2(nx, ny), k.v1 + cst); ] ] ] ] return 0; ] fn main [ var lines := list_break_to_lines(read_lazy(h[0])); var m := list_break(lines, ""); var tokens := 0; for task in m do tokens += solve_task(task); write(h[1], ntos(tokens) + nl); ]