generated from janezicmatej/aoc-template
solution: day 2
This commit is contained in:
parent
00000050ae
commit
00000060b5
|
@ -0,0 +1,6 @@
|
|||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use aoc::parsers::to_vec;
|
||||
|
||||
struct Game {
|
||||
id: u32,
|
||||
balls: Vec<(u32, String)>,
|
||||
}
|
||||
|
||||
impl FromStr for Game {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let rest = s.strip_prefix("Game ").unwrap();
|
||||
let (id, rest) = rest.split_once(':').unwrap();
|
||||
let id = id.parse().unwrap();
|
||||
|
||||
let balls = rest
|
||||
.split([',', ';'])
|
||||
.map(|x| x.strip_prefix(' ').unwrap().split_once(' ').unwrap())
|
||||
.map(|(n, c)| (n.parse::<u32>().unwrap(), c.to_string()))
|
||||
.collect();
|
||||
|
||||
Ok(Game { id, balls })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
let games: Vec<Game> = to_vec(input, '\n');
|
||||
|
||||
let mut id_sum = 0;
|
||||
|
||||
'games: for g in games {
|
||||
for (n, c) in g.balls {
|
||||
match c.as_str() {
|
||||
"blue" => {
|
||||
if n > 14 {
|
||||
continue 'games;
|
||||
}
|
||||
}
|
||||
"red" => {
|
||||
if n > 12 {
|
||||
continue 'games;
|
||||
}
|
||||
}
|
||||
"green" => {
|
||||
if n > 13 {
|
||||
continue 'games;
|
||||
}
|
||||
}
|
||||
_ => continue 'games,
|
||||
}
|
||||
}
|
||||
|
||||
id_sum += g.id;
|
||||
}
|
||||
|
||||
Some(id_sum)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
let games: Vec<Game> = to_vec(input, '\n');
|
||||
|
||||
let mut total_power = 0;
|
||||
|
||||
for g in games {
|
||||
let power: u32 = ["blue", "red", "green"]
|
||||
.iter()
|
||||
.map(|&c| {
|
||||
g.balls
|
||||
.iter()
|
||||
.filter(|(_, cd)| c == cd.as_str())
|
||||
.map(|(n, _)| n)
|
||||
.max()
|
||||
.unwrap()
|
||||
})
|
||||
.product();
|
||||
|
||||
total_power += power;
|
||||
}
|
||||
|
||||
Some(total_power)
|
||||
}
|
||||
|
||||
aoc::solution!(2);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(part_one(&aoc::template::read_file("examples", 2)), Some(8));
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 2)),
|
||||
Some(2286)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue