some refactored python solutions
This commit is contained in:
parent
3f4ade67a3
commit
f33e746bed
12
03/03_refactored.py
Normal file
12
03/03_refactored.py
Normal file
|
@ -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)}')
|
6
03/03_refactored2.py
Normal file
6
03/03_refactored2.py
Normal file
|
@ -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)}')
|
Loading…
Reference in a new issue