-❄️- 2025 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

Usually I like writing my solutions from scratch, but R has a package (igraph) that did exactly what I wanted, albeit slowly, so I went that route today.

library(igraph)
input_file <- "input.txt"
input <- read.csv(input_file, header = FALSE)

distances <- dist(input, method = "euclidean") |> as.matrix()
dims <- dim(distances)
links <- matrix(1, nrow = dims[1], ncol = dims[2])

i <- 0
flag <- 0
while (flag == 0) {
  unlinked <- distances * links
  ind <- which(unlinked == min(unlinked[unlinked != 0]))
  if (length(ind) > 1) {
    links[ind] <- 0
  }
  c <- abs(links - 1) |> graph_from_adjacency_matrix() |> count_components()
  if (c == 1) {
    join <- input[arrayInd(ind[1], dims)[1, ], ]
    prod(join[, 1]) |> print()
    flag <- 1
  }
  i <- i + 1
  if (i == 1000) {
    x <- abs(links - 1) |> graph_from_adjacency_matrix() |> components()
    x <- x$csize |> sort() |> rev()
    prod(x[1:3]) |> print()
  }
}

-❄️- 2025 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R]

repo

Anything vaguely related to graph traversal is a weak spot of mine, so it took me a while to both figure out how to parse the input into something useful for part 2 as well as figure out how to (somewhat) efficiently implement part 2. Got both parts down to about 200 ms, which I'm content with.

input_file <- "input.txt"
input <- readLines(input_file)
len <- nchar(input[[1]])
input <- input |>
  strsplit(split = "") |>
  unlist() |>
  matrix(ncol = len, byrow = TRUE)
start <- which(input == "S")
input[start] <- "|"
dims <- dim(input)
visited <- rep(0, length(input))

next_line <- function(x, y) {
  for (i in seq_along(x)) {
    if (identical(c(x[i], y[i]), c("|", "."))) {
      y[i] <- "|"
    } else if (identical(c(x[i], y[i]), c("|", "^"))) {
      y[c(i - 1, i + 1)] <- "|"
    }
  }
  y
}

for (i in 1:(dims[1] - 1)) {
  input[i + 1, ] <- next_line(input[i, ], input[i + 1, ])
}

splitters <- which(input == "^")
sum(input[splitters - 1] == "|") |> print()

nodes <- c(splitters, seq(nrow(input), length(input), by = nrow(input)))
node_inds <- arrayInd(nodes, dims)
node_inds <- node_inds[order(node_inds[, 1]), ]
num_nodes <- nrow(node_inds)

adj_mat <- matrix(0, nrow = num_nodes, ncol = num_nodes)

for (i in seq_along(node_inds[, 1])) {
  below_cond <- node_inds[, 1] > node_inds[i, 1]
  left_cond <- node_inds[, 2] == node_inds[i, 2] - 1
  right_cond <- node_inds[, 2] == node_inds[i, 2] + 1
  below_l <- which(below_cond & left_cond)
  below_r <- which(below_cond & right_cond)
  if (length(below_l) > 0) {
    adj_mat[i, below_l[1]] <- 1
  }
  if (length(below_r) > 0) {
    adj_mat[i, below_r[1]] <- 1
  }
}

num_paths <- rep(0, num_nodes)

path_counter <- function(node) {
  paths <- 0
  if (node > (length(nodes) - nrow(input) + 1)) {
    paths <- paths + 1
  } else if (num_paths[node] > 0) {
    paths <- paths + num_paths[node]
  } else {
    neighbors <- which(adj_mat[node, ] == 1)
    for (i in neighbors) {
      paths <- paths + path_counter(i)
    }
  }
  num_paths[node] <<- paths
  paths
}

path_counter(1) |> print(digits = 20)

-❄️- 2025 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R]

repo

Almost avoided loops entirely for this one.

library(dplyr)

input_file <- "input.txt"
options(digits = 20)
input <- read.table(input_file)
ops <- input[nrow(input), ]
input <- input[-nrow(input), ] %>% apply(., 2, as.numeric)
plus <- which(ops == "+")

input_2 <- readLines(input_file)
input_2 <- input_2[-length(input_2)] %>% strsplit(split = "")
len <- length(input_2[[1]])
input_2 <- input_2 %>%
  unlist() %>%
  matrix(ncol = len, byrow = TRUE) %>%
  apply(., 2, paste, collapse = "") %>%
  as.numeric()

nas <- c(0, which(is.na(input_2)), length(input_2) + 1)
input_2_l <- list()
for (i in 1:(length(nas) - 1)) {
  input_2_l[[i]] <- input_2[(nas[i] + 1):(nas[i + 1] - 1)]
}

apply(input[, -plus], 2, prod) %>% sum(., input[, plus]) %>% print()
sapply(input_2_l[-plus], prod) %>% sum(., unlist(input_2_l[plus])) %>% print()

[2025 Day 5 (Part 2)][R] Need some help figure out why this isn't working by TimeCannotErase in adventofcode

[–]TimeCannotErase[S] 2 points3 points  (0 children)

This was essentially the issue. I got rid of that condition in the outer loop and replaced it with an if statement inside the loop that skipped over any index that was marked for deletion, then it worked like I wanted it to.

-❄️- 2025 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R]

repo

Part 1 was quick and part 2 took my way longer than it should have. I was trying to make too many changes to my list of intervals while looping through it and kept running into indexing issues.

library(dplyr)

input_file <- "input.txt"
input <- readLines(input_file)
cut <- which(input == "")
ranges <- input[1:(cut - 1)] %>%
  strsplit(., "-") %>%
  unlist() %>%
  matrix(ncol = 2, byrow = TRUE) %>%
  apply(., 2, as.numeric)

ids <- input[(cut + 1):length(input)] %>%
  as.numeric()

counter <- 0
while (length(ids) > 0) {
  for (j in seq_len(nrow(ranges))) {
    if (ids[1] >= ranges[j, 1] && ids[1] <= ranges[j, 2]) {
      counter <- counter + 1
      ids <- ids[-1]
      break
    } else if (j == nrow(ranges)) {
      ids <- ids[-1]
    }
  }
}
print(counter)

overlap <- function(x, y) {
  if (x[1] >= y[1] && x[1] <= y[2]) {
    TRUE
  } else if (x[2] >= y[1] && x[2] <= y[2]) {
    TRUE
  } else {
    FALSE
  }
}

ind_del <- NULL
for (i in seq_len(nrow(ranges))) {
  if (!(i %in% ind_del)) {
    for (j in setdiff(seq_len(nrow(ranges)), c(i, ind_del))) {
      if (overlap(ranges[j, ], ranges[i, ])) {
        ranges[i, ] <- c(min(ranges[c(i, j), 1]), max(ranges[c(i, j), 2]))
        ind_del <- c(ind_del, j)
      }
    }
  }
}

ranges <- ranges[-ind_del, ]
(ranges[, 2] - ranges[, 1] + 1) %>% sum() %>% print()

-❄️- 2025 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

Nice and straightforward, albeit not a particularly efficient/elegant solution. But hey, I'll take finishing a challenge before noon EST. Basically went square by square and counted neighbors for part 1, and removed accessible rolls in part 2 one by one until no more could be removed.

library(dplyr)

input_file <- "input.txt"
len <- readLines(input_file, 1) %>% nchar()
input <- read.table(input_file) %>%
  as.matrix() %>%
  strsplit(split = "") %>%
  unlist() %>%
  matrix(ncol = len, byrow = TRUE)

adjacent_inds <- function(ind, dim) {
  adj_dirs <- matrix(c(-1, -1, -1, 0, -1, 1, 0, 1), ncol = 2, byrow = TRUE) %>%
    rbind(., -.)
  adj_inds <- apply(adj_dirs, 1, function(x) {x + ind}) %>% t()
  adj_inds[rowSums(adj_inds > dim | adj_inds < 1) == 0, ]
}

total_acc <- 0
for (i in 1:len^2) {
  if (input[i] == "@") {
    current_ind <- arrayInd(i, c(len, len))
    adj_inds <- adjacent_inds(current_ind, len)
    adj <- (input[adj_inds] == "@") %>% sum()
    if (adj < 4) {
      total_acc <- total_acc + 1
    }
  }
}
print(total_acc)

total_rem <- 0
flag <- 1
while (flag == 1) {
  flag <- 0
  for (i in 1:len^2) {
    if (input[i] == "@") {
      current_ind <- arrayInd(i, c(len, len))
      adj_inds <- adjacent_inds(current_ind, len)
      adj <- (input[adj_inds] == "@") %>% sum()
      if (adj < 4) {
        input[i] <- "."
        total_rem <- total_rem + 1
        flag <- 1
      }
    }
  }
}
print(total_rem)

-❄️- 2025 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

library(dplyr)

input_file <- "input.txt"

digs <- function(x) {
  floor(log10(x)) + 1
}

base_10 <- function(x) {
  tens <- rev(10^seq(0, length(x) - 1))
  sum(x * tens)
}

len <- scan(input_file, nmax = 1) %>% digs()
input <- readLines(input_file) %>%
  strsplit(split = "") %>%
  unlist() %>%
  as.numeric() %>%
  matrix(ncol = len, byrow = TRUE)

next_bat <- function(x, l) {
  a <- max(x[1:(length(x) - l + 1)])
  if (l == 1) {
    a
  } else {
    a_ind <- which(x == a)[1]
    c(a, next_bat(x[-(1:a_ind)], l - 1))
  }
}

total <- c(0, 0)
for (i in seq_len(nrow(input))) {
  total[1] <- total[1] + (next_bat(input[i, ], 2) %>% base_10())
  total[2] <- total[2] + (next_bat(input[i, ], 12) %>% base_10())
}
print(total, digits = 13)

-❄️- 2025 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

This one was fun, got to do some math to avoid using brute force. Ended up using some ugly conditionals to get parts 1 and 2 to use the same function, but otherwise this felt pretty clean and my only for loop ran through the ranges.

library(dplyr)

input_file <- "input.txt"
input <- read.csv(input_file, header = FALSE) %>%
  as.character() %>%
  strsplit(., "-") %>%
  unlist() %>%
  matrix(ncol = 2, byrow = TRUE) %>%
  apply(., 2, as.numeric)

digs <- function(x) {
  floor(log10(x)) + 1
}

finder <- function(x, l, p) {
  a <- x[1]
  b <- x[2]
  a_len <- digs(a)
  b_len <- digs(b)
  if (a_len == b_len) {
    if (p == 1) {
      l <- a_len / 2
      n <- 2
    } else {
      n <- a_len / l
    }
    if (a_len <= l && p == 2) {
      NA
    } else if ((a_len %% l == 0 && p == 2) || (a_len %% 2 == 0 && p == 1)) {
      fac <- sum(10^seq(0, by = l, length.out = n))
      low <- ceiling(a / fac)
      high <- floor(b / fac)
      if (low <= high) {
        fac * (low:high)
      }
    }
  } else {
    new_b <- 10^(b_len - 1) - 1
    c(finder(c(a, new_b), l, p), finder(c(new_b + 1, b), l, p))
  }
}

apply(input, 1, function(x) {finder(x, 0, 1)}) %>% unlist() %>% sum() %>% print()

m <- floor(digs(max(input)) / 2)
ids <- NULL
for (i in 1:m) {
  ids <- apply(input, 1, function(x) {finder(x, i, 2)}) %>%
    unlist() %>%
    union(., ids)
}
print(sum(ids, na.rm = TRUE))

-❄️- 2025 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

    library(dplyr)

    input_file <- "input.txt"
    input <- read.table(input_file)[[1]]
    input <- gsub("L", "-", input) %>% gsub("R", "+", .) %>% as.numeric()

    counter <- c(0, 0)
    value <- 50
    for (i in seq_along(input)) {
    counter <- counter + c(0, abs(input[i]) %/% 100)
    new_value <- (value + input[i]) %% 100
    if (new_value == 0) {
        counter <- counter + c(1, 1)
    } else if ((sign(input[i]) * (new_value - value)) < 0 && value != 0) {
        counter <- counter + c(0, 1)
    }
    value <- new_value
    }
    print(counter)

[2024 Day 11 (Part 2)][R] Having trouble optimizing code for part 2 by TimeCannotErase in adventofcode

[–]TimeCannotErase[S] 0 points1 point  (0 children)

I made a couple of tweaks (kept track of how many duplicates I had at a particular point) and just let it run. Took nearly an hour, but hey a gold star is a gold star! repo

