Day 1

Advent of Code: Worked Solutions

About
Date

December 1, 2020

Setup

Import libraries:

library(tidyverse)

Read and parse text input from file:

input <- scan("../input/day01.txt")

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)