In [1]:
input = '''jio a, +19
inc a
tpl a
inc a
tpl a
inc a
tpl a
tpl a
inc a
inc a
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
jmp +23
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
inc a
tpl a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
tpl a
inc a
jio a, +8
inc b
jie a, +4
tpl a
inc a
jmp +2
hlf a
jmp -7'''
s = {'a': 1, 'b': 0}
def hlf(g):
s[g['r']] /= 2
return 1
def tpl(g):
s[g['r']] *= 3
return 1
def inc(g):
s[g['r']] += 1
return 1
def jmp(g):
return int(g['d'])
def jie(g):
if s[g['r']] % 2 == 0:
return int(g['d'])
return 1
def jio(g):
if s[g['r']] == 1:
return int(g['d'])
return 1
r = [('hlf (?P<r>.)', hlf),
('tpl (?P<r>.)', tpl),
('inc (?P<r>.)', inc),
('jmp ((?P<d>[+-]\d+))', jmp),
('jie (?P<r>.), ((?P<d>[+-]\d+))', jie),
('jio (?P<r>.), ((?P<d>[+-]\d+))', jio)]
import re
def run(code):
i = 0
while i < len(code):
for m, f in r:
x = re.match(m, code[i])
if x:
found = True
i += f(x.groupdict())
break
assert found
run(input.split('\n'))
print s
In [2]:
input = '''1
2
3
7
11
13
17
19
23
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113'''
p = [int(s) for s in input.split('\n')]
W = sum(p) / 4
dp = [None for _ in range(W + 1)]
for x in p:
for i in reversed(range(1, W + 1 - x)):
if dp[i] is not None:
dp[x + i] = dp[i] + 1 if dp[x + i] is None else min(dp[i] + 1, dp[x + i])
dp[x] = 1
print dp[W]
In [3]:
def f(l, s):
n = max(s, max(l)) + 1
dp = [False for _ in range(n)]
for x in l:
for i in reversed(range(n - x)):
dp[x + i] = dp[i] or dp[x + i]
dp[x] = True
return dp[s]
In [4]:
import itertools as it
ret = None
for tidx in it.combinations(range(len(p)), dp[W]):
l = [p[i] for i in tidx]
if sum(l) == W:
t = [p[i] for i in range(len(p)) if i not in tidx]
if f(t, W):
qe = reduce(lambda x, y: x * y, l)
ret = qe if ret is None else min(ret, qe)
print ret
In [18]:
# Enter the code at row 2978, column 3083.
def f(r, c):
if 1 < r:
return r - 1, c + 1
return c + 1, 1
def g(x):
return (x * 252533) % 33554393
r, c = 2978, 3083
# r, c = 6, 6
v = 20151125
x, y = 1, 1
while (x, y) != (r, c):
x, y = f(x, y)
v = g(v)
print v
# 18361852
In [ ]: