From 5580c538d09db86706e717b9c8e4f74d27449855 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 5 Dec 2023 13:14:40 +0100 Subject: fix day4 --- 2023/day4/src/lib.rs | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/2023/day4/src/lib.rs b/2023/day4/src/lib.rs index cc3742a..ca38b56 100644 --- a/2023/day4/src/lib.rs +++ b/2023/day4/src/lib.rs @@ -19,24 +19,26 @@ pub fn part1(input: &str) -> u32 { let numbers: Vec = card.split('|').map(|x| x.trim().to_string()).collect(); assert_eq!(numbers.len(), 2); - let winnings: Vec<_> = numbers[0] - .replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) - .collect(); - let plays: Vec<_> = numbers[1] - .replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) + let nums: Vec> = numbers + .iter() + .map(|x| { + x.replace(" ", " ") + .split(' ') + .map(|x| x.trim().parse::().unwrap()) + .collect() + }) .collect(); - let mut k = 0; + assert_eq!(nums.len(), 2); + let (winnings, plays) = (&nums[0], &nums[1]); + + let mut k = 0.5; for win in winnings { if plays.contains(&win) { - k = if k == 0 { 1 } else { k * 2 }; + k *= 2.; } } - res += k; + res += k as u32; } res @@ -65,17 +67,19 @@ pub fn part2(input: &str) -> u32 { let numbers: Vec = card.split('|').map(|x| x.trim().to_string()).collect(); assert_eq!(numbers.len(), 2); - let winnings: Vec<_> = numbers[0] - .replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) - .collect(); - let plays: Vec<_> = numbers[1] - .replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) + let nums: Vec> = numbers + .iter() + .map(|x| { + x.replace(" ", " ") + .split(' ') + .map(|x| x.trim().parse::().unwrap()) + .collect() + }) .collect(); + assert_eq!(nums.len(), 2); + let (winnings, plays) = (&nums[0], &nums[1]); + let mut k = 0; for win in winnings { if plays.contains(&win) { @@ -83,7 +87,7 @@ pub fn part2(input: &str) -> u32 { } } - for j in i + 1..i + 1 + k { + for j in (i + 1)..(i + 1 + k) { incr[j] += 1 + incr[i]; } -- cgit v1.2.3-18-g5258