read problem inputs from stdin

This commit is contained in:
Xiretza 2020-12-06 12:02:35 +01:00
parent c5409429e7
commit 753a0e7034
6 changed files with 15 additions and 15 deletions

View file

@ -16,7 +16,6 @@ def find_summing(arity: int, nums: Iterable[int]) -> int:
return product(next(filter(lambda xs: sum(xs) == 2020, itertools.permutations(nums, r=arity))))
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
nums = [int(n) for n in f.readlines()]
nums = [int(n) for n in sys.stdin.readlines()]
print(find_summing(2, nums))
print(find_summing(3, nums))

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python
import re
import sys
from collections import Counter
r = re.compile(r'(\d+)-(\d+) (.): (.*)')
@ -15,8 +16,7 @@ def do_line(req_func, line):
return req_func(int(min_required), int(max_required), letter, password)
if __name__ == '__main__':
with open('input.txt', 'r') as f:
lines = list(f.readlines())
lines = list(sys.stdin.readlines())
for func in (requirements_met_1, requirements_met_2):
print(len(list(filter(lambda line: do_line(func, line), lines))))

View file

@ -2,14 +2,15 @@
set -eu
INPUT=$1
GHDLFLAGS="--std=08 --workdir=workdir"
mkdir -p workdir
ghdl remove $GHDLFLAGS
ghdl analyze $GHDLFLAGS parser.vhd verifier.vhd top.vhd sim.vhd
ghdl elab-run $GHDLFLAGS sim -gSTEP=1 -gFILENAME="../input.txt"
ghdl elab-run $GHDLFLAGS sim -gSTEP=2 -gFILENAME="../input.txt"
ghdl elab-run $GHDLFLAGS sim -gSTEP=1 < "$INPUT"
ghdl elab-run $GHDLFLAGS sim -gSTEP=2 < "$INPUT"
echo "Synthesized: "
@ -17,5 +18,5 @@ for step in 1 2; do
ghdl remove $GHDLFLAGS
ghdl synth $GHDLFLAGS -gCOUNTER_WIDTH=12 -gSTEP="$step" parser.vhd verifier.vhd top.vhd -e top > top_syn.vhd
ghdl analyze $GHDLFLAGS top_syn.vhd sim.vhd
ghdl elab-run $GHDLFLAGS sim -gSTEP="$step" -gFILENAME="../input.txt" --ieee-asserts=disable-at-0
ghdl elab-run $GHDLFLAGS sim -gSTEP="$step" --ieee-asserts=disable < "$INPUT"
done

View file

@ -6,14 +6,12 @@ use std.textio.all;
entity sim is
generic (
FILENAME : string := "input.txt";
COUNTER_WIDTH : positive := 12;
STEP : natural range 1 to 2
);
end entity;
architecture a of sim is
file file_handle : text open read_mode is FILENAME;
signal char_in : character;
signal clk, reset, is_record : std_logic;
signal num_verified : unsigned(COUNTER_WIDTH-1 downto 0);
@ -49,8 +47,8 @@ begin
cycle_clock;
lines_loop: loop
exit lines_loop when endfile(file_handle);
readline(file_handle, current_line);
exit lines_loop when endfile(input);
readline(input, current_line);
is_record <= '1';

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python
import sys
def count_trees(right, down, lines):
n = 0
for i, line in enumerate(lines[::down]):
@ -9,8 +11,7 @@ def count_trees(right, down, lines):
return n
if __name__ == '__main__':
with open('input.txt', 'r') as f:
lines = [line.strip() for line in f.readlines()]
lines = [line.strip() for line in sys.stdin.readlines()]
print(count_trees(3, 1, lines))

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python
import sys
def within(a, b, x):
return a <= x <= b
@ -48,8 +50,7 @@ def split_on(f, it):
yield current_part
if __name__ == '__main__':
with open('input.txt', 'r') as f:
lines = [line.strip() for line in f.readlines()]
lines = [line.strip() for line in sys.stdin.readlines()]
specs = list(' '.join(lines) for lines in split_on(lambda l: l == "", lines))