2023 day2/rust: add solution

This commit is contained in:
Xiretza 2023-12-02 12:55:56 +00:00
parent 02c5511c02
commit e2433598c3
4 changed files with 174 additions and 9 deletions

10
2023/day2/rust/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "rust_2023_02"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../../../common/rust" }
enum-map = "2.7.3"

115
2023/day2/rust/src/main.rs Normal file
View file

@ -0,0 +1,115 @@
#![warn(clippy::pedantic)]
use std::{cmp::max, io::stdin, str::FromStr};
use enum_map::{enum_map, Enum, EnumMap};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)]
enum Colour {
Red,
Green,
Blue,
}
impl FromStr for Colour {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"red" => Ok(Self::Red),
"green" => Ok(Self::Green),
"blue" => Ok(Self::Blue),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Draw {
cubes: EnumMap<Colour, usize>,
}
impl Draw {
pub fn is_valid(&self) -> bool {
let max_values = enum_map! {
Colour::Red => 12,
Colour::Green => 13,
Colour::Blue => 14,
};
self.cubes.iter().all(|(c, n)| *n <= max_values[c])
}
}
impl FromStr for Draw {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let cubes = s
.split(", ")
.map(|c| {
let (n, colour) = c.split_once(' ').unwrap();
let n = n.parse().unwrap();
let colour = colour.parse().unwrap();
(colour, n)
})
.collect();
Ok(Draw { cubes })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Game {
draws: Vec<Draw>,
}
impl Game {
pub fn is_valid(&self) -> bool {
self.draws.iter().all(Draw::is_valid)
}
pub fn minimum_cubes_power(&self) -> usize {
self.draws
.iter()
.map(|draw| draw.cubes)
.fold(EnumMap::default(), |mut acc, cubes| {
for (c, n) in cubes {
acc[c] = max(acc[c], n);
}
acc
})
.values()
.product()
}
}
impl FromStr for Game {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (_, draws) = s.split_once(": ").unwrap();
let draws = draws
.split("; ")
.map(|draw| draw.parse().unwrap())
.collect();
Ok(Game { draws })
}
}
fn main() {
let games: Vec<Game> = stdin()
.lines()
.map(|l| l.unwrap().parse().unwrap())
.collect();
let valid_game_ids = games
.iter()
.enumerate()
.filter_map(|(i, g)| g.is_valid().then_some(i + 1));
println!("{}", valid_game_ids.sum::<usize>());
let powers = games.iter().map(Game::minimum_cubes_power);
println!("{}", powers.sum::<usize>());
}

57
Cargo.lock generated
View file

@ -197,6 +197,26 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "enum-map"
version = "2.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6866f3bfdf8207509a033af1a75a7b08abda06bbaaeae6669323fd5a097df2e9"
dependencies = [
"enum-map-derive",
]
[[package]]
name = "enum-map-derive"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]]
name = "fixedbitset"
version = "0.4.2"
@ -393,9 +413,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.47"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
@ -408,9 +428,9 @@ checksum = "42ecc0faae16de3ca772d9083ce96872ab6758767deb1efb46e954dfc1d98342"
[[package]]
name = "quote"
version = "1.0.21"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
@ -635,6 +655,14 @@ dependencies = [
"aoc",
]
[[package]]
name = "rust_2023_02"
version = "0.1.0"
dependencies = [
"aoc",
"enum-map",
]
[[package]]
name = "rustversion"
version = "1.0.11"
@ -686,7 +714,7 @@ checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.105",
]
[[package]]
@ -719,7 +747,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustversion",
"syn",
"syn 1.0.105",
]
[[package]]
@ -733,6 +761,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -759,7 +798,7 @@ checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.105",
]
[[package]]
@ -825,7 +864,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn",
"syn 1.0.105",
"wasm-bindgen-shared",
]
@ -847,7 +886,7 @@ checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.105",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

View file

@ -28,4 +28,5 @@ members = [
"2022/day15/rust",
"2022/day18/rust",
"2023/day1/rust",
"2023/day2/rust",
]