-
Notifications
You must be signed in to change notification settings - Fork 1
/
day03.Rmd
59 lines (52 loc) · 1.36 KB
/
day03.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: "--- Day 3: Toboggan Trajectory ---"
author: Fleur Kelpin
date: Dec 3, 2020
output: github_document
---
```{r message=FALSE, warning=FALSE}
library(tidyverse)
input <- read_lines("day03.txt")
```
# Part 1
Start by **counting all the trees** you would encounter for the slope right 3, down 1
```{r}
hasTreeOnRow <- function(rowNum, stepRight) {
row <- input [[rowNum]]
col <- ((stepRight * (rowNum - 1)) %% str_length(row)) + 1
char <- str_sub(row, col, col)
char == "#"
}
```
```{r}
seq(along.with = input) %>%
sapply(function(rowNum) {
hasTreeOnRow(rowNum, 3)
}) %>%
sum()
```
# Part 2
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.
**What do you get if you multiply together the number of trees encountered on each of the listed slopes?**
```{r}
treesOnSlope <- function(stepRight, stepDown) {
seq(from = 1, to = length(input), by = stepDown) %>%
sapply(function(rowNum) {
hasTreeOnRow(rowNum, stepRight / stepDown)
}) %>%
sum()
}
counts <- (c(
treesOnSlope(1, 1),
treesOnSlope(3, 1),
treesOnSlope(5, 1),
treesOnSlope(7, 1),
treesOnSlope(1, 2)
))
prod(counts)
```