Bladeren bron

🧹

master
Roderic Day 5 jaren geleden
bovenliggende
commit
96a67e46b9
13 gewijzigde bestanden met toevoegingen van 35 en 27 verwijderingen
  1. +1
    -0
      makefile
  2. +2
    -4
      requirements.txt
  3. +3
    -2
      y2018/p17.py
  4. +3
    -1
      y2019/intcode.py
  5. +2
    -2
      y2019/p02.py
  6. +6
    -4
      y2019/p04.py
  7. +2
    -1
      y2019/p05.py
  8. +1
    -1
      y2019/p06.py
  9. +12
    -9
      y2019/p07.py
  10. +1
    -1
      y2019/p08.py
  11. +1
    -1
      y2019/p09.py
  12. +0
    -1
      y2019/p10.py
  13. +1
    -0
      y2019/p11.py

+ 1
- 0
makefile Bestand weergeven

@@ -3,6 +3,7 @@ DATA = $(shell echo $(FILE) | sed -e s/\.py/\.dat/)
PYTHONPATH=.

main: venv/
# @venv/bin/flake8 --exclude=venv/
@touch $(DATA)
@cat $(DATA) | venv/bin/python -u $(FILE)


+ 2
- 4
requirements.txt Bestand weergeven

@@ -1,4 +1,2 @@
pypng
numpy
scipy
scikit-image
flake8
flake8-import-order

+ 3
- 2
y2018/p17.py Bestand weergeven

@@ -10,7 +10,7 @@ def read_in():
_ = re.sub(r'(\d+)\.\.(\d+)', r'range(\1, \2 + 1)', _)
_ = re.sub(r'=(\d+)', r'=[\1]', _)
exec(_, globals())
grid.update({(X, Y): '#' for X in x for Y in y})
grid.update({(X, Y): '#' for X in x for Y in y}) # noqa
return grid


@@ -49,7 +49,8 @@ def drain(end):
x, y = stack.pop()
grid[x, y] = '|'

if grid[x - 1, y] == '|' and grid[x + 1, y] == ' ' and grid[x, y + 1] != '|':
left, right, below = grid[x - 1, y], grid[x + 1, y], grid[x, y + 1]
if all([right == ' ', left == '|', below != '|']):
flow((x + 1, y))

for dx, dy in [(-1, 0), (1, 0), (0, -1)]:

+ 3
- 1
y2019/intcode.py Bestand weergeven

@@ -28,6 +28,9 @@ def get_parameters(ns, pos, modes, N, writes, relbase):


def compute(ns, in_iter):
def consume(N, writes=''):
return get_parameters(ns, pos, modes, N, writes, relbase)

if isinstance(ns, str):
ns = parse(ns)
if isinstance(in_iter, int):
@@ -35,7 +38,6 @@ def compute(ns, in_iter):

pos = 0
relbase = 0
consume = lambda n, writes='': get_parameters(ns, pos, modes, n, writes, relbase)

while True:
op = ns[pos] % 100

+ 2
- 2
y2019/p02.py Bestand weergeven

@@ -1,6 +1,6 @@
import itertools
import re
import sys

from intcode import compute, parse


@@ -16,4 +16,4 @@ text = sys.stdin.read()
print(check(12, 2))
for noun, verb in itertools.product(range(100), repeat=2):
if check(noun, verb) == 19690720:
print(100 * noun + verb)
print(100 * noun + verb)

+ 6
- 4
y2019/p04.py Bestand weergeven

@@ -1,6 +1,8 @@
import sys
import itertools
from itertools import groupby

strings = [str(n) for n in range(347312, 805915 + 1)]
print(sum(list(s) == sorted(s) and any(len(list(vs)) >= 2 for k, vs in itertools.groupby(s)) for s in strings))
print(sum(list(s) == sorted(s) and any(len(list(vs)) == 2 for k, vs in itertools.groupby(s)) for s in strings))

A, B = [int(n) for n in sys.stdin.read().split('-')]
strings = [str(n) for n in range(A, B + 1)]
print(sum(list(s) == sorted(s) and any(len(list(vs)) >= 2 for k, vs in groupby(s)) for s in strings)) # noqa
print(sum(list(s) == sorted(s) and any(len(list(vs)) == 2 for k, vs in groupby(s)) for s in strings)) # noqa

+ 2
- 1
y2019/p05.py Bestand weergeven

@@ -1,5 +1,6 @@
import sys
from intcode import compute, parse

from intcode import compute


tests = [

+ 1
- 1
y2019/p06.py Bestand weergeven

@@ -1,5 +1,5 @@
import sys
import collections
import sys


# build

+ 12
- 9
y2019/p07.py Bestand weergeven

@@ -1,5 +1,6 @@
import sys
import itertools
from itertools import chain, cycle, permutations

from intcode import compute


@@ -12,19 +13,20 @@ def solve(ns, phases):
out = list(compute(ns, iter([n, out])))[-1]
return out

print(max(solve(ns, phases) for phases in itertools.permutations(range(5))))

print(max(solve(ns, phases) for phases in permutations(range(5))))


def solve2(ns, phases):
feedback = []
iter_phases = iter(phases)
iter_feed = iter(feedback)
loop = itertools.cycle([
compute(ns, itertools.chain([next(iter_phases), 0], iter_feed)),
compute(ns, itertools.chain([next(iter_phases)], iter_feed)),
compute(ns, itertools.chain([next(iter_phases)], iter_feed)),
compute(ns, itertools.chain([next(iter_phases)], iter_feed)),
compute(ns, itertools.chain([next(iter_phases)], iter_feed)),
loop = cycle([
compute(ns, chain([next(iter_phases), 0], iter_feed)),
compute(ns, chain([next(iter_phases)], iter_feed)),
compute(ns, chain([next(iter_phases)], iter_feed)),
compute(ns, chain([next(iter_phases)], iter_feed)),
compute(ns, chain([next(iter_phases)], iter_feed)),
])
try:
for machine in loop:
@@ -32,4 +34,5 @@ def solve2(ns, phases):
except StopIteration:
return feedback[-1]

print(max(solve2(ns, phases) for phases in itertools.permutations(range(5, 10))))

print(max(solve2(ns, phases) for phases in permutations(range(5, 10))))

+ 1
- 1
y2019/p08.py Bestand weergeven

@@ -4,7 +4,7 @@ import sys
w, h = 25, 6
text = sys.stdin.read()

count = lambda m: lambda layer: sum(n == m for n in layer)
count = lambda m: lambda layer: sum(n == m for n in layer) # noqa

layers = list(zip(*[(int(n) for n in text.strip())] * w * h))
min_layer = min(layers, key=count(0))

+ 1
- 1
y2019/p09.py Bestand weergeven

@@ -1,8 +1,8 @@
import re
import sys

from intcode import compute


text = sys.stdin.read()
print(next(compute(text, 1)))
print(next(compute(text, 2)))

+ 0
- 1
y2019/p10.py Bestand weergeven

@@ -1,7 +1,6 @@
import itertools
import math
import sys
from functools import partial


def measure(start, other):

+ 1
- 0
y2019/p11.py Bestand weergeven

@@ -1,5 +1,6 @@
import collections
import sys

from intcode import compute



Laden…
Annuleren
Opslaan