Day 1: Inverse Captcha

Reference

Part One

library(tidyverse)

input <- read_lines(file = "inputs/2017/01.txt")

input <- str_split_1(string = input, pattern = "") |> as.integer()

digits <- tibble(digit   = input,
                 compare = c(input[-1], input[1])) |> 
    mutate(match = if_else(digit == compare, digit, 0L))

digits |> 
    summarise(total = sum(match)) |> 
    pull(total) |> 
    cat()
## 1119

Part Two

mid <- nrow(digits) / 2

digits <- tibble(digit   = input,
                 compare = c(input[-(1:mid)], input[1:mid])) |>
    mutate(match = if_else(digit == compare, digit, 0L))

digits |>
    summarise(total = sum(match)) |>
    pull(total) |>
    cat()
## 1420