diff --git a/README.md b/README.md index f5f9a73..e884adb 100644 --- a/README.md +++ b/README.md @@ -27,27 +27,27 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles. > All benchmarks are run on a *Dell Precision 5550* laptop with an *Intel Core i7-10875H* CPU. - +
SolutionPart 1 PerformancePart 2 Performance
DaysPart 1 PerformancePart 2 Performance
-| Day | Code | Input | -| :------------------------------------------------------: | :------------------------------: | :---------------------------------------: | -| [1: Trebuchet?!](https://adventofcode.com/2023/day/1) | [`day1.rs`](aoc2023/src/day1.rs) | [`day1.txt`](aoc2023/input/2023/day1.txt) | -| [1: Cube Conundrum](https://adventofcode.com/2023/day/2) | [`day2.rs`](aoc2023/src/day2.rs) | [`day2.txt`](aoc2023/input/2023/day2.txt) | +| Puzzle | Code | Input | +| :---------------------------------------------------: | :------------------------------: | :---------------------------------------: | +| [Trebuchet?!](https://adventofcode.com/2023/day/1) | [`day1.rs`](aoc2023/src/day1.rs) | [`day1.txt`](aoc2023/input/2023/day1.txt) | +| [Cube Conundrum](https://adventofcode.com/2023/day/2) | [`day2.rs`](aoc2023/src/day2.rs) | [`day2.txt`](aoc2023/input/2023/day2.txt) | -| Generator | Runner | -| :-------: | :-----: | -| 4.21 µs | 47.4 µs | -| 508 µs | 1.12 µs | +| Generator | Runner | +| :-------: | :------: | +| 5.450 µs | 49.58 µs | +| 476.4 µs | 1.031 µs | -| Generator | Runner | -| :-------: | :-----: | -| 0.42 µs | 753 µs | -| 323 µs | 1.14 µs | +| Generator | Runner | +| :-------: | :------: | +| 0.471 µs | 795.3 µs | +| 297.5 µs | 1.076 µs |
diff --git a/aoc2023/src/day2.rs b/aoc2023/src/day2.rs index 8d0d46e..2d13a94 100644 --- a/aoc2023/src/day2.rs +++ b/aoc2023/src/day2.rs @@ -1,4 +1,5 @@ use aoc_runner_derive::{aoc, aoc_generator}; +use std::str::FromStr; #[aoc_generator(day2)] fn parse(input: &str) -> Vec { @@ -32,7 +33,7 @@ fn parse(input: &str) -> Vec { cubes.trim().split(',').for_each(|cube| { let mut cube = cube.trim().split(' '); let n = cube.next().unwrap().parse().unwrap(); - let color = utils::Color::from(cube.next().unwrap()); + let color = utils::Color::from_str(cube.next().unwrap()).unwrap(); cube_set.set_n_color(color, n); }); cube_set @@ -76,13 +77,14 @@ mod utils { Blue, } - impl From<&str> for Color { - fn from(s: &str) -> Self { + impl std::str::FromStr for Color { + type Err = &'static str; + fn from_str(s: &str) -> Result { match s { - "red" => Self::Red, - "green" => Self::Green, - "blue" => Self::Blue, - _ => panic!("Invalid color: {}", s), + "red" => Ok(Self::Red), + "green" => Ok(Self::Green), + "blue" => Ok(Self::Blue), + _ => Err("Invalid color"), } } } @@ -98,20 +100,11 @@ fn part1(input: &[utils::Game]) -> u32 { .iter() .filter_map(|game| { // Determine if game is valid - let is_game_valid = game - .cube_sets - .iter() - .find_map(|cube_set| { - if cube_set.n_red > MAX_N_RED - || cube_set.n_green > MAX_N_GREEN - || cube_set.n_blue > MAX_N_BLUE - { - Some(false) - } else { - None - } - }) - .unwrap_or(true); + let is_game_valid = game.cube_sets.iter().all(|cube_set| { + cube_set.n_red <= MAX_N_RED + && cube_set.n_green <= MAX_N_GREEN + && cube_set.n_blue <= MAX_N_BLUE + }); // Return the ID of the game if valid to be summed if is_game_valid {