day 04 part 2
This commit is contained in:
parent
436f6c56c3
commit
01e069083b
|
@ -10,5 +10,5 @@ fn main() {
|
|||
println!("Result: {}", part1::part1(&structured_input));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result: {}", part2::part2());
|
||||
println!("Result: {}", part2::part2(&structured_input));
|
||||
}
|
||||
|
|
|
@ -7,11 +7,12 @@ use nom::{
|
|||
IResult,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq,)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Card {
|
||||
pub id: usize,
|
||||
pub winning_numbers: BTreeSet<u8>,
|
||||
pub numbers: BTreeSet<u8>,
|
||||
pub multiplier: usize,
|
||||
}
|
||||
|
||||
impl Card {
|
||||
|
@ -36,15 +37,13 @@ impl Card {
|
|||
id: id as usize,
|
||||
winning_numbers,
|
||||
numbers,
|
||||
multiplier: 1,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub fn winning_number_matches(&self) -> BTreeSet<u8> {
|
||||
self.numbers
|
||||
.intersection(&self.winning_numbers)
|
||||
.cloned()
|
||||
.collect()
|
||||
pub fn num_matches(&self) -> usize {
|
||||
self.numbers.intersection(&self.winning_numbers).count()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,32 +72,38 @@ mod tests {
|
|||
Card {
|
||||
id: 1,
|
||||
winning_numbers: collection! {17, 41, 48, 83, 86},
|
||||
numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86}
|
||||
numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 2,
|
||||
winning_numbers: collection! {13, 16, 20, 32, 61},
|
||||
numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82}
|
||||
numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 3,
|
||||
winning_numbers: collection! {1, 21, 44, 53, 59},
|
||||
numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82}
|
||||
numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 4,
|
||||
winning_numbers: collection! {41, 69, 73, 84, 92},
|
||||
numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84}
|
||||
numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 5,
|
||||
winning_numbers: collection! {26, 28, 32, 83, 87},
|
||||
numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93}
|
||||
numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 6,
|
||||
winning_numbers: collection! {13 , 18, 31, 56, 72},
|
||||
numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77}
|
||||
numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77},
|
||||
multiplier: 1,
|
||||
}
|
||||
]
|
||||
);
|
||||
|
@ -115,6 +120,7 @@ mod tests {
|
|||
id: 1,
|
||||
winning_numbers: collection! {41, 48, 83, 86, 17},
|
||||
numbers: collection! {83, 86, 6, 31, 17, 9, 48, 53},
|
||||
multiplier: 1,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
|
|
@ -4,9 +4,9 @@ pub fn part1(input: &[Card]) -> usize {
|
|||
input
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let len = c.winning_number_matches().len();
|
||||
if len >=1{
|
||||
2_usize.pow((len-1).try_into().unwrap())
|
||||
let len = c.num_matches();
|
||||
if len >= 1 {
|
||||
2_usize.pow((len - 1).try_into().unwrap())
|
||||
} else {
|
||||
0
|
||||
}
|
||||
|
@ -27,31 +27,37 @@ mod tests {
|
|||
id: 1,
|
||||
winning_numbers: collection! {17, 41, 48, 83, 86},
|
||||
numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 2,
|
||||
winning_numbers: collection! {13, 16, 20, 32, 61},
|
||||
numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 3,
|
||||
winning_numbers: collection! {1, 21, 44, 53, 59},
|
||||
numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 4,
|
||||
winning_numbers: collection! {41, 69, 73, 84, 92},
|
||||
numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 5,
|
||||
winning_numbers: collection! {26, 28, 32, 83, 87},
|
||||
numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 6,
|
||||
winning_numbers: collection! {13 , 18, 31, 56, 72},
|
||||
numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77},
|
||||
multiplier: 1,
|
||||
},
|
||||
];
|
||||
assert_eq!(part1(&input), 13);
|
||||
|
|
|
@ -1,15 +1,65 @@
|
|||
use crate::parse::*;
|
||||
|
||||
pub fn part2() -> usize {
|
||||
unimplemented!()
|
||||
pub fn part2(input: &[Card]) -> usize {
|
||||
let mut stack: Vec<Card> = input.to_vec();
|
||||
for i in 0..stack.len() {
|
||||
let matches = stack[i].num_matches();
|
||||
if matches >= 1 {
|
||||
//+1 because we want 3 matches to give the num 1,2,3
|
||||
for advance in 1..(matches+1){
|
||||
stack[i+advance].multiplier+=stack[i].multiplier
|
||||
}
|
||||
}
|
||||
}
|
||||
stack.iter().map(|c| c.multiplier).sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use collection_literals::collection;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!(0, 0);
|
||||
let input = vec![
|
||||
Card {
|
||||
id: 1,
|
||||
winning_numbers: collection! {17, 41, 48, 83, 86},
|
||||
numbers: collection! {6, 9, 17, 31, 48, 53, 83, 86},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 2,
|
||||
winning_numbers: collection! {13, 16, 20, 32, 61},
|
||||
numbers: collection! {17, 19, 24, 30, 32, 61, 68, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 3,
|
||||
winning_numbers: collection! {1, 21, 44, 53, 59},
|
||||
numbers: collection! {1, 14, 16, 21, 63, 69, 72, 82},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 4,
|
||||
winning_numbers: collection! {41, 69, 73, 84, 92},
|
||||
numbers: collection! {5, 51, 54, 58, 59, 76, 83, 84},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 5,
|
||||
winning_numbers: collection! {26, 28, 32, 83, 87},
|
||||
numbers: collection! {12, 22, 30, 36, 70, 82, 88, 93},
|
||||
multiplier: 1,
|
||||
},
|
||||
Card {
|
||||
id: 6,
|
||||
winning_numbers: collection! {13 , 18, 31, 56, 72},
|
||||
numbers: collection! {10, 11, 23, 35, 36, 67, 74, 77},
|
||||
multiplier: 1,
|
||||
},
|
||||
];
|
||||
assert_eq!(part2(&input), 30);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue