Skip to content

Commit

Permalink
Day 2 in the books
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerNet committed Dec 2, 2023
1 parent f805224 commit 98d1de9
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ test_lib = []

[dependencies]
pico-args = "0.5.0"
regex = "1.10.2"
5 changes: 5 additions & 0 deletions data/examples/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
83 changes: 83 additions & 0 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
advent_of_code::solution!(2);

use regex::Regex;

pub fn part_one(input: &str) -> Option<u32> {
Some(
input
.lines()
.filter_map(|line| {
let game_re = Regex::new(r"^Game (\d+):").unwrap();
let red_re = Regex::new(r" (\d+) red").unwrap();
let green_re = Regex::new(r" (\d+) green").unwrap();
let blue_re = Regex::new(r" (\d+) blue").unwrap();
let game_id = game_re.captures(line).expect("Expected game_id to exist")[1]
.parse::<u32>()
.expect("Expect game_id to be an int");
let reds = red_re
.captures_iter(line)
.filter(|c| c[1].parse::<u32>().expect("Red capture should be int") > 12)
.count();
let greens = green_re
.captures_iter(line)
.filter(|c| c[1].parse::<u32>().expect("Green capture should be int") > 13)
.count();
let blues = blue_re
.captures_iter(line)
.filter(|c| c[1].parse::<u32>().expect("Blue capture should be int") > 14)
.count();
if reds != 0 || greens != 0 || blues != 0 {
None
} else {
Some(game_id)
}
})
.sum(),
)
}

pub fn part_two(input: &str) -> Option<u32> {
Some(
input
.lines()
.filter_map(|line| {
let red_re = Regex::new(r" (\d+) red").unwrap();
let green_re = Regex::new(r" (\d+) green").unwrap();
let blue_re = Regex::new(r" (\d+) blue").unwrap();
let reds = red_re
.captures_iter(line)
.map(|c| c[1].parse::<u32>().expect("Red capture should be int"))
.reduce(|acc, e| if e > acc { e } else { acc })
.unwrap();
let greens = green_re
.captures_iter(line)
.map(|c| c[1].parse::<u32>().expect("green capture should be int"))
.reduce(|acc, e| if e > acc { e } else { acc })
.unwrap();
let blues = blue_re
.captures_iter(line)
.map(|c| c[1].parse::<u32>().expect("blue capture should be int"))
.reduce(|acc, e| if e > acc { e } else { acc })
.unwrap();
Some(reds * greens * blues)
})
.sum(),
)
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(8));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(2286));
}
}

0 comments on commit 98d1de9

Please sign in to comment.