Day 10

AdventOfCode > 2022

Part 1: Find the signal strength during the 20th, 60th, 100th, 140th, 180th, and 220th cycles. What is the sum of these six signal strengths?

I manually downloaded my personal day 9 input file as a logged user, and here I get the data in a more appropriate shape.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0          ✔ purrr   0.3.5     
✔ tibble  3.1.8          ✔ dplyr   1.0.10    
✔ tidyr   1.2.1          ✔ stringr 1.5.0.9000
✔ readr   2.1.3          ✔ forcats 0.5.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
file <- here::here("2022/10_input")

data <- file %>%
  read_csv(col_names = "x", show_col_types = FALSE) %>% 
  pull() %>%
  str_split(" ") %>%
  unlist() %>% 
  as.integer() %>% 
  replace_na(0) 
Warning in replace_na(., 0): NAs introduced by coercion
data
  [1]   0   2   0   3   0   3   0  -2   0   4   0   0   1   0   4   0   1   0
 [19]   0   4   0   1   0   0   2   0   5   0 -28   0  30   0   0   5   0   1
 [37]   0   0 -38   0   0   0   0   0   5   0   5   0   3   0   2   0  -2   0
 [55]   2   0   0   0  -2   0  12   0   0   2   0   3   0   0   2   0 -31   0
 [73]  32   0   7   0   0  -2   0 -37   0   1   0   5   0   1   0   0  31   0
 [91] -25   0 -10   0  13   0   0   0  18   0 -11   0   3   0   0   0   1   0
[109]   4   0 -32   0  15   0  24   0  -2   0   0 -37   0   0   0   0   5   0
[127]   5   0  21   0 -20   0   0   6   0  19   0  -5   0  -8   0 -22   0  26
[145]   0 -22   0  23   0   2   0   0   0   0   8   0 -10   0 -27   0  33   0
[163] -27   0   0  34   0 -33   0   2   0  19   0 -12   0  11   0 -20   0  12
[181]   0  18   0 -11   0 -14   0  15   0   2   0   0   3   0   2   0   0   0
[199]   0 -33   0   0   1   0   2   0   0   3   0   4   0   0   1   0   2   0
[217]   0   0   7   0   1   0   0   4   0 -17   0  18   0   5   0  -1   0   5
[235]   0   1   0   0   0   0

Here the result:

begin <- 1
register <- cumsum(c(begin, data))
sum(register[seq(20,220,40)]*seq(20,220,40))
[1] 14540

Part 2: Render the image given by your program. What eight capital letters appear on your CRT?

begin <- 1
register <- cumsum(c(begin, data)) + 1
result <- 1:(length(register) - 1) %>% map_chr(function(x) {
    if ((x %% 40) %in% (register[x] - 1):(register[x] + 1)) "#" else "."
})
p <- tibble(pixel = result) %>%
    mutate(num = row_number()) %>%
    mutate(col = (num-1) %% 40 ) %>%
    mutate(row = -((num-1) %/% 40)) %>%
    ggplot(aes(col, row)) +
    geom_point(aes(shape = result, color = result)) +
    scale_shape_manual(values=c(1, 15)) +
    scale_color_manual(values=c("white","darkgreen")) +
    theme_minimal() +
    theme(
        legend.position = "none",
        axis.title=element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
    ) +
    labs(x = NULL, y = NULL)

ggplotly(p)