-
Notifications
You must be signed in to change notification settings - Fork 1
/
day10.Rmd
50 lines (41 loc) · 1.31 KB
/
day10.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
---
title: "--- Day 10: Adapter Array ---"
author: Fleur Kelpin
date: Dec 10, 2020
output: github_document
---
```{r message=FALSE, warning=FALSE}
library(tidyverse)
input <- readr::read_csv("day10.txt", col_names = FALSE)[[1]] %>%
prepend(0) %>%
{append(., max(.) + 3)} %>%
sort()
as_tibble(input)
```
# Part 1
Find a chain that uses all of your adapters to connect the charging outlet to
your device's built-in adapter and count the joltage differences between the
charging outlet, the adapters, and your device. **What is the number of 1-jolt
differences multiplied by the number of 3-jolt differences?**
```{r}
input %>%
diff() %>%
{ sum(. == 1) * sum(. == 3) }
```
# Part 2
**What is the total number of distinct ways you can arrange the adapters to
connect the charging outlet to your device?**
Use dynamic programming to reuse the number of ways you can connect the lower
jolt adapters to the outlet to compute the number of ways for the next adapter.
```{r}
adapters <- tibble(jolt = input, ways=as.numeric(NA))
adapters[1, 'ways'] = 1
while((index <- detect_index(adapters$ways, is.na)) > 0 ){
joltage <- adapters[index, 'jolt']
prev_adapters <- filter(adapters, jolt %>% between(joltage-3, joltage-1))
adapters[index, 'ways'] = sum(prev_adapters$ways)
}
adapters
options(digits = 15)
max(adapters$ways)
```