temp-aoc-2023/two/src/two.rs

76 lines
1.9 KiB
Rust
Raw Normal View History

2023-12-02 05:52:02 +00:00
use std::io::{self, Read};
#[derive(Debug)]
struct Game {
#[allow(unused)]
id: u32,
matches: Vec<Match>,
}
impl Game {
2023-12-02 06:09:04 +00:00
fn min_match(&self) -> Match {
self.matches.iter().fold(Match(0, 0, 0), |acc, m| {
let mut red = acc.0;
let mut green = acc.1;
let mut blue = acc.2;
if m.0 > red {
red = m.0;
}
if m.1 > green {
green = m.1;
}
if m.2 > blue {
blue = m.2;
2023-12-02 05:52:02 +00:00
}
2023-12-02 06:09:04 +00:00
Match(red, green, blue)
2023-12-02 05:52:02 +00:00
})
}
}
#[derive(Debug)]
2023-12-02 06:09:04 +00:00
struct Match(u32, u32, u32);
impl Match {
fn power(&self) -> u32 {
self.0 * self.1 * self.2
}
}
2023-12-02 05:52:02 +00:00
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let lines: Vec<String> = buf.lines().map(ToOwned::to_owned).collect();
let answer: u32 = lines
.iter()
.map(|l| &l[4..])
.map(|l| l.split(':').map(str::trim).collect::<Vec<&str>>())
.map(|l| Game {
id: l[0].parse().unwrap(),
matches: l[1]
.split(';')
.map(str::trim)
.map(|round| {
round
.split(',')
.map(str::trim)
.map(|cubes| cubes.split(' ').collect::<Vec<&str>>())
.fold(Match(0, 0, 0), |acc, cube_count| match cube_count[1] {
"red" => Match(cube_count[0].parse().unwrap(), acc.1, acc.2),
"green" => Match(acc.0, cube_count[0].parse().unwrap(), acc.2),
"blue" => Match(acc.0, acc.1, cube_count[0].parse().unwrap()),
_ => acc,
})
})
.collect(),
})
2023-12-02 06:09:04 +00:00
.map(|g| g.min_match())
.map(|m| m.power())
2023-12-02 05:52:02 +00:00
.sum();
print!("{answer}")
}