From f33e746bed26d3f6bf4324ca44be051e84aa87db Mon Sep 17 00:00:00 2001 From: trwnh Date: Thu, 3 Dec 2020 22:29:19 -0600 Subject: [PATCH] some refactored python solutions --- 03/03_refactored.py | 12 ++++++++++++ 03/03_refactored2.py | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 03/03_refactored.py create mode 100644 03/03_refactored2.py diff --git a/03/03_refactored.py b/03/03_refactored.py new file mode 100644 index 0000000..6d705f9 --- /dev/null +++ b/03/03_refactored.py @@ -0,0 +1,12 @@ +with open("input.txt","r") as f: + tmap = f.read().split('\n') + +def trees(right, down, tmap=tmap): + count = 0 + for row, line in enumerate(tmap[::down]): # array slicing by step + if line[ row * right % len(tmap[0]) ] == "#": + count += 1 + return count + +print(f'Part 1: {trees(3,1)}') +print(f'Part 2: {trees(1,1)*trees(3,1)*trees(5,1)*trees(7,1)*trees(1,2)}') \ No newline at end of file diff --git a/03/03_refactored2.py b/03/03_refactored2.py new file mode 100644 index 0000000..628333c --- /dev/null +++ b/03/03_refactored2.py @@ -0,0 +1,6 @@ +with open("input.txt","r") as f: + tmap = f.read().split('\n') +def trees(dx, dy, tmap=tmap): + return sum( [ line[i*dx % len(line)] == "#" for i, line in enumerate(tmap[::dy])] ) # array stepping AND list comprehension +print(f'Part 1: {trees(3,1)}') +print(f'Part 2: {trees(1,1)*trees(3,1)*trees(5,1)*trees(7,1)*trees(1,2)}') \ No newline at end of file