瀏覽代碼

🦔

master
Roderic Day 5 年之前
父節點
當前提交
7a752d817d
共有 2 個檔案被更改,包括 35 行新增2 行删除
  1. +3
    -2
      y2019/intcode.py
  2. +32
    -0
      y2019/p11.py

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





def parse(string): def parse(string):
ns = [int(n) for n in re.findall(r'-?\d+', string)]
memory = collections.defaultdict(int) memory = collections.defaultdict(int)
memory.update(enumerate(int(n) for n in re.findall(r'-?\d+', string)))
return dict(memory)
memory.update(enumerate(ns))
return memory




def get_parameters(ns, pos, modes, N, writes, relbase): def get_parameters(ns, pos, modes, N, writes, relbase):

+ 32
- 0
y2019/p11.py 查看文件

import collections
import sys
from intcode import compute


def get_panels(ns, start):
feedback = [start]
iter_in = iter(feedback)
robot = compute(ns, iter_in)

pos, ori = 0, -1j
panels = collections.defaultdict(int)
for paint, instruction in zip(robot, robot):
panels[pos] = paint
ori *= 1j if instruction else -1j
pos += ori
feedback.append(panels[pos])
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()
print(len(get_panels(text, 0)))
visualize(get_panels(text, 1), brush=' #')

Loading…
取消
儲存