02.rs
1 #![feature(test)] 2 3 type Input = Vec<Game>; 4 type Game = Vec<CubeCounts>; 5 6 #[derive(Debug, Clone, Copy, Default)] 7 struct CubeCounts { 8 red: u32, 9 green: u32, 10 blue: u32, 11 } 12 13 fn setup(input: &str) -> Input { 14 input 15 .lines() 16 .map(|line| { 17 line.split(": ") 18 .nth(1) 19 .unwrap() 20 .split("; ") 21 .map(|round| { 22 round 23 .split(", ") 24 .fold(CubeCounts::default(), |mut acc, round| { 25 let mut round = round.split(' '); 26 let count = round.next().unwrap().parse().unwrap(); 27 let color = round.next().unwrap(); 28 match color { 29 "red" => acc.red = count, 30 "green" => acc.green = count, 31 "blue" => acc.blue = count, 32 _ => panic!(), 33 } 34 acc 35 }) 36 }) 37 .collect() 38 }) 39 .collect() 40 } 41 42 fn min_config(game: &Game) -> CubeCounts { 43 game.iter() 44 .fold(CubeCounts::default(), |acc, &round| CubeCounts { 45 red: acc.red.max(round.red), 46 green: acc.green.max(round.green), 47 blue: acc.blue.max(round.blue), 48 }) 49 } 50 51 fn part1(input: &Input) -> usize { 52 input 53 .iter() 54 .enumerate() 55 .filter(|(_, game)| { 56 let min = min_config(game); 57 min.red <= 12 && min.green <= 13 && min.blue <= 14 58 }) 59 .map(|(i, _)| i + 1) 60 .sum() 61 } 62 63 fn part2(input: &Input) -> u32 { 64 input 65 .iter() 66 .map(|game| { 67 let min = min_config(game); 68 min.red * min.green * min.blue 69 }) 70 .sum() 71 } 72 73 aoc::main!(2023, 2, ex: 1);