2021-04: Giant Squid

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.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:dplyr':

    between, first, last

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

    transpose
library(stringr)

Part 1

# part 1
dt <- data.frame(fread("input2.txt"))
call <- c(84,28,29,75,58,71,26,6,73,74,41,39,87,37,16,79,55,60,62,80,64,95,46,15,5,47,2,35,32,78,89,90,96,33,4,69,42,30,54,85,65,83,44,63,20,17,66,81,67,77,36,68,82,93,10,25,9,34,24,72,91,88,11,38,3,45,14,56,22,61,97,27,12,48,18,1,31,98,86,19,99,92,8,43,52,23,21,0,7,50,57,70,49,13,51,40,76,94,53,59)
bingo <- FALSE

for(i in 1:length(call)) {
  dt$V1[dt$V1 == call[i]] <- NA
  dt$V2[dt$V2 == call[i]] <- NA
  dt$V3[dt$V3 == call[i]] <- NA
  dt$V4[dt$V4 == call[i]] <- NA
  dt$V5[dt$V5 == call[i]] <- NA
  
  for(R in seq(1, nrow(dt), 5)) {
    for(r in R:(R+4)) {
      if(sum(is.na(dt[r,])) == 5) {
        bingo <- TRUE
        break
      }
    }
    for(c in 1:5) {
      if(sum(is.na(dt[R:(R+4),c])) == 5) {
        bingo <- TRUE
        break
      }
    }
    if(bingo)
      break
  }
  if(bingo)
    break
}
sum(dt[R:(R+4),], na.rm = TRUE) * call[i]
[1] 29440

Part 2

# part 2
dt <- data.frame(fread("input2.txt"))
call <- c(84,28,29,75,58,71,26,6,73,74,41,39,87,37,16,79,55,60,62,80,64,95,46,15,5,47,2,35,32,78,89,90,96,33,4,69,42,30,54,85,65,83,44,63,20,17,66,81,67,77,36,68,82,93,10,25,9,34,24,72,91,88,11,38,3,45,14,56,22,61,97,27,12,48,18,1,31,98,86,19,99,92,8,43,52,23,21,0,7,50,57,70,49,13,51,40,76,94,53,59)
bingo <- 0
bingoTF <- FALSE
boards <- nrow(dt) / 5

for(i in 1:length(call)) {
  dt$V1[dt$V1 == call[i]] <- NA
  dt$V2[dt$V2 == call[i]] <- NA
  dt$V3[dt$V3 == call[i]] <- NA
  dt$V4[dt$V4 == call[i]] <- NA
  dt$V5[dt$V5 == call[i]] <- NA
  
  for (R in seq(1, nrow(dt), 5)) {
    if (sum(is.na(dt[R:(R + 4), 1:5])) != 25) {
      for (r in R:(R + 4)) {
        if (sum(is.na(dt[r, ])) == 5) {
          dt[r, ] <- NA
          bingo <- append(bingo, i)
          bingoTF <- TRUE
          break
        }
      }
      for (c in 1:5) {
        if (sum(is.na(dt[R:(R + 4), c])) == 5) {
          dt[R:(R + 4), c] <- NA
          bingo <- append(bingo, i)
          bingoTF <- TRUE
          break
        }
      }
      
      if (bingoTF) {
        dt[R:(R + 4),] <- NA
        bingo <- append(bingo, i)
        bingoTF <- FALSE
      }
    }
  }
}

dt <- data.frame(fread("input2.txt"))
bingoTF <- FALSE

for(i in 1:(bingo[length(bingo)]-1)) {
  dt$V1[dt$V1 == call[i]] <- NA
  dt$V2[dt$V2 == call[i]] <- NA
  dt$V3[dt$V3 == call[i]] <- NA
  dt$V4[dt$V4 == call[i]] <- NA
  dt$V5[dt$V5 == call[i]] <- NA
  
  for (R in seq(1, nrow(dt), 5)) {
    if (sum(is.na(dt[R:(R + 4), 1:5])) != 25) {
      for (r in R:(R + 4)) {
        if (sum(is.na(dt[r, ])) == 5) {
          dt[r, ] <- NA
          bingoTF <- TRUE
          break
        }
      }
      for (c in 1:5) {
        if (sum(is.na(dt[R:(R + 4), c])) == 5) {
          dt[R:(R + 4), c] <- NA
          bingoTF <- TRUE
          break
        }
      }
      
      if (bingoTF) {
        dt[R:(R + 4),] <- NA
        bingoTF <- FALSE
      }
    }
  }
}

dt <- tail(dt[rowSums(dt, na.rm = TRUE) != 0,], 5)
i <- i + 1
dt$V1[dt$V1 == call[i]] <- NA
dt$V2[dt$V2 == call[i]] <- NA
dt$V3[dt$V3 == call[i]] <- NA
dt$V4[dt$V4 == call[i]] <- NA
dt$V5[dt$V5 == call[i]] <- NA
sum(dt, na.rm = TRUE) * call[i]
[1] 13884