0
0
Fork 0

Add advent code for 2020 days 1-4

This commit is contained in:
Oliver-Akins 2020-12-06 20:38:44 -07:00
parent 2c466792a4
commit 948009891a
12 changed files with 256 additions and 0 deletions

1
day_3/info.md Normal file
View file

@ -0,0 +1 @@
https://adventofcode.com/2020/day/3

30
day_3/part_1.py Normal file
View file

@ -0,0 +1,30 @@
with open("input") as f:
hill = f.read().split("\n")
# Index deltas
slope = [
1, # the change in vertical height of the toboggan
3 # the change in horizontal distance of the toboggan
]
# The starting position of the toboggan
position = [0, 0]
tree_count = 0
while position[0] < len(hill):
# Wrap the pattern as needed
if position[1] >= len(hill[position[0]]):
position[1] -= len(hill[position[0]])
# Check for trees
if hill[position[0]][position[1]] == "#":
tree_count += 1
position[0] += slope[0]
position[1] += slope[1]
print(f"Trees Hit: {tree_count}")

41
day_3/part_2.py Normal file
View file

@ -0,0 +1,41 @@
with open("input") as f:
hill = f.read().split("\n")
# Index deltas
slopes = [
[1, 1], # right 1, down 1
[1, 3], # right 3, down 1
[1, 5], # right 5, down 1
[1, 7], # right 7, down 1
[2, 1] # right 1, down 2
]
# The starting position of the toboggan
tree_counts = []
for slope in slopes:
position = [0, 0]
tree_count = 0
while position[0] < len(hill):
# Wrap the pattern as needed
if position[1] >= len(hill[position[0]]):
position[1] -= len(hill[position[0]])
# Check for trees
if hill[position[0]][position[1]] == "#":
tree_count += 1
position[0] += slope[0]
position[1] += slope[1]
tree_counts.append(tree_count)
total = 1
for x in tree_counts:
total *= x
print(tree_counts, total)