Skip to content

Commit

Permalink
aoc2023/day2: Update solution
Browse files Browse the repository at this point in the history
Signed-off-by: Andrej Orsula <orsula.andrej@gmail.com>
  • Loading branch information
AndrejOrsula committed Dec 2, 2023
1 parent 1659db5 commit fd60cfa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<table>
<tr><th>Solution</th><th>Part 1 Performance</th><th>Part 2 Performance</th></tr>
<tr><th>Days</th><th>Part 1 Performance</th><th>Part 2 Performance</th></tr>
<tr><td>

| 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) |

</td><td>

| 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 |

</td><td>

| 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 |

</td></tr>
</table>
Expand Down
35 changes: 14 additions & 21 deletions aoc2023/src/day2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::str::FromStr;

#[aoc_generator(day2)]
fn parse(input: &str) -> Vec<utils::Game> {
Expand Down Expand Up @@ -32,7 +33,7 @@ fn parse(input: &str) -> Vec<utils::Game> {
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
Expand Down Expand Up @@ -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<Self, Self::Err> {
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"),
}
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit fd60cfa

Please sign in to comment.