I did ‘Advent of Code’ and here is what I learned

Emil Hvitfeldt

2020 December 1st

Find the two entries that sum to 2020

My Solution

input <- as.numeric(readLines("01-input"))

sum2 <- function(input, target) {
  for (i in input) for (j in input) if (i + j == target) return(i * j)
}

sum2(input, 2020)

502 Bad Gateway 🔥🔥🔥

Possible Approaches

  • loops
  • expand.grid
  • sample
  • Rockstar

The goal is to extinguish my desparation
Knock the goal down

The enemy are accountants
Knock the enemy down

The system is the enemy
The machine is the enemy

Listen to the money
Until the money is gone
Cast the money into the fire
Let the rage at the enemy be the fire
Build the enemy up
Listen to the money

While the enemy is higher than the system
Let chaos be the rage at the system
Let destruction be the rage at the machine
If the goal is chaos with destruction
Break it down

Build the machine up
If the enemy is higher than the machine
Take it to the top

Build the system up
The machine is the system

Shout chaos of destruction

Types of problems

  • recursive
  • Grid based
  • parsable

Parsable Solutions

forward 2
down 4
down 3
up 4
down 1
down 8
up 9
forward 1
down 9
forward 6
down 7
forward 1
down 1
up 2
forward 8
down 3
down 9
down 3

Parsable Solutions

forward(2)
down(4)
down(3)
up(4)
down(1)
down(8)
up(9)
forward(1)
down(9)
forward(6)
down(7)
forward(1)
down(1)
up(2)
forward(8)
down(3)
down(9)
down(3)
  • modify the input
  • define forward(), down() and up()
  • run eval(parse(text = input))
  • profit

memory exhausted (limit reached)

Lanternfish

Initial state: 3,4,3,1,2

Lanternfish

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1

Lanternfish

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1
After  2 days: 1,2,1,6,0,8

Lanternfish

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1
After  2 days: 1,2,1,6,0,8
After  3 days: 0,1,0,5,6,7,8
After  4 days: 6,0,6,4,5,6,7,8,8
After  5 days: 5,6,5,3,4,5,6,7,7,8
After  6 days: 4,5,4,2,3,4,5,6,6,7
After  7 days: 3,4,3,1,2,3,4,5,5,6
After  8 days: 2,3,2,0,1,2,3,4,4,5
After  9 days: 1,2,1,6,0,1,2,3,3,4,8
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8

Lanternfish

Part 1

How many lanternfish would there be after 80 days?

Lanternfish

Part 1

How many lanternfish would there be after 80 days?

Part 2

How many lanternfish would there be after 256 days?

Lanternfish

Part 1

How many lanternfish would there be after 80 days? ~1.5 MB

Part 2

How many lanternfish would there be after 256 days?

Lanternfish

Part 1

How many lanternfish would there be after 80 days? ~1.5 MB

Part 2

How many lanternfish would there be after 256 days? ~6.58 TB

General workflow

  • reading in data

  • doing calculations

  • unit tests

  • Solution

  • {readr}
  • data.table::fread()
  • {jsonlite}
  • read_parquet()

General workflow

  • reading in data

  • doing calculations

  • unit tests

  • Solution

  • vectors
  • matrices
  • data.frames
  • lists

General workflow

  • reading in data

  • doing calculations

  • unit tests

  • Solution

General workflow

  • reading in data

  • doing calculations

  • unit tests

  • Solution

You might learn something

Ways you can play this game

  • You can go for speed
  • shortest solution
  • no dependencies
  • try another language

go at your own phase

The R community is awesome!

Many different problems

  • Squares With Three Sides (2016 day 3)
  • Encoding Error (2020 day 9)
  • Wizard Simulator 20XX (2015 day 22)

the problems are CLEARLY written

Take home

  • You might learn something
  • Join the Community
  • Have fun!