Run
aoc_source(day = 3, part = 1)
input = aoc_read(day = 3)
aoc_run(solve_day3_part1(input))
Elapsed: 0.012 seconds
Memory: 1110 KB
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8 Day 9 Day 10 Day 11 Day 12 Day 13 Day 14 Day 15 Day 16
#' Sum of multiplications
#'
#' Uses regex to find numbers in format mul(x, y). Multiplies each pair and
#' adds the results together.
#'
#' @param input
#' Character vector of lines to parse for numbers to multiply.
#'
#' @return
#' The sum of results of multiplications
solve_day3_part1 <- function(input) {
muls <- stringi::stri_extract_all_regex(input,
"(?<=mul\\()[0-9]+,[0-9]+(?=\\))")
muls <- unlist(muls)
muls <- strsplit(muls, ",")
muls <- purrr::list_transpose(muls)
muls <- lapply(muls, as.numeric)
sum(muls[[1]] * muls[[2]])
}
aoc_source(day = 3, part = 1)
input = aoc_read(day = 3)
aoc_run(solve_day3_part1(input))
Elapsed: 0.012 seconds
Memory: 1110 KB
#' Sum of enabled multiplications
#'
#' Add do() to the start of the input. Uses regex to split the input on do() and
#' don't(), removing all the sections that start with don't(). Then find the sum
#' of multiplying remaining pairs.
#'
#' @param input
#' Character vector of lines to parse for numbers to multiply.
#'
#' @return
#' The sum of results of enabled multiplications
solve_day3_part2 <- function(input) {
line <- paste0(input, collapse = "")
line <- paste0("do()", line)
split <- stringi::stri_split_regex(line, "(?=do\\(\\))|(?=don't\\(\\))")
split <- unlist(split)
split <- split[substring(split, 1, 4) == "do()"]
mul_sums <- lapply(split, sum_muls)
sum(unlist(mul_sums))
}
#' Sum of multiplications
#'
#' Same as part 1 solution. Uses regex to find numbers in format mul(x, y).
#' Multiplies each pair and adds the results together.
#'
#' @param input
#' Character vector of lines to parse for numbers to multiply.
#'
#' @return
#' The sum of results of multiplications
sum_muls <- function(section) {
muls <- stringi::stri_extract_all_regex(section,
"(?<=mul\\()[0-9]+,[0-9]+(?=\\))")
if (is.na(muls)) return(0)
muls <- unlist(muls)
muls <- strsplit(muls, ",")
muls <- purrr::list_transpose(muls)
muls <- lapply(muls, as.numeric)
sum(muls[[1]] * muls[[2]])
}
aoc_source(day = 3, part = 2)
input = aoc_read(day = 3)
aoc_run(solve_day3_part2(input))
Elapsed: 0.016 seconds
Memory: 142 KB
import re
def solve_day3_part1(text):
"""
Sum of multiplications
Uses regex to find numbers in format mul(x, y). Multiplies each pair and
adds the results together.
Parameters
----------
text : list of str
Lines to parse for numbers to multiply.
Returns
-------
int
The sum of results of multiplications
"""
muls = [re.findall("(?<=mul\()[0-9]+,[0-9]+(?=\))", line) for line in text]
muls = [pair for line in muls for pair in line]
muls = [pair.split(",") for pair in muls]
muls = [[int(num) for num in pair] for pair in muls]
muls = [pair[0] * pair[1] for pair in muls]
return(sum(muls))
import re
def solve_day3_part2(text):
"""
Sum of enabled multiplications
Add do() to the start of the input. Uses regex to split the input on do() and
don't(), removing all the sections that start with don't(). Then find the sum
of multiplying remaining pairs.
Parameters
----------
text : list of str
Lines to parse for numbers to multiply.
Returns
-------
int
The sum of results of enabled multiplications
"""
line = "".join(text)
line = "do()" + line
splits = re.split("(?=do\(\))|(?=don't\(\))", line)
splits = [line for line in splits if line[0:5] != "don't"]
mul_sums = [sum_muls(line) for line in splits]
return(sum(mul_sums))
def sum_muls(section):
"""
Sum of multiplications
Uses regex to find numbers in format mul(x, y). Multiplies each pair and
adds the results together.
Parameters
----------
section : list of str
Lines to parse for numbers to multiply.
Returns
-------
int
The sum of results of multiplications
"""
muls = re.findall("(?<=mul\()[0-9]+,[0-9]+(?=\))", section)
if len(muls) == 0:
return(0)
muls = [pair.split(",") for pair in muls]
muls = [[int(num) for num in pair] for pair in muls]
muls = [pair[0] * pair[1] for pair in muls]
return(sum(muls))