05: Print Queue

Lots of minor hiccups leading to a major fizzle. Overall I want to see how everyone else’s solutions look, but I think mine is rather elegant, even if a bit slow.

library(mistlecode)

options(scipen = 999)
input <- 'input.txt'
skip <- which(readLines(input) == '')

dt1 <- 
  input |>
  readr::read_delim('|', col_names = FALSE, n_max = skip - 1, show_col_types = FALSE) |>
  dplyr::filter(!is.na(.data$X2))
Registered S3 methods overwritten by 'bit64':
  method               from  
  as.double.integer64  cheapr
  as.integer.integer64 cheapr
dt2 <-
  input |>
  readLines()
dt2 <-
  dt2[(skip+1):length(dt2)] |>
  strsplit(',') |>
  purrr::map(as.integer)

Part 1

This was really frustrating. I was struggling and eventually found I had lines with even numbers of items. I couldn’t figure out what the heck was up with that and how to get a middle item from an even length set and then eventually realized my input ingestion was messing everything up. Once I switched from read.delim() to readLines(), I was able to clear everything right up.

middle_page <-
  dt2 |>
  purrr::map_int(\(x) {
    if (length(x) %% 2 == 0) print(x)
    is_valid <-
      x |>
      seq_along() |>
      purrr::map_lgl(\(i) {
        if (i == length(x)) return(TRUE)
        left <- x[i]; right <- x[(i+1):length(x)];
        check_right <- dt1$X2[dt1$X1 == left]
        all(right %in% check_right)
      }) |>
      all()
    if (is_valid) x[ceiling(length(x) / 2)] else 0
  })
sum(middle_page)
[1] 7307

Part 2

This was just a matter of getting the logic straight in my head then putting it on sreen and getting over silly mistakes like filtering for things that passed the test in part 1 instead of things that failed. Except. I’ve always struggled with recursion but I’m getting better at it and I’m getting better at it and I’m getting better at it and eventually I might be able to solve that stupid bag problem from 2020.

swap <- function(x, i = 1) {
  left <- x[i]; right <- x[i+1];
  check_right <- dt1$X2[dt1$X1 == left]
  if (is.na(right)) return(x)
  if (!(right %in% check_right)) {
    x[i+1] <- left; x[i] <- right;
    swap(x, 1)
  } else { swap(x, i + 1) }
}

dt2[middle_page == 0] |>
  purrr::map_int(\(x) {
    x <- swap(x)
    x[ceiling(length(x) / 2)]
  }) |>
  sum()
[1] 4713