— R Day 5 Part 1 —
solve_day5_part1 <- function(input) {
split_at <- which(input == "")
ranges <- input[1:(split_at - 1)]
ranges <- read.table(
text = ranges,
sep = "-",
col.names = c("start", "end")
)
ids <- input[(split_at + 1L):length(input)]
ids <- as.numeric(ids)
fresh_ids <- vapply(
ids,
\(id) any(id >= ranges[["start"]] & id <= ranges[["end"]]),
logical(1)
)
sum(fresh_ids)
}
Run
aoc_source(day = 5, part = 1)
input = aoc_read(day = 5)
aoc_run(solve_day5_part1(input))
Elapsed: 0.023 seconds
Memory: 2473 KB
— R Day 5 Part 2 —
solve_day5_part2 <- function(input) {
split_at <- which(input == "")
ranges <- input[1:(split_at - 1)]
ranges <- read.table(
text = ranges,
sep = "-",
col.names = c("start", "end")
)
ranges <- sort_by(ranges, ranges[["start"]])
rows_to_check <- 2:NROW(ranges)
new_ranges <- ranges
new_ranges[rows_to_check, ] <- NA_integer_
prev_row <- 1L
for (row in rows_to_check) {
check_row <- ranges[row, ]
stored_row <- new_ranges[prev_row, ]
extends_prev <- check_row[["start"]] <= stored_row[["end"]] &&
check_row["end"] >= stored_row["end"]
if (extends_prev) {
new_ranges[prev_row, "end"] <- ranges[row, "end"]
next
}
within_prev <- check_row[["start"]] >= stored_row[["start"]] &&
check_row[["end"]] <= stored_row[["end"]]
if (!within_prev) {
new_ranges[prev_row + 1L, ] <- check_row
prev_row <- prev_row + 1L
}
}
sum(new_ranges[["end"]] - new_ranges[["start"]] + 1L, na.rm = TRUE)
}
Run
aoc_source(day = 5, part = 2)
input = aoc_read(day = 5)
aoc_run(solve_day5_part2(input))
Elapsed: 0.026 seconds
Memory: 691 KB
— Python Day 5 Part 1 —
def solve_day5_part1(text):
split_at = text.index("\n")
ranges = text[:split_at]
ranges = [line.split("-") for line in ranges]
ranges = [[int(range[0]), int(range[1].rstrip())] for range in ranges]
ids = text[(split_at+1):]
ids = [int(id) for id in ids]
id_count = 0
for id in ids:
for range in ranges:
if id >= range[0] and id <= range[1]:
id_count += 1
break
return(id_count)
Run
aoc_source(day = 5, part = 1)
input = aoc_read(day = 5)
result = aoc_run("solve_day5_part1(input)")
Elapsed: 0.006 seconds
Memory: 87 KB
— Python Day 5 Part 2 —
def solve_day5_part2(text):
split_at = text.index("\n")
ranges = text[:split_at]
ranges = [line.split("-") for line in ranges]
ranges = [[int(range[0]), int(range[1].rstrip())] for range in ranges]
ranges = sorted(ranges, key = lambda range: range[0])
new_ranges = [ranges[0]]
prev_ptr = 0
for try_range in range(1, len(ranges)):
try_lower, try_upper = ranges[try_range]
prev_lower, prev_upper = new_ranges[prev_ptr]
extends_prev = try_lower <= prev_upper and try_upper >= prev_upper
if extends_prev:
new_ranges[prev_ptr] = [prev_lower, try_upper]
continue
within_prev = try_lower >= prev_lower and try_upper <= prev_upper
if not within_prev:
new_ranges += [[try_lower, try_upper]]
prev_ptr += 1
id_count = 0
for new_range in new_ranges:
id_count += new_range[1] - new_range[0] + 1
return(id_count)
Run
aoc_source(day = 5, part = 2)
input = aoc_read(day = 5)
result = aoc_run("solve_day5_part2(input)")
Elapsed: 0.007 seconds
Memory: 83 KB
Back to top