Skip to content

Commit

Permalink
AOC 2024 day 03
Browse files Browse the repository at this point in the history
Signed-off-by: Mårten Kongstad <[email protected]>
  • Loading branch information
amhk committed Dec 3, 2024
1 parent 4e74522 commit de17d39
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 2024/03/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "aoc-2024-03"
version = "0.1.0"
authors = ["Mårten Kongstad <[email protected]>"]
edition = "2021"

[dependencies]
anyhow = "1.0"
aoc = { path = "../../aoc" }
regex = "1.11.1"
6 changes: 6 additions & 0 deletions 2024/03/src/input.txt

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions 2024/03/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use anyhow::Result;
use regex::Regex;

fn main() -> Result<()> {
let input = include_str!("input.txt");
aoc::run!(part_one(input), 173517243)?;
aoc::run!(part_two(input), 100450138)?;
Ok(())
}

fn parse(input: &str, support_toggle_instr: bool) -> Result<Vec<(usize, usize)>> {
let re = if support_toggle_instr {
Regex::new(r#"do\(\)|don't\(\)|mul\((\d+),(\d+)\)"#).unwrap()
} else {
Regex::new(r#"mul\((\d+),(\d+)\)"#).unwrap()
};
let mut enabled = true;
let mut out = vec![];
for caps in re.captures_iter(input) {
if caps[0].starts_with("mul(") {
if enabled {
let a = caps[1].parse::<usize>()?;
let b = caps[2].parse::<usize>()?;
out.push((a, b));
}
} else if caps[0] == *"don't()" {
enabled = false;
} else if caps[0] == *"do()" {
enabled = true;
};
}
Ok(out)
}

fn part_one(input: &str) -> Result<usize> {
let pairs = parse(input, false)?;
Ok(pairs.into_iter().map(|(a, b)| a * b).sum())
}

fn part_two(input: &str) -> Result<usize> {
let pairs = parse(input, true)?;
Ok(pairs.into_iter().map(|(a, b)| a * b).sum())
}

#[cfg(test)]
mod tests {
use super::*;

const INPUT_PART_ONE: &str = include_str!("test-input-part-one.txt");
const INPUT_PART_TWO: &str = include_str!("test-input-part-two.txt");

#[test]
fn test_parse() {
assert_eq!(
parse(INPUT_PART_ONE, false).unwrap(),
vec![(2, 4), (5, 5), (11, 8), (8, 5)]
);
assert_eq!(parse(INPUT_PART_TWO, true).unwrap(), vec![(2, 4), (8, 5)]);
}

#[test]
fn test_part_one() {
assert_eq!(part_one(INPUT_PART_ONE).unwrap(), 161);
}

#[test]
fn test_part_two() {
assert_eq!(part_two(INPUT_PART_TWO).unwrap(), 48);
}
}
1 change: 1 addition & 0 deletions 2024/03/src/test-input-part-one.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
1 change: 1 addition & 0 deletions 2024/03/src/test-input-part-two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de17d39

Please sign in to comment.