library(tidyverse)
Day 1
Advent of Code: Worked Solutions
Setup
Import libraries:
Read and parse text input from file:
<- scan("../input/day01.txt") input
Part 1
Create every combination of numbers, determine which sum to 2020, and take their product:
combn(input, m = 2, simplify = FALSE) |>
keep(~ sum(.x) == 2020) |>
map_dbl(prod)
Part 2
Repeat as above, but take combinations 3 at a time:
combn(input, m = 3, simplify = FALSE) |>
keep(~ sum(.x) == 2020) |>
map_dbl(prod)