#Day 3 - Lobby - Part 1 inputD3P1 <- data.frame(input = readLines("D3input.txt"), stringsAsFactors = FALSE) inputD3P1 <- inputD3P1 |> rowwise() |> mutate(shorter_input = str_sub(input, end = -2), firstdigit = max(strsplit(shorter_input, "")[[1]]), firstdigitpos = which(strsplit(shorter_input, "")[[1]] == firstdigit)[1], remainder = str_sub(input, start = (firstdigitpos + 1)), seconddigit = max(strsplit(remainder, "")[[1]]), joltage = as.numeric(firstdigit)*10+as.numeric(seconddigit)) |> ungroup() print(paste("answer is",sum(inputD3P1$joltage)))
#Part 2 - the same thing but twelve times instead of 2 inputD3P1 <- data.frame(input = readLines("D3input.txt"), stringsAsFactors = FALSE) inputD3P1 <- inputD3P1 |> rowwise() |> mutate(shorter_input = str_sub(input, end = -12), firstdigit = max(strsplit(shorter_input, "")[[1]]), firstdigitpos = which(strsplit(shorter_input, "")[[1]] == firstdigit)[1], remainder = str_sub(input, start = (firstdigitpos + 1), end = -11), seconddigit = max(strsplit(remainder, "")[[1]]), seconddigitpos = which(strsplit(remainder, "")[[1]] == seconddigit)[1], remainder2 = str_sub(input, start = (firstdigitpos+seconddigitpos + 1), end = - 10), thirddigit = max(strsplit(remainder2, "")[[1]]), thirddigitpos = which(strsplit(remainder2, "")[[1]] == thirddigit)[1], remainder3 = str_sub(input, start = (firstdigitpos+seconddigitpos +thirddigitpos + 1), end = -9), #and so on ...... joltage = as.numeric(firstdigit)*100000000000+as.numeric(seconddigit)*10000000000+ as.numeric(thirddigit)*1000000000+as.numeric(fourthdigit)*100000000+ as.numeric(fifthdigit)*10000000+as.numeric(sixthdigit)*1000000+ as.numeric(seventhdigit)*100000+as.numeric(eigthdigit)*10000+ as.numeric(ninthdigit)*1000+as.numeric(tenthdigit)*100+ as.numeric(eleventhdigit)*10+as.numeric(twelthdigit)) |> ungroup() print(paste("answer is",sum(inputD3P1$joltage)))
I'm so behind with these!! but it has been a busy month, sometimes the elves have to take a back seat, sorry elves
#AdventOfCode day 3 - I basically used the exact same approach for parts 1 and 2, so here they both are
#AoCDay3 #RStats