浏览代码

🕷

master
Roderic Day 5 年前
父节点
当前提交
bd5aefc87c
共有 8 个文件被更改,包括 44 次插入45 次删除
  1. +3
    -2
      makefile
  2. +12
    -0
      toolkit.py
  3. +3
    -14
      y2018/p17.py
  4. +2
    -3
      y2019/p01.py
  5. +5
    -2
      y2019/p08.py
  6. +3
    -10
      y2019/p11.py
  7. +4
    -2
      y2019/p13.py
  8. +12
    -12
      y2019/p14.py

+ 3
- 2
makefile 查看文件

FILE = $(shell find . -name p??.py -type f | xargs ls -rt | tail -n 1)
FILE = $(shell find . -path "./y????/p??.py" -type f | xargs ls -rt | tail -n 1)
DATA = $(shell echo $(FILE) | sed -e s/\.py/\.dat/) DATA = $(shell echo $(FILE) | sed -e s/\.py/\.dat/)
PYTHONPATH=.
PYTHONPATH = .
export


main: venv/ main: venv/
@touch $(DATA) @touch $(DATA)

+ 12
- 0
toolkit.py 查看文件

def render(grid, brush):
if isinstance(brush, str):
brush = {i: c for i, c in enumerate(brush)}
xmin, *_, xmax = sorted(int(p.real) for p in grid)
ymin, *_, ymax = sorted(int(p.imag) for p in grid)
brush[None] = ' '
rendered = ''
for y in range(ymin, ymax + 1):
for x in range(xmin, xmax + 1):
rendered += brush[grid.get(complex(x, y))]
rendered += '\n'
return rendered

+ 3
- 14
y2018/p17.py 查看文件

_ = _.replace(',', ';') _ = _.replace(',', ';')
_ = re.sub(r'(\d+)\.\.(\d+)', r'range(\1, \2 + 1)', _) _ = re.sub(r'(\d+)\.\.(\d+)', r'range(\1, \2 + 1)', _)
_ = re.sub(r'=(\d+)', r'=[\1]', _) _ = re.sub(r'=(\d+)', r'=[\1]', _)
exec(_, globals())
grid.update({(X, Y): '#' for X in x for Y in y}) # noqa
temp = {}
exec(_, temp)
grid.update({(X, Y): '#' for X in temp['x'] for Y in temp['y']})
return grid return grid




def write_out(grid, path):
xmin, *_, xmax = sorted(x for x, y in grid)
ymin, *_, ymax = sorted(y for x, y in grid)
text = '\n'.join(
''.join(grid[x, y] for x in range(xmin - 5, xmax + 5))
for y in range(ymin, ymax + 1)
)
with open('p17out.dat', 'w') as fp:
fp.write(text)


def flow(grid, start, ymax): def flow(grid, start, ymax):
stack = [start] stack = [start]
while stack: while stack:
counter = collections.Counter(grid.values()) counter = collections.Counter(grid.values())
print(counter['~'] + counter['|']) print(counter['~'] + counter['|'])
print(counter['~']) print(counter['~'])
write_out(grid, 'y2018/p17out.dat')

+ 2
- 3
y2019/p01.py 查看文件

return [m] + handle(m) if m > 0 else [] return [m] + handle(m) if m > 0 else []




if __name__ == '__main__':
print(sum(handle(n)[0] for n in ns))
print(sum(sum(handle(n)) for n in ns))
print(sum(handle(n)[0] for n in ns))
print(sum(sum(handle(n)) for n in ns))

+ 5
- 2
y2019/p08.py 查看文件

import sys import sys


from toolkit import render



w, h = 25, 6 w, h = 25, 6
text = sys.stdin.read() text = sys.stdin.read()
min_layer = min(layers, key=count(0)) min_layer = min(layers, key=count(0))
print(count(1)(min_layer) * count(2)(min_layer)) print(count(1)(min_layer) * count(2)(min_layer))


px = (' #'[next(n for n in stack if n != 2)] for stack in zip(*layers))
print('\n'.join(''.join(next(px) for _ in range(w)) for _ in range(h)))
px = (next(n for n in stack if n != 2) for stack in zip(*layers))
grid = {complex(x, y): next(px) for y in range(h) for x in range(w)}
print(render(grid, ' #'))

+ 3
- 10
y2019/p11.py 查看文件



from intcode import compute from intcode import compute


from toolkit import render



def get_panels(ns, start): def get_panels(ns, start):
feedback = [start] feedback = [start]
return panels return panels




def visualize(panels, brush):
xmin, *_, xmax = sorted(int(p.real) for p in panels)
ymin, *_, ymax = sorted(int(p.imag) for p in panels)
for y in range(ymin, ymax + 1):
for x in range(xmin, xmax + 1):
print(brush[panels[complex(x, y)]], end='')
print()


text = sys.stdin.read() text = sys.stdin.read()
print(len(get_panels(text, 0))) print(len(get_panels(text, 0)))
visualize(get_panels(text, 1), brush=' #')
print(render(get_panels(text, 1), brush=' #'))

+ 4
- 2
y2019/p13.py 查看文件

import collections import collections
import sys import sys


from intcode import compute
from intcode import compute, parse




def arkanoid(text): def arkanoid(text):
grid, _ = arkanoid(text) grid, _ = arkanoid(text)
print(collections.Counter(grid.values())[2]) print(collections.Counter(grid.values())[2])


_, score = arkanoid('2' + text[1:])
ns = parse(text)
ns[0] = 2
_, score = arkanoid(ns)
print(score) print(score)

+ 12
- 12
y2019/p14.py 查看文件





text = sys.stdin.read() text = sys.stdin.read()
to_get = {}
cookbook = {}
for line in text.strip().splitlines(): for line in text.strip().splitlines():
for i, (qty, name) in enumerate(re.findall(r'(\d+) ([A-Z]+)', line)[::-1]): for i, (qty, name) in enumerate(re.findall(r'(\d+) ([A-Z]+)', line)[::-1]):
if i == 0: if i == 0:
output = name output = name
to_get[output] = {output: -int(qty)}
cookbook[output] = {output: -int(qty)}
else: else:
to_get[output][name] = int(qty)
cookbook[output][name] = int(qty)




def fuel_to_ore(wanted):
required = collections.Counter({'FUEL': wanted})
pending = required.copy()
def fuel_to_ore(state):
state = collections.Counter(state)
pending = state.copy()
while pending: while pending:
pending = {k: v for k, v in required.items() if k != 'ORE' and v > 0}
pending = {k: v for k, v in state.items() if k in cookbook and v > 0}
for out, out_qty in pending.items(): for out, out_qty in pending.items():
min_qty = -to_get[out][out]
min_qty = -cookbook[out][out]
n_times = math.ceil(out_qty / min_qty) n_times = math.ceil(out_qty / min_qty)
required.update({k: v * n_times for k, v in to_get[out].items()})
return required['ORE']
state.update({k: v * n_times for k, v in cookbook[out].items()})
return state




print(fuel_to_ore(1))
print(fuel_to_ore({'FUEL': 1})['ORE'])




def bsearch(fn, goal, lo, hi): def bsearch(fn, goal, lo, hi):
return lo return lo




print(bsearch(fuel_to_ore, 1E12, 1, 10_000_000))
print(bsearch(lambda n: fuel_to_ore({'FUEL': n})['ORE'], 1E12, 1, 10_000_000))

正在加载...
取消
保存