summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-12-05 13:14:40 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-12-05 13:14:40 +0100
commit5580c538d09db86706e717b9c8e4f74d27449855 (patch)
treeb9a9d56ec1d9de8b3ed8735b484c5c5811bf53d1 /2023
parentb0257b30584a6a9da6d1cd1eee4219ccdbfa464a (diff)
fix day4
Diffstat (limited to '2023')
-rw-r--r--2023/day4/src/lib.rs48
1 files 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<String> = 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::<u32>().unwrap())
- .collect();
- let plays: Vec<_> = numbers[1]
- .replace(" ", " ")
- .split(' ')
- .map(|x| x.trim().parse::<u32>().unwrap())
+ let nums: Vec<Vec<_>> = numbers
+ .iter()
+ .map(|x| {
+ x.replace(" ", " ")
+ .split(' ')
+ .map(|x| x.trim().parse::<u32>().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<String> = 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::<u32>().unwrap())
- .collect();
- let plays: Vec<_> = numbers[1]
- .replace(" ", " ")
- .split(' ')
- .map(|x| x.trim().parse::<u32>().unwrap())
+ let nums: Vec<Vec<_>> = numbers
+ .iter()
+ .map(|x| {
+ x.replace(" ", " ")
+ .split(' ')
+ .map(|x| x.trim().parse::<u32>().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];
}