Skip to content

Commit cb1454b

Browse files
completed Day 3 part 2
1 parent 17b1d13 commit cb1454b

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

day3/README.txt

+15-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,18 @@ The locations you'd check in the above example are marked here with O where ther
4848
.#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# --->
4949
In this example, traversing the map using this slope would cause you to encounter 7 trees.
5050

51-
Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter?
51+
Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter?
52+
53+
--- Part Two ---
54+
Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all.
55+
56+
Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom:
57+
58+
Right 1, down 1.
59+
Right 3, down 1. (This is the slope you already checked.)
60+
Right 5, down 1.
61+
Right 7, down 1.
62+
Right 1, down 2.
63+
In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied together, these produce the answer 336.
64+
65+
What do you get if you multiply together the number of trees encountered on each of the listed slopes?

day3/test_toboggan.py

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def test_something(self):
2121
trees = toboggan.count_trees(self.route, (3,1))
2222
self.assertEqual(7, trees)
2323

24+
def test_count_trees_for_all_slopes(self):
25+
trees = toboggan.calculate(self.route, [(1,1), (3,1), (5,1), (7,1), (1,2)])
26+
self.assertEqual(336, trees)
27+
2428

2529
if __name__ == '__main__':
2630
unittest.main()

day3/toboggan.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import numpy
2+
13
TREE = '#'
24

35

6+
def calculate(route: str, slopes: list) -> int:
7+
trees = [count_trees(route, slope) for slope in slopes]
8+
return numpy.prod(trees)
9+
10+
411
def count_trees(route: str, slope: tuple) -> int:
512
parsed_route = parse(route)
613
x = 0
@@ -23,4 +30,5 @@ def parse(route: str) -> list:
2330

2431
if __name__ == '__main__':
2532
with open('input.txt') as data:
26-
print(count_trees(data.read(), (3, 1)))
33+
slopes = [(1,1), (3,1), (5,1), (7,1), (1,2)]
34+
print(calculate(data.read(), slopes))

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
numpy

0 commit comments

Comments
 (0)