Browse Source

Day 3: Python

master
boB Rudis 3 years ago
parent
commit
88d28777c7
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1020
      R/.Rhistory
  2. 1
      R/.Rproj.user/shared/notebooks/paths
  3. 19
      R/03.R
  4. 42
      R/03.py

1020
R/.Rhistory

File diff suppressed because it is too large

1
R/.Rproj.user/shared/notebooks/paths

@ -3,6 +3,7 @@
/Users/hrbrmstr/Development/2020-code-advent/R/02.R="AF113B0D"
/Users/hrbrmstr/Development/2020-code-advent/R/02.py="0900576A"
/Users/hrbrmstr/Development/2020-code-advent/R/03.R="B9D5FDD0"
/Users/hrbrmstr/Development/2020-code-advent/R/03.py="C2F47E74"
/Users/hrbrmstr/Development/2020-code-advent/README.md="74DC8DCF"
/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt="53BE9636"
/Users/hrbrmstr/Development/2020-code-advent/input/02-01.txt="C32036DD"

19
R/03.R

@ -72,8 +72,6 @@
library(stringi)
library(tidyverse)
# 03-01 -------------------------------------------------------------------
readLines("../input/03-01.txt") %>%
stri_split_boundaries(
opts_brkiter = stri_opts_brkiter("character"),
@ -105,10 +103,27 @@ tree_count <- function(orig_map, slope_x, slope_y) {
}
# 03-01 -------------------------------------------------------------------
tree_count(orig_map, 3, 1)
# 03-02 -------------------------------------------------------------------
# --- Part Two ---
#
# Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all.
#
# 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:
#
# 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.
# In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied together, these produce the answer 336.
#
# What do you get if you multiply together the number of trees encountered on each of the listed slopes?
x <- c(1, 3, 5, 7, 1)
y <- c(1, 1, 1, 1, 2)

42
R/03.py

@ -0,0 +1,42 @@
# with open("/tmp/test.txt") as f:
with open("../input/03-01.txt") as f:
orig_map = f.read().splitlines()
def tree_count(orig_map, slope_x, slope_y):
m_cols = len(orig_map[0])
m_rows = len(orig_map)
n_tiles = (slope_x+1) * m_rows // (m_cols)
actual_map = [ row * n_tiles for row in orig_map ]
xpos = 0
ypos = 0
n_trees = 0
while True:
xpos = xpos + slope_x
ypos = ypos + slope_y
if (ypos >= m_rows):
break
if (actual_map[ypos][xpos] == "#"):
n_trees = n_trees + 1
return(n_trees)
# 03-01 -------------------------------------------------------------------
tree_count(orig_map, 3, 1)
# 03-02 -------------------------------------------------------------------
from functools import reduce
x = [ 1, 3, 5, 7, 1 ]
y = [ 1, 1, 1, 1, 2 ]
reduce(lambda x, y: x*y, [ tree_count(orig_map, x[idx], y[idx]) for idx in range(len(x)) ])
Loading…
Cancel
Save