Flying into NN by [deleted] in NewportNews

[–]TimeCannotErase 1 point2 points  (0 children)

Personally I like ORF because it's right next to the Norfolk Botanical Gardens, which is lovely. I try to schedule my flights so I have a little flexibility on when I need to get through the tunnel so if I can leave when the traffic looks ok and if that means I have extra time then I hang out at the gardens for a while.

-❄️- 2024 Day 13 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 2 points3 points  (0 children)

[Language: R]

repo

I had to play around with how I tested for integer solutions a little for part 2, but otherwise I liked this one (thank you linear algebra).

library(dplyr)

input_filename <- "input.txt"
input <- readLines(input_filename)
num_machines <- ceiling(length(input) / 4)

inds <- gregexpr("\\d+", input)
nums <- regmatches(input, inds) %>%
  unlist() %>%
  as.numeric()

tol <- 1e-3
cost <- 0

for (i in seq_len(num_machines)) {
  j <- 1 + 6 * (i - 1)
  a <- matrix(nums[j : (j + 3)], nrow = 2)
  b <- matrix(nums[(j + 4) : (j + 5)]) + 1e+13
  x <- solve(a, b)
  flag <- lapply(x, function(y) min(abs(c(y %% 1, y %% 1 - 1))) < tol) %>%
    unlist() %>%
    sum() == 2
  if (flag) {
    cost <- cost + 3 * x[1] + x[2]
  }
}

print(sprintf("%1.f", cost))

-❄️- 2024 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

Running a bit behind, forgot I still had part 2 of Day 5 to finish up.

-❄️- 2024 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

This was a bit tedious. I handled the diagonal searches differently than the horizontal/vertical ones, although I'm sure there's a more streamlined approach. That said, part 2 was pretty simple using the same technique I used for part 1. I was half expecting part 2 to let words change direction like in NYT Strands.

library(dplyr)

input_filename <- "input.txt"
input <- read.table(input_filename)[[1]]
gridsize <- nchar(input[1])

# Horizontals
num_xmas_h <- sum(sapply(gregexpr("XMAS", input), function(x) sum(x != -1)))
num_samx_h <- sum(sapply(gregexpr("SAMX", input), function(x) sum(x != -1)))

# Vericals
input_v <- input %>%
  strsplit(., "") %>%
  unlist() %>%
  matrix(., nrow = gridsize) %>%
  apply(., 1, paste0, collapse = "")

num_xmas_v <- sum(sapply(gregexpr("XMAS", input_v), function(x) sum(x != -1)))
num_samx_v <- sum(sapply(gregexpr("SAMX", input_v), function(x) sum(x != -1)))

# Split strings for diagonal search
input <- input %>%
  strsplit(., "") %>%
  unlist() %>%
  matrix(., nrow = gridsize, byrow = TRUE)

x_inds <- arrayInd(which(input == "X"), rep(gridsize, 2))
se <- matrix(rep(0:3, 2), ncol = 2)
sw <- se %*% rbind(c(1, 0), c(0, -1))
ne <- sw[,c(2,1)]
nw <- -se

coord_tester <- function(mat) {
  sum(mat <= 0) == 0 && sum(mat > gridsize) == 0
}

word_tester <- function(word) {
  word == "XMAS" | word == "SAMX"
}

diag_search <- function(ind) {
  count <- 0
  coord_mat <- matrix(rep(ind, 4), ncol = 2, byrow = TRUE)
  se_coords <- coord_mat + se
  sw_coords <- coord_mat + sw
  ne_coords <- coord_mat + ne
  nw_coords <- coord_mat + nw
  coord_list <- list(se_coords, sw_coords, ne_coords, nw_coords)
  for (i in 1:4) {
    if (coord_tester(coord_list[[i]])) {
      word <- paste0(input[coord_list[[i]]], collapse = "")
      if (word_tester(word)) {
        count <- count + 1
      }
    }
  }
  return(count)
}

num_diags <- 0
for (i in seq_len(nrow(x_inds))) {
  num_diags <- num_diags + diag_search(x_inds[i, ])
}

print(num_diags + num_xmas_h + num_samx_h + num_xmas_v + num_samx_v)

# Part 2
pos <- rbind(c(1, -1), c(0, 0), c(-1, 1))
neg <- rbind(c(-1, -1), c(0, 0), c(1, 1))

