Unable to load image

Day 21 AoC: use Z3 b-words

Post code, boast about it.

13
Jump in the discussion.

No email address required.

    def eval(s, zs = None):
        nonlocal humn
        if zs and s == 'humn':
            humn = z3.Int('humn')
            return humn

        x = data[s]
        if isinstance(x, int):
            return x
        if isinstance(x, str):
            data[s] = eval(x, zs)
            return data[s]
        a, op, b = x
        a = eval(a, zs)
        b = eval(b, zs)
        data[s] = a, op, b
        if zs and s == 'root':
            op = '='
        op = {
            '+': operator.add,
            '-': operator.sub,
            '*': operator.mul,
            '/': lambda xx, yy: xx / yy if zs else xx // yy,
            '=': operator.eq,
        }[op]
        if zs:
            if (isinstance(a, int) and isinstance(b, int)):
                return op(a, b)
            print(s, x)
            var = z3.Int(s)
            zs.add(var == op(a, b))
            return var

        return op(a, b)

    if not second:
        return eval('root')

    import z3
    zs = z3.Solver()
    root = eval('root', zs)
    zs.add(root == True)
    assert zs.check() == z3.sat
    print(zs.model())
    return zs.model().eval(humn).as_long()

I told you bros, learn Z3. It even has typescript bindings! Tho I wasted so much time trying to figure out why it doesn't support this newfangled '//' operator. Also I was puzzled for a long time why it gives me 0 as an acceptable answer when I don't tell it that root must be true LMAO.

Jump in the discussion.

No email address required.

I must say that rdrama is the last place I expected to see someone advocating for the use of an SMT solver. :parrotscience:

Jump in the discussion.

No email address required.

SMT solver

i just want one that proves my program halts for sure.

Jump in the discussion.

No email address required.

Try SPARK then. Termination proofs don't play well with unbounded resources.

Jump in the discussion.

No email address required.

i mean. my computer's resources aren't unbounded. i wouldn't mind if they were, but they aren't

Jump in the discussion.

No email address required.

tbh the whole thing, the number of people here who even participated in AoC is mindblowing and weird.

Jump in the discussion.

No email address required.



Now playing: Funky the Main Monkey (DKC2).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.