Advertisement · 728 × 90
#
Hashtag
#AoCDay5
Advertisement · 728 × 90
# Day 5 - Cafeteria - Part 1

inputD5P1 <- data.frame(input = readLines("D5input.txt"), stringsAsFactors = FALSE)  #load data and split into 2 parts
blankrow <- which(inputD5P1$input == "")[1]
top <- inputD5P1 |> filter(row_number() < blankrow)
bottom <- inputD5P1 |> filter(row_number() > blankrow) |> 
  rename(candidate = 1) |> mutate(candidate = as.numeric(candidate)) |> arrange(candidate)

#split the top bit into from and to and tidy up the overlapping ranges:
top <- top |> separate(input, into = c("fromnum", "tonum"), sep = "-") |>
  mutate(fromnum = as.numeric(fromnum), tonum = as.numeric(tonum)) |> arrange(fromnum)
tidied_top <- tidy_ranges(top) |> arrange(fromnum)

#get rid of deffo spoiled items 
bottom <- bottom |> filter(candidate >= min(tidied_top$fromnum) & candidate <= max(tidied_top$tonum))

#now check through the ranges for each remaining candidate - find the first "from" that it is >= , and then mark as a match 
# if <= the to on that row - then when we have passed the "to" on a row, get rid

freshcounter <- 0
for (i in 1:nrow(bottom)) {
  candidate <- bottom[i,1]   
  tidied_top <- filter(tidied_top, tonum >= candidate)
  matchrow <- which(tidied_top$fromnum <= candidate)[1]
  if (!is.na(matchrow) && candidate <= tidied_top[matchrow,2]) {freshcounter <- freshcounter + 1}}

print(paste("answer is",freshcounter))

# Day 5 - Cafeteria - Part 1 inputD5P1 <- data.frame(input = readLines("D5input.txt"), stringsAsFactors = FALSE) #load data and split into 2 parts blankrow <- which(inputD5P1$input == "")[1] top <- inputD5P1 |> filter(row_number() < blankrow) bottom <- inputD5P1 |> filter(row_number() > blankrow) |> rename(candidate = 1) |> mutate(candidate = as.numeric(candidate)) |> arrange(candidate) #split the top bit into from and to and tidy up the overlapping ranges: top <- top |> separate(input, into = c("fromnum", "tonum"), sep = "-") |> mutate(fromnum = as.numeric(fromnum), tonum = as.numeric(tonum)) |> arrange(fromnum) tidied_top <- tidy_ranges(top) |> arrange(fromnum) #get rid of deffo spoiled items bottom <- bottom |> filter(candidate >= min(tidied_top$fromnum) & candidate <= max(tidied_top$tonum)) #now check through the ranges for each remaining candidate - find the first "from" that it is >= , and then mark as a match # if <= the to on that row - then when we have passed the "to" on a row, get rid freshcounter <- 0 for (i in 1:nrow(bottom)) { candidate <- bottom[i,1] tidied_top <- filter(tidied_top, tonum >= candidate) matchrow <- which(tidied_top$fromnum <= candidate)[1] if (!is.na(matchrow) && candidate <= tidied_top[matchrow,2]) {freshcounter <- freshcounter + 1}} print(paste("answer is",freshcounter))

# Part 2 - glad I tidied up my ranges in part 1, makes this part easy peasy! :)

#re-create tidied_top and count how many in each range

tidied_top <- tidy_ranges(top) |> mutate(howmany = tonum - fromnum + 1)
print(paste("answer is",sum(tidied_top$howmany)))

# Part 2 - glad I tidied up my ranges in part 1, makes this part easy peasy! :) #re-create tidied_top and count how many in each range tidied_top <- tidy_ranges(top) |> mutate(howmany = tonum - fromnum + 1) print(paste("answer is",sum(tidied_top$howmany)))

Oops - I forgot to post #AdventOfCode day 5 #AoCDay5 #RStats

I was a bit smug about this one because I solved it fairly quickly - and part 2 (which seemed to trip a lot of other people up) was an absolute doddle, because I'd already done most of the work in part 1 - pure luck really

3 0 1 0