2020 Advent of Code solutions in various languages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

512 lines
13 KiB

) -> asm$intr[run]
while (all(asm$visits < 2)) {
asm$visits[curr] <- asm$visits[curr] + 1
switch(
asm$instr[curr],
"acc" = curr + 1,
"nop" = curr + 1,
"jmp" = curr + asm[curr,]$value
) -> nxt
if (nxt > nrow(asm)) break
if ((asm$visits[curr] < 2) && (asm$instr[curr] == "acc")) {
accumulator <- accumulator + asm$value[curr]
}
curr <- nxt
}
if (all(asm$visits < 2)) break
}
read_delim(
file = input,
delim = " ",
col_names = c("instr", "value"),
col_types = "cd"
) %>%
mutate(
visits = 0
) -> asm
orig_program <- asm
for (run in seq_len(nrow(asm))) {
asm <- orig_program
curr <- 1
accumulator <- 0
switch(
asm$instr[run],
"acc" = "acc",
"nop" = "jmp",
"jmp" = "nop"
) -> asm$intr[run]
while (all(asm$visits < 2)) {
asm$visits[curr] <- asm$visits[curr] + 1
switch(
asm$instr[curr],
"acc" = curr + 1,
"nop" = curr + 1,
"jmp" = curr + asm[curr,]$value
) -> nxt
if (nxt > nrow(asm)) break
if ((asm$visits[curr] < 2) && (asm$instr[curr] == "acc")) {
accumulator <- accumulator + asm$value[curr]
}
curr <- nxt
}
if (all(asm$visits < 2)) break
}
read_delim(
file = input,
delim = " ",
col_names = c("instr", "value"),
col_types = "cd"
) %>%
mutate(
visits = 0
) -> asm
orig_program <- asm
for (run in seq_len(nrow(asm))) {
asm <- orig_program
curr <- 1
accumulator <- 0
switch(
asm$instr[run],
"acc" = "acc",
"nop" = "jmp",
"jmp" = "nop"
) -> asm$instr[run]
while (all(asm$visits < 2)) {
asm$visits[curr] <- asm$visits[curr] + 1
switch(
asm$instr[curr],
"acc" = curr + 1,
"nop" = curr + 1,
"jmp" = curr + asm[curr,]$value
) -> nxt
if (nxt > nrow(asm)) break
if ((asm$visits[curr] < 2) && (asm$instr[curr] == "acc")) {
accumulator <- accumulator + asm$value[curr]
}
curr <- nxt
}
if (all(asm$visits < 2)) break
}
accumulator
?read.delim
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:5
window - preamble_length
preamble_length - window
input_start <- 6
preamble_length <- 5
window <- 1:preamble_length
tail(letters, -1)
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
expand_grid(input[window])
expand_grid(input[window], input[windpw])
expand_grid(input[window], input[winopw])
expand_grid(input[window], input[window])
expand.grid(input[window], input[window])
crossprod(input[window], input[window])
expand.grid(input[window], input[window])
?expand_grid
expand_grid(x1 = input[window], x2 = input[window])
expand_grid(x1 = input[window], x2 = input[window]) %>%
filter(x1 != x2)
expand_grid(x1 = window, x2 = window)
expand_grid(x1 = window, x2 = window) %>%
filter(x1 != x2) %>%
mutate(
sum = input[x1] + input[x2]
)
?combn
combn(window)
combn(window, 2)
combn(window, 2, FUN=sum)
combn(window, 2, simplify = FALSE)
t(combn(window, 2))
as.data.frame(t(combn(window, 2)))
as.data.frame(t(combn(window, 2))) %>%
mutate(
sum = input[x1] + input[x2]
)
as.data.frame(t(combn(window, 2))) %>%
mutate(
sum = input[V1] + input[V2]
)
no_sum_found <- TRUE
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums == input[input_start])
input <- tail(input, -1)
}
input
no_sum_found
as.data.frame(t(combn(window, 2))) %>%
mutate(
sum = input[V1] + input[V2]
)
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
as.data.frame(t(combn(window, 2))) %>%
mutate(
sum = input[V1] + input[V2]
)
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2,],
sum = input[V1] + input[V2]
)
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
)
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
input <- tail(input, -1)
}
input
pair_sums
input
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
cat("Processing", input[input_start], "\n")
no_sum_found <- any(pair_sums$sum == input[input_start])
input <- tail(input, -1)
}
input[input_start]
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
cat("Processing", input[input_start], "\n")
no_sum_found <- any(pair_sums$sum == input[input_start])
if (!no_sum_found) input <- tail(input, -1)
}
input[input_start]
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
cat("Processing", input[input_start], "\n")
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
input[input_start]
library(tidyverse)
input <- read_lines("../input/09-01.txt")
input
input <- read_lines("../input/09-01.txt")
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
input <- read_lines("../input/09-01.txt")
input <- as.mnumeric(read_lines("../input/09-01.txt"))
input <- as.numeric(read_lines("../input/09-01.txt"))
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
input[input_start]
invalid <- input[input_start]
invalid
input <- as.numeric(read_lines("../input/09-01.txt"))
input <- test_input
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
invalid <- input[input_start]
invalid
input <- test_input
seq_along(input)
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
sum_found <- FALSE
input <- test_input
cumsum_found <- FALSE
for (i in seq_along(input)) {
csum <- cumsum[i:length(input)]
if (any(csum == invalid)) break
}
input <- test_input
cumsum_found <- FALSE
for (i in seq_along(input)) {
csum <- cumsum(i:length(input))
if (any(csum == invalid)) break
}
range(input[i:which(csum == invalid)])
i
csum
input <- test_input
cumsum_found <- FALSE
for (i in seq_along(input)) {
csum <- cumsum(input[i:length(input)])
if (any(csum == invalid)) break
}
range(input[i:which(csum == invalid)])
sum(range(input[i:which(csum == invalid)]))
invalid
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
sum_found <- FALSE
while((!sum_found) && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
sum_found <- any(pair_sums$sum == input[input_start])
if (!sum_found) input <- tail(input, -1)
}
invalid <- input[input_start]
input <- test_input
cumsum_found <- FALSE
csum
which(csum == invalid)
csum == invalid
i
invalid
invalid <- input[input_start]
invalid
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
sum_found <- FALSE
while((!sum_found) && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
sum_found <- any(pair_sums$sum == input[input_start])
if (!sum_found) input <- tail(input, -1)
}
invalid <- input[input_start]
invalid
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
sum_found <- FALSE
while((!sum_found) && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
sum_found <- any(pair_sums$sum == input[input_start])
if (!sum_found) input <- tail(input, -1)
}
invalid <- input[input_start]
(invalid <- input[input_start])
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
sum_found <- FALSE
while((!sum_found) && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
sum_found <- any(pair_sums$sum == input[input_start])
if (!sum_found) input <- tail(input, -1)
}
(invalid <- input[input_start])
input <- test_input
test_input
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
input <- test_input
preamble_length <- 5
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
cat("Processing", input[input_start], "\n")
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
(invalid <- input[input_start])
input <- test_input
cumsum_found <- FALSE
for (i in seq_along(input)) {
csum <- cumsum(input[i:length(input)])
if (any(csum == invalid)) break
}
range(input[i:which(csum == invalid)])
invalid
input[i:length(input)]
cumsum
csum
which(csum == invalid)
range(input[i:(i+which(csum == invalid))])
range(input[i:(i+which(csum == invalid)-1)])
sum(range(input[i:(i+which(csum == invalid)-1)]))
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
# input <- test_input
input <- as.numeric(read_lines("../input/09-01.txt"))
preamble_length <- 5
window <- 1:preamble_length
library(tidyverse)
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
# input <- test_input
input <- as.numeric(read_lines("../input/09-01.txt"))
# preamble_length <- 5
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=6)) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
(invalid <- input[input_start])
# input <- test_input
input <- as.numeric(read_lines("../input/09-01.txt"))
for (i in seq_along(input)) {
csum <- cumsum(input[i:length(input)])
if (any(csum == invalid)) break
}
sum(range(input[i:(i+which(csum == invalid)-1)]))
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576)
# input <- test_input
input <- as.numeric(read_lines("../input/09-01.txt"))
# preamble_length <- 5
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
while(no_sum_found && (length(input)>=(preamble_length + 1))) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
val1 = input[V1],
val2 = input[V2],
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
}
(invalid <- input[input_start])