diff --git a/R/.Rhistory b/R/.Rhistory index af4322d..1e18a4e 100644 --- a/R/.Rhistory +++ b/R/.Rhistory @@ -1,512 +1,512 @@ -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -sum() -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -> paths -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -sum() -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum() + lengths(paths) - 1 } -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + lengths(paths) - 1 } -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -input <- read_lines("/tmp/test.txt") -input %>% -stri_match_first_regex( -"^([[:alpha:][:space:]]+)[[:space:]]bags[[:space:]](.*)$", -) %>% -.[,2:3] %>% -as.data.frame() %>% -as_tibble() %>% -rename( -bag_color = 1, -rule = 2 +) -> 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" ) %>% -separate_rows(rule, sep=", ") %>% mutate( -count = case_when( -stri_detect_regex(rule, "^[[:digit:]]") ~ stri_match_first_regex(rule, "^([[:digit:]]+)")[,2], -stri_detect_regex(rule, "contain [[:digit:]]") ~ stri_match_first_regex(rule, "^contain ([[:digit:]]+)")[,2], -stri_detect_fixed(rule, "no other") ~ "0" -) %>% -as.integer(), -contains = stri_match_first_regex(rule, "[[:space:]]([[:alpha:][:space:]]+)[[:space:]]ba")[,2] -) %>% -select(bag_color, contains, count) -> bag_rules -bag_rules %>% -select( -to = bag_color, -from = contains -) %>% -graph_from_data_frame(directed = TRUE) -> g -g %>% -subcomponent("shiny gold", "out") %>% -names() %>% -grep("shiny gold|no other", ., value = TRUE, invert = TRUE) %>% -length() -igraph::bfs( -graph = g, -root = "shiny gold", -neimode = "out", -unreachable = FALSE, -father = TRUE, -pred = TRUE, -) -> tmp -ig <- induced_subgraph(g, na.exclude(as.numeric(tmp$order)), impl = "create_from_scratch") -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -> paths -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -cat(.x[curr+1], .x[curr], "\n") -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) -curr <- 1 -res <- 1 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -all_simple_paths(ig, 1) %>% -map(names) -> paths -paths %>% -map_dbl(~{ -.x <- rev(head(.x, -1)) +visits = 0 +) -> asm +orig_program <- asm +for (run in seq_len(nrow(asm))) { +asm <- orig_program curr <- 1 -res <- 1 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -> paths -paths %>% -map_dbl(~{ -# .x <- rev(head(.x, -1)) -.x <- rev(.x) -curr <- 1 -res <- 1 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -res <- res * ct -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths %>% -map_dbl(~{ -# .x <- rev(head(.x, -1)) -.x <- rev(.x) -curr <- 1 -res <- 0 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -if (ct == 0) { -res <- res + 1 -} else { -res <- res*ct -} -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths %>% -map_dbl(~{ -# .x <- rev(head(.x, -1)) -.x <- rev(.x) -curr <- 1 -res <- 0 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -if (ct == 0) { -res <- res + 1 -} else { -res <- res + res*ct -} -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths %>% -map_dbl(~{ -# .x <- rev(head(.x, -1)) -.x <- rev(.x) -curr <- 1 -res <- 0 -repeat { -ct <- filter(bag_rules, bag_color == .x[curr+1] & contains == .x[curr])$count -cat(.x[curr+1], .x[curr], ct, "\n") -if (ct == 0) { -res <- res + 1 -} else { -res <- res*ct -} -curr <- curr + 1 -if (.x[curr] == "shiny gold") break -} -res -}) %>% -{ sum(.) + length(paths) - 1 } -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[curr] & contains == .x[curr+1])$count -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(.x[curr+1], .x[curr], ct, "\n") -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count) -cat(.x[start], .x[start+1], ct, "\n") -if (ct == 0) ct <- 1 -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(.x[start], .x[start+1], ct, "\n") -if (ct == 0) ct <- 1 -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths -ig -all_simple_paths -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -> paths -paths -all_simple_paths(ig, 1) %>% -map(names) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(.x[start], .x[start+1], ct, "\n") -if (ct == 0) ct <- 1 -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(.x[start], .x[start+1], ct, "\n") -if (ct == 0) ct <- 1 -return(ct + ct * counter(path, start+1)) -} -counter(.x, 1) -}) -2 * ( 2 * (2 * ( 2 * ( 2 * (2))))) -# input <- read_lines("/tmp/test.txt") -input <- read_lines("/tmp/test-orig.txt") -input %>% -stri_match_first_regex( -"^([[:alpha:][:space:]]+)[[:space:]]bags[[:space:]](.*)$", -) %>% -.[,2:3] %>% -as.data.frame() %>% -as_tibble() %>% -rename( -bag_color = 1, -rule = 2 +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" ) %>% -separate_rows(rule, sep=", ") %>% mutate( -count = case_when( -stri_detect_regex(rule, "^[[:digit:]]") ~ stri_match_first_regex(rule, "^([[:digit:]]+)")[,2], -stri_detect_regex(rule, "contain [[:digit:]]") ~ stri_match_first_regex(rule, "^contain ([[:digit:]]+)")[,2], -stri_detect_fixed(rule, "no other") ~ "0" -) %>% -as.integer(), -contains = stri_match_first_regex(rule, "[[:space:]]([[:alpha:][:space:]]+)[[:space:]]ba")[,2] -) %>% -select(bag_color, contains, count) -> bag_rules -bag_rules %>% -select( -to = bag_color, -from = contains -) %>% -graph_from_data_frame(directed = TRUE) -> g -g %>% -subcomponent("shiny gold", "out") %>% -names() %>% -grep("shiny gold|no other", ., value = TRUE, invert = TRUE) %>% -length() -igraph::bfs( -graph = g, -root = "shiny gold", -neimode = "out", -unreachable = FALSE, -father = TRUE, -pred = TRUE, -) -> tmp -ig <- induced_subgraph(g, na.exclude(as.numeric(tmp$order)), impl = "create_from_scratch") -all_simple_paths(ig, 1) %>% -map(names) %>% -keep(~"no other" %in% .x) -> paths -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -if (ct == 0) ct <- 1 -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -if (ct == 0) ct <- 1 -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1)) -} -counter(.x, 1) -}) %>% -sum() -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -if (ct == 0) ct <- 1 -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1) + 1) -} -counter(.x, 1) -}) %>% -sum() -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -bag <- if (ct == 0) 0 else 1 -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1) + bag) -} -counter(.x, 1) -}) %>% -sum() -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -bag <- if (ct == 0) 0 else 1 -cat(.x[start], .x[start+1], ct, "\n") -return(bag * counter(path, start+1) + bag) -} -counter(.x, 1) -}) %>% -sum() -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -bag <- if (ct == 0) 0 else 1 -if (ct == 0) ct <- 1 -cat(.x[start], .x[start+1], ct, "\n") -return(ct * counter(path, start+1) + bag) -} -counter(.x, 1) -}) %>% -sum() -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(1) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -bag <- if (ct == 0) 0 else 1 -if (ct == 0) ct <- 1 -cat(.x[start], .x[start+1], ct, "\n\n") -return(ct * counter(path, start+1) + bag) -} -counter(.x, 1) -}) %>% -sum() -(1 + 3 + 1 + 4 + 1 + 2 + 5 + 1 + 2 + 6 + 1) -length(paths) -27+4 -paths %>% -map_dbl(~{ -counter <- function(path, start) { -if (path[start] == "no other") return(0) -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -if (ct == 0) -return(ct * counter(path, start+1) + bag) -} -1 + counter(.x, 1) -}) %>% -sum() -1 + (1*3) + (1*4) + 2 + (2*5) + (2*6) -all_simple_paths(ig, 1) -all_simple_paths(ig, 1) %>% -map(names) -all_simple_paths(ig, 1) %>% -map_chr(names) -all_simple_paths(ig, 1) %>% -map(names) -all_simple_paths(ig, 1) %>% -map(names) -> paths -for (path in paths) { -ct <- filter(bag_rules, bag_color == .x[start] & contains == .x[start+1])$count -cat(path, ct, "\n") -} +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]) diff --git a/R/.Rproj.user/shared/notebooks/paths b/R/.Rproj.user/shared/notebooks/paths index ba3b65e..911ed33 100644 --- a/R/.Rproj.user/shared/notebooks/paths +++ b/R/.Rproj.user/shared/notebooks/paths @@ -26,6 +26,7 @@ /Users/hrbrmstr/Development/2020-code-advent/js/03.js="E24752E6" /Users/hrbrmstr/Development/2020-code-advent/js/04.js="CB04A9CB" /Users/hrbrmstr/Development/2020-code-advent/js/05.js="AFC9E61D" +/Users/hrbrmstr/Development/2020-code-advent/js/08.js="E730C21E" /Users/hrbrmstr/Development/2020-code-advent/scala/01/01/src/main/scala/Main.scala="8F03A1D5" /Users/hrbrmstr/Development/2020-code-advent/scala/02/02/src/main/scala/Main.scala="418A1A5F" /private/tmp/test.txt="1CB0AD22" diff --git a/R/09.R b/R/09.R index db58c4a..0751c9e 100644 --- a/R/09.R +++ b/R/09.R @@ -75,9 +75,9 @@ 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 +condition <- TRUE -while(no_sum_found && (length(input)>=(preamble_length + 1))) { +while(condition && (length(input)>=(preamble_length + 1))) { as.data.frame(t(combn(window, 2))) %>% mutate( @@ -86,9 +86,9 @@ while(no_sum_found && (length(input)>=(preamble_length + 1))) { sum = input[V1] + input[V2] ) -> pair_sums - no_sum_found <- any(pair_sums$sum == input[input_start]) + condition <- any(pair_sums$sum == input[input_start]) - if (no_sum_found) input <- tail(input, -1) + if (condition) input <- tail(input, -1) } @@ -133,7 +133,7 @@ while(no_sum_found && (length(input)>=(preamble_length + 1))) { # producing 62. # # What is the encryption weakness in your XMAS-encrypted list of numbers? -# s +# # 09-02 ------------------------------------------------------------------- # input <- test_input diff --git a/js/09.js b/js/09.js new file mode 100644 index 0000000..35cae9a --- /dev/null +++ b/js/09.js @@ -0,0 +1,67 @@ +var fs = require("fs") +var combn = require('generatorics') // npm install generatorics + +Array.prototype.accumulate = function(ƒ) { + res = [this[0]] + for (var idx=1; idx parseInt(x)) + +//input = [ 35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576 ] + +preamble_length = 25 +input_start = preamble_length +condition = true + +while(condition && (input.length > (preamble_length))) { + + for (var pair of combn.combination([...Array(preamble_length).keys()], 2)) { + condition = (input[pair[0]] + input[pair[1]]) == input[input_start] + if (condition) break + } + + if (condition) input.shift() + +} + +invalid = input[input_start] + +console.log(invalid) +// 09-02 + +input = fs.readFileSync("../input/09-01.txt", "utf-8") + .split("\n") + .map((x) => parseInt(x)) + +//input = [ 35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576 ] + +const cumsum = (sum => value => sum += value)(0) + +for (var idx=0; idx a + b) + + if (csum.includes(invalid)) break // 'invalid' comes from part 1 + +} + +rng = input.slice(idx, idx+csum.indexOf(invalid)) + +console.log(Math.min(...rng) + Math.max(...rng)) + + + + + + + + + + +