Grilled Cheese

ExploreLog inSign up
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-12-03 22:42:33 EST

PhotosVideosAllPeople
LatestRanked
Blog post image
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Code from https://github.com/riinupius/advent2026/blob/main/day03/foggy-forest.R
Photo, no description
Photo, no description
Map for Nov 25 (Is This a Map?): Is this a map? Aracruz, painted in gouache, by Maria Luiza Freitas Rocha.
Title card. Day 4 of 25, #RCLIAdvent2026: Finding the executable. Override, PATH, then known locations.
library(tidyverse)

# A period k is feasible if and only if every modulo-k residue class
# contains at most one distinct known colour (ignoring '?').
min_period <- function(barcode) {
  chars <- strsplit(barcode, "")[[1]]
  n <- length(chars)
  # Try candidate instruction lengths from shortest to longest.
  # The first valid must be the minimum.
  for (k in seq_len(n)) {
    # Assume k works until we discover a contradiction.
    valid <- TRUE
    
    # For a repeating instruction sequence of length k, positions
    # sharing the same remainder modulo k are produced by the same
    # instruction and therefore must contain the same colour.
    for (r in seq_len(k)) {
      # Extract all barcode positions belonging to this modulo class.
      idx <- seq(r, n, by = k)
      # Unknown colours ('?') place no restrictions on the solution,
      # so only retain colours that are actually known.
      vals <- chars[idx]
      vals <- vals[vals != "?"]
      # If a modulo class contains two different known colours,
      # this value of k cannot work because one instruction position
      # would need to print multiple colours.
      if (length(unique(vals)) > 1) {
        valid <- FALSE
        break
      }
    }
    # Since periods are tested from smallest to largest, the first
    # valid period encountered is the minimum instruction count.
    if (valid) {
      return(k)
    }
  }
  # Defensive fallback. In practice k = n must always be valid.
  n
}
#part 1
barcodes <- readLines("input.txt")
answer <-
  tibble(barcode = barcodes) |>
  mutate(min_instructions = map_int(barcode, min_period)) |>
  summarise(total = sum(min_instructions)) 
answer$total
# part 2 collapse the input
barall <- paste(barcodes, collapse = "")
min_period(barall)
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Load more