⛄ Day 3
View Day 2
< |
>
View Day 4
Advent of Code is a coding challenge
that releases a puzzle every day starting December 1st up until the 25th -
like an Advent Calendar but no stale chocolate...
Part 1
Given a 2D, repeating map of 'trees' and snow find how many trees you will hit with a slope of
3 right, 1 down starting from the top left corner.
Solution:
import csv
def get_panel(file_name):
panel = []
with open(file_name, 'r') as data:
reader = csv.reader(data)
for line in reader:
panel.append(list(line[0]))
return panel
def print_panel(panel):
print('\n'.join(map(lambda l: ''.join(l), panel)))
def count_trees(panel, step_h, step_w):
panel_h = len(panel)
panel_w = len(panel[0])
index_h = 0
index_w = 0
tree_count = 0
while index_h < panel_h - step_h:
index_h += step_h
index_w += step_w
if index_w >= panel_w:
index_w = index_w - panel_w
xo = panel[index_h][index_w]
if xo == '#':
tree_count += 1
return tree_count
panel = get_panel('__03.txt')
trees = count_trees(panel, 1, 3)
print('TREES: ', trees)
Part 2
Find how many trees you would hit for the following slopes:
Right 1, down 1.
Right 3, down 1. (This is the slope you already checked.)
Right 5, down 1.
Right 7, down 1.
Right 1, down 2.
Solution:
slopes = [(1,1), (1,3), (1,5), (1,7), (2,1)]
tree_count = map(lambda x: count_trees(panel, x[0], x[1]), slopes)
print(tree_count)
print(reduce(lambda x, y: x*y, tree_count))
View Day 2
< |
>
View Day 4