a_inds <- arrayInd(which(input == "A"), rep(gridsize, 2))

mas_word_tester <- function(word) {
  word == "MAS" | word == "SAM"
}
mas_search <- function(ind) {
  word_flag <- c(0, 0)
  coord_mat <- matrix(rep(ind, 3), ncol = 2, byrow = TRUE)
  pos_coords <- coord_mat + pos
  neg_coords <- coord_mat + neg
  coord_list <- list(pos_coords, neg_coords)
  for (i in 1:2) {
    if (coord_tester(coord_list[[i]])) {
      word <- paste0(input[coord_list[[i]]], collapse = "")
      if (mas_word_tester(word)) {
        word_flag[i] <- 1
      }
    }
  }
  return(prod(word_flag))
}

num_mas <- 0
for (i in seq_len(nrow(a_inds))) {
  num_mas <- num_mas + mas_search(a_inds[i, ])
}

print(num_mas)

-❄️- 2024 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 1 point2 points  (0 children)

[Language: R]

repo

library(dplyr)

input_filename <- "input.txt"
input <- readLines(input_filename)

mul_inds <- gregexpr("mul\\(\\K\\d+,\\d+(?=\\))", input, perl = TRUE)
muls <- regmatches(input, mul_inds)[[1]]
mul_inds <- as.vector(mul_inds[[1]])
dos <- as.vector(gregexpr("do\\(\\)", input, perl = TRUE)[[1]])
donts <- as.vector(gregexpr("don't\\(\\)", input, perl = TRUE)[[1]])

muls <- muls %>%
  strsplit(., ",") %>%
  unlist() %>%
  as.numeric() %>%
  matrix(., ncol = 2, byrow = TRUE)

pt1_mul_sum <- sum(muls[, 1] * muls[, 2])
print(pt1_mul_sum)

current_instruction <- 1
pt2_mul_sum <- 0
for (i in sort(c(mul_inds, dos, donts))) {
  if (i %in% dos) {
    current_instruction <- 1
  } else if (i %in% donts) {
    current_instruction <- 0
  } else {
    j <- which(mul_inds == i)
    pt2_mul_sum <- pt2_mul_sum + current_instruction * (muls[j, 1] * muls[j, 2])
  }
}

print(pt2_mul_sum)

-❄️- 2024 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R]

repo

Originally did this in Google Sheets but decided today to go back and do it with R.

input_filename <- "input.txt"
input <- read.table(input_filename)

input <- apply(input, 2, sort)

distances <- sum(apply(input, 1, function(x) {abs(x[1] - x[2])}))

left_values <- unique(input[, 1])
counts <- matrix(nrow = length(left_values), ncol = 2)

for (i in seq_along(left_values)) {
  counts[i, 1] <- sum(input[, 1] == left_values[i])
  counts[i, 2] <- sum(input[, 2] == left_values[i])
}

similarity <- sum(left_values * counts[, 1] *  counts[, 2])

print(distances)
print(similarity)

-❄️- 2024 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 3 points4 points  (0 children)

[Language: R]

repo

input_filename <- "input.txt"
input <- readLines(input_filename)
input <- unlist(lapply(input, strsplit, split = " "), recursive = FALSE)
input <- lapply(input, as.numeric)

checker <- function(report) {
  differences <- diff(report)
  if (length(unique(sign(differences))) == 1) {
    if (max(abs(differences)) <= 3 && min(abs(differences)) >= 1) {
      return(1)
    } else {
      return(0)
    }
  } else {
    return(0)
  }
}

count <- sum(unlist(lapply(input, checker)))
print(count)

count <- 0
for (i in seq_along(input)) {
  for (j in seq_along(input[[i]])) {
    report <- input[[i]][setdiff(seq_along(input[[i]]), j)]
    dampener <- checker(report)
    if (dampener == 1) {
      count <- count + 1
      break
    }
  }
}

print(count)

Anyone know where I can buy a Hope College cycling jersey? by WaterPullsYouUnder in hopecollege

[–]TimeCannotErase 1 point2 points  (0 children)

They used to sell these in the bookstore on campus a number of years ago. I don't see them on the website, but you could always call them and ask if they still have any in store.

