Part 1 day 4

This commit is contained in:
pasterp 2023-12-04 17:27:05 +01:00
parent f03c3dc4b4
commit a7e82af3dc
1 changed files with 31 additions and 4 deletions

View File

@ -1,26 +1,53 @@
use log::debug;
use super::utils::Part; use super::utils::Part;
struct Card { struct Card {
nb_winning: u32 nb_winning: u32
} }
pub fn parse(line: &str) -> Card { fn parse(line: &str) -> Card {
debug!("Processing {}", line);
let (_, values) = line.split_once(':').unwrap(); let (_, values) = line.split_once(':').unwrap();
let (winning, numbers) = values.split_once('|').unwrap(); let (winning, numbers) = values.split_once('|').unwrap();
let nb_win : Vec<u32> = winning.trim().split(' ').filter(|v| !v.is_empty()).map(|v| v.parse().unwrap()).collect();
let nb_card : Vec<u32> = numbers.trim().split(' ').filter(|v| !v.is_empty()).map(|v| v.parse().unwrap()).collect();
todo!() debug!("{:?}", nb_win);
debug!("{:?}", nb_card);
let mut value = 0;
for nb in nb_card {
if nb_win.contains(&nb) {
if value == 0 {
value = 1;
}else{
value *= 2;
}
}
}
Card { nb_winning: value }
} }
pub fn solve(lines: Vec<String>, part: Part) -> u32 { pub fn solve(lines: Vec<String>, part: Part) -> u32 {
let mut sum = 0;
for line in lines {
sum += parse(&line).nb_winning;
}
sum
todo!()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::parse;
#[test] #[test]
fn test_line() { fn test_line() {
let line = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53".to_owned(); let line = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53".to_owned();
let card = parse(&line);
assert_eq!(card.nb_winning, 8);
} }
} }