fn c2d(b : byte) [ if b >= 'a', b <= 'z' then return b - 'a'; else if b >= '0', b <= '9' then return b - '0' + 26; else abort; ] fn g2n(b : bytes) : int := (36 * 36) * c2d(b[0]) + 36 * c2d(b[1]) + c2d(b[2]); option op [ op_nothing; op_and; op_or; op_xor; ] fn main [ var sections := list_break(list_break_to_lines(read_lazy(h[0])), ""); var state := array_fill(sint8, -1, [ ipower(36, 3) ]); for l in sections[0] do [ var g := g2n(l[ .. 3]); if l[5] = '0' then state[g] := 0; else if l[5] = '1' then state[g] := 1; else abort; ] var ops := array_fill(mktuple3(op.op_nothing, -1, -1), [ ipower(36, 3) ]); for l in sections[1] do [ var b := list_break_whitespace(l); var i1 := g2n(b[0]); var i2 := g2n(b[2]); var ir := g2n(b[4]); var o : op; if b[1] = "AND" then o := op.op_and; else if b[1] = "OR" then o := op.op_or; else if b[1] = "XOR" then o := op.op_xor; else abort; ops[ir] := mktuple3(o, i1, i2); ] while true do [ var did_something := false; for i := 0 to ipower(36, 3) do [ if ops[i].v1 is op_nothing then continue; if state[i] >= 0 then continue; if state[ops[i].v2] < 0 then continue; if state[ops[i].v3] < 0 then continue; var new_state : sint8; if ops[i].v1 is op_and then new_state := state[ops[i].v2] and state[ops[i].v3]; else if ops[i].v1 is op_or then new_state := state[ops[i].v2] or state[ops[i].v3]; else if ops[i].v1 is op_xor then new_state := state[ops[i].v2] xor state[ops[i].v3]; else abort; state[i] := new_state; did_something := true; ] if not did_something then break; ] var result := 0; var bit := 0; for i := 0 to ipower(36, 3) do [ if i div (36 * 36) <> 25 then continue; if state[i] < 0 then continue; if state[i] = 1 then result bts= bit; bit += 1; ] write(h[1], ntos(result) + nl); ]