mirror of
https://gitlab.com/MisterBiggs/aoc-2023-rust.git
synced 2025-06-15 22:46:52 +00:00
day2
This commit is contained in:
parent
90dfcaebe9
commit
d78aee48a7
54
src/day2.rs
54
src/day2.rs
@ -1,4 +1,5 @@
|
|||||||
// use itertools::Itertools;
|
// use itertools::Itertools;
|
||||||
|
use std::cmp;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
@ -6,9 +7,10 @@ pub fn run() {
|
|||||||
let input = fs::read_to_string("./inputs/day2.txt").expect("Could not read file");
|
let input = fs::read_to_string("./inputs/day2.txt").expect("Could not read file");
|
||||||
|
|
||||||
println!("\tPart 1: {}", part1(&input));
|
println!("\tPart 1: {}", part1(&input));
|
||||||
// println!("\tPart 2: {}", part2(&input));
|
println!("\tPart 2: {}", part2(&input));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct Game {
|
struct Game {
|
||||||
red: usize,
|
red: usize,
|
||||||
green: usize,
|
green: usize,
|
||||||
@ -66,7 +68,35 @@ fn part1(games: &str) -> usize {
|
|||||||
valid_games.into_iter().sum()
|
valid_games.into_iter().sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn part2(calibration_input: &str) -> usize {}
|
fn part2(games: &str) -> usize {
|
||||||
|
let mut game_powers: Vec<usize> = vec![];
|
||||||
|
for game in games.lines() {
|
||||||
|
let (_, rounds) = game.split_once(':').unwrap();
|
||||||
|
|
||||||
|
let mut game_minimums = Game {
|
||||||
|
red: 0,
|
||||||
|
green: 0,
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
|
for round in rounds.split(';') {
|
||||||
|
let dice = round.split(',');
|
||||||
|
for die in dice {
|
||||||
|
let (amount_str, color) = die.trim_start().split_once(' ').unwrap();
|
||||||
|
let amount = amount_str.parse::<usize>().unwrap();
|
||||||
|
|
||||||
|
match color {
|
||||||
|
"red" => game_minimums.red = cmp::max(game_minimums.red, amount),
|
||||||
|
"green" => game_minimums.green = cmp::max(game_minimums.green, amount),
|
||||||
|
"blue" => game_minimums.blue = cmp::max(game_minimums.blue, amount),
|
||||||
|
_ => panic!("No color match found"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
game_powers.push(game_minimums.red * game_minimums.green * game_minimums.blue);
|
||||||
|
}
|
||||||
|
game_powers.into_iter().sum()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -83,16 +113,14 @@ Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
|||||||
assert_eq!(part1(input), 8);
|
assert_eq!(part1(input), 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn test_2() {
|
fn test_2() {
|
||||||
// let input = "two1nine
|
let input = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
// eightwothree
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
// abcone2threexyz
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
// xtwone3four
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
// 4nineeightseven2
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
||||||
// zoneight234
|
|
||||||
// 7pqrstsixteen";
|
|
||||||
|
|
||||||
// assert_eq!(part2(input), 281);
|
assert_eq!(part2(input), 2286);
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user