Day2 Rust
This commit is contained in:
1
day2/rust/.gitignore
vendored
Normal file
1
day2/rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
7
day2/rust/Cargo.lock
generated
Normal file
7
day2/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
||||||
8
day2/rust/Cargo.toml
Normal file
8
day2/rust/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
135
day2/rust/src/main.rs
Normal file
135
day2/rust/src/main.rs
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
struct Game {
|
||||||
|
id: usize,
|
||||||
|
grabs: Vec<Grab>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Grab {
|
||||||
|
cubes: Vec<Cube>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cube {
|
||||||
|
count: usize,
|
||||||
|
color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Color {
|
||||||
|
RED,
|
||||||
|
GREEN,
|
||||||
|
BLUE,
|
||||||
|
UNKNOWN,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let stdin = std::io::stdin();
|
||||||
|
|
||||||
|
let mut games: Vec<Game> = Vec::new();
|
||||||
|
|
||||||
|
// Read and parse input
|
||||||
|
for line in stdin.lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
|
||||||
|
let (game_id_str, grab_strs) = line.split_once(":").unwrap();
|
||||||
|
|
||||||
|
let game_id = game_id_str.split_once(" ").unwrap().1.parse().unwrap();
|
||||||
|
|
||||||
|
let mut grabs = Vec::new();
|
||||||
|
|
||||||
|
for grab_str in grab_strs.split(";") {
|
||||||
|
let mut cubes = Vec::new();
|
||||||
|
|
||||||
|
for cube in grab_str.split(",").map(|str| str.trim()) {
|
||||||
|
let (count, color) = cube.split_once(' ').unwrap();
|
||||||
|
|
||||||
|
let count = count.parse().unwrap();
|
||||||
|
let color = match color {
|
||||||
|
"red" => Color::RED,
|
||||||
|
"green" => Color::GREEN,
|
||||||
|
"blue" => Color::BLUE,
|
||||||
|
_ => Color::UNKNOWN,
|
||||||
|
};
|
||||||
|
|
||||||
|
cubes.push(Cube { count, color })
|
||||||
|
}
|
||||||
|
|
||||||
|
grabs.push(Grab { cubes });
|
||||||
|
}
|
||||||
|
|
||||||
|
games.push(Game { id: game_id, grabs })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 1
|
||||||
|
let red_cubes = 12;
|
||||||
|
let green_cubes = 13;
|
||||||
|
let blue_cubes = 14;
|
||||||
|
let mut part1 = 0;
|
||||||
|
|
||||||
|
for game in &games {
|
||||||
|
let mut possible = true;
|
||||||
|
for grab in &game.grabs {
|
||||||
|
for cube in &grab.cubes {
|
||||||
|
match cube.color {
|
||||||
|
Color::RED => {
|
||||||
|
if cube.count > red_cubes {
|
||||||
|
possible = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Color::GREEN => {
|
||||||
|
if cube.count > green_cubes {
|
||||||
|
possible = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Color::BLUE => {
|
||||||
|
if cube.count > blue_cubes {
|
||||||
|
possible = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Color::UNKNOWN => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !possible {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if possible {
|
||||||
|
part1 += game.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", part1);
|
||||||
|
|
||||||
|
// Part 2
|
||||||
|
let mut part2 = 0;
|
||||||
|
|
||||||
|
for game in &games {
|
||||||
|
let mut min_reds = 0;
|
||||||
|
let mut min_greens = 0;
|
||||||
|
let mut min_blues = 0;
|
||||||
|
|
||||||
|
for grab in &game.grabs {
|
||||||
|
for cube in &grab.cubes {
|
||||||
|
match cube.color {
|
||||||
|
Color::RED => {
|
||||||
|
min_reds = std::cmp::max(min_reds, cube.count);
|
||||||
|
}
|
||||||
|
Color::GREEN => {
|
||||||
|
min_greens = std::cmp::max(min_greens, cube.count);
|
||||||
|
}
|
||||||
|
Color::BLUE => {
|
||||||
|
min_blues = std::cmp::max(min_blues, cube.count);
|
||||||
|
}
|
||||||
|
Color::UNKNOWN => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
part2 += min_reds * min_greens * min_blues;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", part2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user