Roderic Day 3 år sedan
förälder
incheckning
a372b6acff
4 ändrade filer med 103 tillägg och 2 borttagningar
  1. +2
    -2
      toolkit.py
  2. +22
    -0
      y2015/p20.py
  3. +76
    -0
      y2015/p22.py
  4. +3
    -0
      y2021/p01.py

+ 2
- 2
toolkit.py Visa fil

@@ -112,5 +112,5 @@ if __name__ == '__main__':
builtins.re = re
rel = re.sub(r'.+(y\d+)/(p\d+).+', r'\1.\2', os.environ['FILE'])
mod = importlib.import_module(rel)
print(getattr(mod, 'ans1', None))
print(getattr(mod, 'ans2', None))
print('ans1', getattr(mod, 'ans1', None))
print('ans2', getattr(mod, 'ans2', None))

+ 22
- 0
y2015/p20.py Visa fil

@@ -0,0 +1,22 @@
from collections import defaultdict
from itertools import count


def divisors(N):
ds = {1, N}
for n in range(2, int(N**0.5) + 1):
if N % n == 0:
ds.update({n, N // n})
return ds


lim = int(text)
ans1, ans2 = None, None
n = 830_000
while ans1 is None or ans2 is None:
ds = divisors(n)
if ans1 is None and sum(ds) > lim // 10:
ans1 = n
if ans2 is None and sum(d for d in ds if n // d <= 50) > lim // 11:
ans2 = n
n += 1

+ 76
- 0
y2015/p22.py Visa fil

@@ -0,0 +1,76 @@
from collections import namedtuple
import itertools


def play(spell, state):
is_win = 0
hp, mp, boss_hp, boss_dmg, turns, mp_spent, is_hard = state
for turn in (spell, None):
# prelude
if turn is not None and is_hard:
hp -= 1
if hp <= 0:
is_win = -1
break
if 'r' in turns[-5:]:
mp += 101
if 'p' in turns[-6:]:
boss_hp -= 3
if boss_hp <= 0:
is_win = 1
break
# action
if turn == 'm':
mp_spent += 53
boss_hp -= 4
elif turn == 'd':
mp_spent += 73
boss_hp -= 2
hp += 2
elif turn == 's':
mp_spent += 113
if 's' in turns[-5:]:
is_win = -1
break
elif turn == 'p':
mp_spent += 173
if 'p' in turns[-5:]:
is_win = -1
break
elif turn == 'r':
mp_spent += 229
if 'r' in turns[-4:]:
is_win = -1
break
elif turn == None:
hp -= max(boss_dmg - 7 if 's' in turns[-6:] else boss_dmg, 1)
turns += (turn,)
# denouement
if mp_spent > mp:
is_win = -1
break
if boss_hp <= 0:
is_win = 1
break
if hp <= 0:
is_win = -1
break
return is_win, State(hp, mp, boss_hp, boss_dmg, turns, mp_spent, is_hard)


State = namedtuple('State', 'hp,mp,boss_hp,boss_dmg,turns,mp_spent,is_hard')
boss_hp, boss_dmg = map(int, re.findall(r'\d+', text))

pending, wins = [State(50, 500, boss_hp, boss_dmg, tuple(), 0, False)], set()
while not wins:
outcomes = [play(spell, state) for state in pending for spell in 'mdspr']
wins |= {state for is_win, state in outcomes if is_win == 1}
pending = [state for is_win, state in outcomes if is_win == 0]
ans1 = min(win.mp_spent for win in wins)

pending, wins = [State(50, 500, boss_hp, boss_dmg, tuple(), 0, True)], set()
while not wins:
outcomes = [play(spell, state) for state in pending for spell in 'mdspr']
wins |= {state for is_win, state in outcomes if is_win == 1}
pending = [state for is_win, state in outcomes if is_win == 0]
ans2 = min(win.mp_spent for win in wins)

+ 3
- 0
y2021/p01.py Visa fil

@@ -0,0 +1,3 @@
ns = [int(n) for n in text.splitlines()]
ans1 = sum(b > a for a, b in zip(ns, ns[1:]))
ans2 = sum(sum(w[:-1]) < sum(w[1:]) for w in zip(ns, ns[1:], ns[2:], ns[3:]))

Laddar…
Avbryt
Spara