input <- readLines("2017/01-input")
chars <- as.numeric(strsplit(input, "")[[1]])
sum(chars[chars == c(chars[-1], chars[1])])
[1] 1044
0.002 sec elapsed
[1] 1054
0.002 sec elapsed
[1] 32020
0.358 sec elapsed
0.088 sec elapsed
input <- scan("2017/03-input")
center <- 1
right <- 2
up <- 4
left <- 6
down <- 8
ring <- 1
repeat {
ring <- ring + 1
right <- down + ring + (ring - 1)
up <- right + ring * 2
left <- up + ring * 2
down <- left + ring * 2
if (any(c(right, up, left, down) >= input)) break
}
min(abs(c(right, up, left, down) - input)) + ring
[1] 371
0.004 sec elapsed
input <- scan("2017/03-input")
get_sum <- function(x, values) {
neightbor_locs <- purrr::pmap_chr(
expand.grid(x = c(-1, 0, 1) + x[1], y = c(-1, 0, 1) + x[2]),
function(x, y) paste0(x, "_", y)
)
sum(unlist(values[neightbor_locs]), na.rm = TRUE)
}
loc <- c(0, 0)
values <- list()
values[paste0(loc, collapse = "_")] <- 1
ring <- 1
# repeat
ring <- 0
repeat {
if (value > input) break
ring <- ring + 1
# right (advance)
loc <- loc + c(1, 0)
value <- get_sum(loc, values)
values[paste0(loc, collapse = "_")] <- value
cat("loc:", loc, "value:", value, "\n")
if (value > input) break
# go up
while (value <= input) {
loc <- loc + c(0, 1)
value <- get_sum(loc, values)
values[paste0(loc, collapse = "_")] <- value
cat("loc:", loc, "value:", value, "\n")
if (identical(loc, c(ring, ring))) break
if (value > input) break
}
# go left
while (value <= input) {
loc <- loc + c(-1, 0)
value <- get_sum(loc, values)
values[paste0(loc, collapse = "_")] <- value
cat("loc:", loc, "value:", value, "\n")
if (identical(loc, c(-ring, ring))) break
if (value > input) break
}
# go down
while (value <= input) {
loc <- loc + c(0, -1)
value <- get_sum(loc, values)
values[paste0(loc, collapse = "_")] <- value
cat("loc:", loc, "value:", value, "\n")
if (identical(loc, c(-ring, -ring))) break
if (value > input) break
}
# go right
while (value <= input) {
loc <- loc + c(1, 0)
value <- get_sum(loc, values)
values[paste0(loc, collapse = "_")] <- value
cat("loc:", loc, "value:", value, "\n")
if (identical(loc, c(ring, -ring))) break
if (value > input) break
}
}
value
[1] 9132360
0.015 sec elapsed
[1] 386
0.08 sec elapsed
[1] 208
0.218 sec elapsed
input <- as.numeric(readLines("2017/05-input"))
len <- length(input)
index <- 1
steps <- 0
repeat {
steps <- steps + 1
offset <- input[index]
input[index] <- input[index] + 1
index <- index + offset
if (index < 1 | index > len) break
}
steps
[1] 364539
0.164 sec elapsed
input <- as.numeric(readLines("2017/05-input"))
len <- length(input)
index <- 1
steps <- 0
repeat {
steps <- steps + 1
offset <- input[index]
if (offset >= 3) {
input[index] <- input[index] - 1
} else {
input[index] <- input[index] + 1
}
index <- index + offset
if (index < 1 | index > len) break
}
steps
[1] 27477714
8.334 sec elapsed
input <- scan("2017/06-input")
seen <- paste(input, collapse = " ")
input_len <- length(input)
cycles <- 0
repeat {
cycles <- cycles + 1
index <- min(which(max(input) == input))
blocks <- input[index]
input[index] <- 0
while (blocks > 0) {
index <- index + 1
if (index > input_len) {
index <- 1
}
input[index] <- input[index] + 1
blocks <- blocks - 1
}
if (paste(input, collapse = " ") %in% seen) break
seen <- c(seen, paste(input, collapse = " "))
}
cycles
length(seen) - which(paste(input, collapse = " ") == seen) + 1
[1] 4
0.006 sec elapsed
input <- scan("2017/06-input")
seen <- paste(input, collapse = " ")
input_len <- length(input)
cycles <- 0
repeat {
cycles <- cycles + 1
index <- min(which(max(input) == input))
blocks <- input[index]
input[index] <- 0
while (blocks > 0) {
index <- index + 1
if (index > input_len) {
index <- 1
}
input[index] <- input[index] + 1
blocks <- blocks - 1
}
if (paste(input, collapse = " ") %in% seen) break
seen <- c(seen, paste(input, collapse = " "))
}
length(seen) - which(paste(input, collapse = " ") == seen) + 1
[1] 4
0.006 sec elapsed
input <- readLines("2017/07-input")
library(stringr)
loc <- str_subset(input, "->")[1] |>
str_extract("[a-z]+")
repeat {
next_id <- str_detect(input, paste0(".+", loc))
if (all(!next_id)) break
loc <- str_extract(input[next_id], "^[a-z]+")
}
loc
[1] "dgoocsw"
0.056 sec elapsed
input <- readLines("2017/07-input")
library(stringr)
loc <- str_subset(input, "->")[1] |>
str_extract("[a-z]+")
repeat {
next_id <- str_detect(input, paste0(".+", loc))
if (all(!next_id)) break
loc <- str_extract(input[next_id], "^[a-z]+")
}
get_children <- function(x, input) {
str_subset(input, paste0("^", x)) |>
str_remove(".*> ") |>
str_split_1(", ")
}
get_weight <- function(x) {
value <- str_subset(input, paste0("^", x))
res <- str_extract(value, "[0-9]+") |> as.integer()
if (str_detect(value, ">")) {
res <- res + sum(purrr::map_int(get_children(x, input), get_weight))
}
res
}
bot <- loc
error <- function(x) max(x) - min(x)
wrong_amount <- error(purrr::map_int(get_children(bot, input), get_weight))
repeat {
children <- get_children(bot, input)
weights <- purrr::map_int(children, get_weight)
if (length(table(weights)) == 1) break
bot <- children[names(table(weights)[table(weights) == 1]) == weights]
}
wrong_weight <- str_subset(input, paste(children, collapse = ", ")) |>
str_extract("[0-9]+") |>
as.integer()
wrong_weight - wrong_amount
[1] 1275
0.571 sec elapsed
input <- readLines("2017/08-input")
inc <- function(x, value) {
name <- deparse(substitute(x))
.GlobalEnv[[name]] <- .GlobalEnv[[name]] + value
}
dec <- function(x, value) {
name <- deparse(substitute(x))
.GlobalEnv[[name]] <- .GlobalEnv[[name]] - value
}
library(stringr)
registers <- str_extract_all(input, "[a-z]+") |>
unlist() |>
unique() |>
setdiff(c("dec", "inc", "if"))
materialize <- function(name) {
.GlobalEnv[[name]] <- 0
}
purrr::walk(registers, materialize)
reorganize <- function(x) {
glue::glue("if ({x[5]} {x[6]} {x[7]}) {x[2]}({x[1]}, {x[3]})")
}
str_split(input, " ") |>
purrr::map_chr(reorganize) |>
parse(text = _) |>
eval()
purrr::map_dbl(registers, ~eval(parse(text = .x))) |>
max()
[1] 4902
0.123 sec elapsed
input <- readLines("2017/08-input")
highest <- 0
inc <- function(x, value) {
name <- deparse(substitute(x))
.GlobalEnv[[name]] <- .GlobalEnv[[name]] + value
.GlobalEnv[["highest"]] <- max(.GlobalEnv[["highest"]], .GlobalEnv[[name]])
}
dec <- function(x, value) {
name <- deparse(substitute(x))
.GlobalEnv[[name]] <- .GlobalEnv[[name]] - value
.GlobalEnv[["highest"]] <- max(.GlobalEnv[["highest"]], .GlobalEnv[[name]])
}
library(stringr)
registers <- str_extract_all(input, "[a-z]+") |>
unlist() |>
unique() |>
setdiff(c("dec", "inc", "if"))
materialize <- function(name) {
.GlobalEnv[[name]] <- 0
}
purrr::walk(registers, materialize)
reorganize <- function(x) {
glue::glue("if ({x[5]} {x[6]} {x[7]}) {x[2]}({x[1]}, {x[3]})")
}
str_split(input, " ") |>
purrr::map_chr(reorganize) |>
parse(text = _) |>
eval()
highest
[1] 7037
0.12 sec elapsed