read problem inputs from stdin

This commit is contained in:
Xiretza 2020-12-06 12:02:35 +01:00
parent 63c6f24ef5
commit c5d334348a
Signed by: xiretza
GPG key ID: 17B78226F7139993
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)))) return product(next(filter(lambda xs: sum(xs) == 2020, itertools.permutations(nums, r=arity))))
if __name__ == '__main__': if __name__ == '__main__':
with open(sys.argv[1], 'r') as f: nums = [int(n) for n in sys.stdin.readlines()]
nums = [int(n) for n in f.readlines()]
print(find_summing(2, nums)) print(find_summing(2, nums))
print(find_summing(3, nums)) print(find_summing(3, nums))

View file

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

View file

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

View file

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

View file

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

View file

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