-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc24.py
More file actions
executable file
·82 lines (67 loc) · 2.49 KB
/
Copy pathaoc24.py
File metadata and controls
executable file
·82 lines (67 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import sqlite3
import utils
import time
def execute(cursor, tokens, debug=False):
cmd = tokens[0]
op1 = tokens[1]
if cmd == "inp":
sub_vars = ["?" if var == op1 else var for var in ("x", "y", "z", "w")]
for i in range(1, 10):
cursor.execute(
f"""
insert into states(x, y, z, w, num)
select {', '.join(sub_vars)}, num * 10 + ?
from old_states
""",
(i, i),
)
else:
op2 = tokens[2]
if cmd == "add":
expr = f"{op1} + {op2}"
elif cmd == "mul":
expr = f"{op1} * {op2}"
elif cmd == "div":
expr = f"{op1} / {op2}"
elif cmd == "mod":
expr = f"{op1} % {op2}"
elif cmd == "eql":
expr = f"case when {op1} == {op2} then 1 else 0 end"
sub_vars = [f"{expr} as {var}" if var == op1 else f"{var} as {var}" for var in ("x", "y", "z", "w")]
cursor.execute(
f"""
insert into states(x, y, z, w, num)
select x as x, y as y, z as z, w as w, min(num) as num from (
select {', '.join(sub_vars)}, num
from old_states
) group by x, y, z, w
"""
)
def main():
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("create table states(x int, y int, z int, w int, num int)")
cursor.execute("insert into states (x, y, z, w, num) values (0, 0, 0, 0, 0)")
for idx, line in enumerate(utils.test_input()):
line = line.strip()
print(f"[{idx+1}]: {line}")
cursor.execute("drop table if exists old_states")
cursor.execute("alter table states rename to old_states")
cursor.execute("create table states(x int, y int, z int, w int, num int)")
start = time.time()
execute(cursor, line.split(" "), debug=True)
end = time.time()
if False:
cursor.execute("select x, y, z, w, num from states")
for x, y, z, w, num in cursor:
print(f" x: {x}, y: {y}, z: {z}, w: {w} / {num}")
cursor.execute("select max(_rowid_) from states limit 1")
(states,) = cursor.fetchone()
print(f"{states} states, {time.time() - start:.02f} seconds")
print()
cursor.execute("select min(num) from states where z = 0")
(answer,) = cursor.fetchone()
print(f"answer: {answer}")
if __name__ == "__main__":
main()