duck-rabbit-soup

Advent of Code 1

Advent of code is fun. I used to half learn racketlang a couple of years ago. I think I'll do a few this year in R.

The first one has a nice recursive structure, but I'm just going to brute force it because it's easier!

library(tidyverse)

vec <- readr::read_file("input_1.txt") %>%
stringr::str_split("\n", simplify = TRUE) %>%
as.numeric()

to_match <- 2020 - vec

indexes <- which(to_match %in% vec)

# heres the solution to part 1
vec[indexes] %>% reduce(`*`)

first_differences <- 2020 - vec

diffed_first_difference <- map(first_differences, ~ . - vec)

mapped_indexes <- map(diffed_first_difference, ~ which(. %in% vec)) %>%
keep(~ length(.) == 2) %>%
reduce(c) %>%
unique()

# and heres the solution to part 2
vec[mapped_indexes] %>% reduce(`*`)