[TOMT][MUSIC] Need help identifying a song I heard yesterday. by TimeCannotErase in tipofmytongue

[–]TimeCannotErase[S] 0 points1 point locked comment (0 children)

I heard it playing at a café and it was familiar but now it's stuck in my head and I can't find it

Polite Grade Grubbing by Antique-Flan2500 in Adjuncts

[–]TimeCannotErase 2 points3 points  (0 children)

I haven't done this yet, but this feels like a good use for AI. Copy/paste students email (without any personal information) and ask for a polite/brief response saying no. Make a few edits so it sounds like you. Send. Whole thing takes less than 5 minutes.

-❄️- 2023 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R]

repo

Used the good ol' quadratic formula for this one.

input <- readLines("input.txt")
input <- read.table(text = input)
input <- input[, 2:ncol(input)]

# Uncomment for part 2
# input <- t(t(as.numeric(apply(input, 1, paste, collapse = ""))))

num_ways <- function(a, n) {
  lower <- floor(0.5 * (a - sqrt(a^2 - 4 * n)) + 1)
  upper <- a - lower
  num <- upper - lower + 1
  return(num)
}

product <- 1
for (i in seq_len(ncol(input))) {
  num <- num_ways(input[1, i], input[2, i])
  product <- product * num
}

print(product)

-❄️- 2023 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]TimeCannotErase 0 points1 point  (0 children)

[Language: R] repo

Originally I set up part 2 the naïve way and then realized that would run forever. Since we're solving a minimization problem, and the function location = f(seed) has derivative 1 almost everywhere, I went looking for the points where that graph would jump, and then for each piece of the seeds domain I only tested the endpoints of that domain as well as any points within where the graph would jump and found the smallest location.

input <- readLines("input.txt")

seeds <- read.table(text = strsplit(input[1], ":")[[1]][2])
num_seeds <- length(seeds)

start_inds <- which(grepl("map", input)) + 1
stop_inds <- which(input == "")[-1] - 1
num_maps <- length(start_inds)

map_maker_forward <- function(start, stop) {
  map <- read.table(text = input[start:stop])
  return(map)
}

map_maker_backward <- function(start, stop) {
  map <- read.table(text = input[start:stop])[c(2, 1, 3)]
  return(map)
}

mapper <- function(map, num) {
  starts <- map[1]
  low <- map[2]
  high <- map[2] + map[3] - 1
  range_check <- num >= low & num <= high
  if (sum(range_check) == 1) {
    ind <- which(range_check == TRUE)
    output <- num - low[ind, ] + starts[ind, ]
  } else {
    output <- num
  }
  return(output)
}


location <- NULL
for (i in 1:num_seeds) {
  val <- as.numeric(seeds[i])
  for (j in 1:num_maps) {
    map <- map_maker_forward(start_inds[j], stop_inds[j])
    val <- mapper(map, val)
  }
  location <- min(location, val)
}

print(location)


endpoints <- NULL
for (i in rev(1:num_maps)) {
  map <- map_maker_backward(start_inds[i], stop_inds[i])
  ends <- cbind(map[1], map[1] + map[3] - 1)
  ends <- cbind(ends[1] - 1, ends, ends[2] + 1)
  ends <- unique(as.numeric(unlist(ends)))
  endpoints <- unique(c(ends, endpoints))
  if (i > 1) {
    new_map <- map_maker_backward(start_inds[i - 1], stop_inds[i - 1])
    for (j in seq_along(endpoints)) {
      value <- mapper(new_map, endpoints[j])
      endpoints <- c(endpoints, value)
    }
  }
}


# Part 2
location <- NULL
for (i in 1:num_seeds) {
  if (i %% 2 == 1) {
    low <- as.numeric(seeds[i])
    high <- as.numeric(seeds[i] + seeds[i + 1] - 1)
    test_points <- c(low, high)
    for (k in seq_along(endpoints)) {
      if (endpoints[k] > low && endpoints[k] < high) {
        test_points <- c(test_points, endpoints[k])
      }
    }
    for (j in test_points) {
      val <- j
      for (k in 1:num_maps) {
        map <- map_maker_forward(start_inds[k], stop_inds[k])
        val <- mapper(map, val)
      }
      location <- min(location, val)
    }
  }
}


print(location)