# Libraries
library(tidyverse)
# Read input from file
<- read_lines("../input/day25.txt", skip_empty_rows = TRUE) input
Day 25
Advent of Code: Worked Solutions
Setup
Part 1
Convert “snafu” strings to decimal numbers:
<- function(snafu) {
snafu_to_dbl |>
snafu str_split("") |>
map(~ case_match(rev(.x), "2" ~ 2, "1" ~ 1, "0" ~ 0, "-" ~ -1, "=" ~ -2)) |>
map(~ .x * 5^(0:(length(.x) - 1))) |>
map_dbl(sum)
}
Convert decimal numbers to “snafu” strings:
<- function(x) {
dbl_to_snafu <- c()
output
repeat {
<- c((x + 2) %% 5 - 2, output)
output <- floor((x + 2) / 5)
x
if (x == 0) break
}
|>
output case_match(2 ~ '2', 1 ~ '1', 0 ~ '0', -1 ~ '-', -2 ~ '=') |>
str_c(collapse = "")
}
Run on puzzle input:
|>
input snafu_to_dbl() |>
sum() |>
dbl_to_snafu()
[1] "122-12==0-01=00-0=02"