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!("Result: {}", part1::part1(&structured_input));
|
||||||
|
|
||||||
println!("Part Two");
|
println!("Part Two");
|
||||||
println!("Result: {}", part2::part2());
|
println!("Result: {}", part2::part2(&structured_input));
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,12 @@ use nom::{
|
||||||
IResult,
|
IResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq,)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Card {
|
pub struct Card {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
pub winning_numbers: BTreeSet<u8>,
|
pub winning_numbers: BTreeSet<u8>,
|
||||||
pub numbers: BTreeSet<u8>,
|
pub numbers: BTreeSet<u8>,
|
||||||
|
pub multiplier: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Card {
|
impl Card {
|
||||||
|
@ -36,15 +37,13 @@ impl Card {
|
||||||
id: id as usize,
|
id: id as usize,
|
||||||
winning_numbers,
|
winning_numbers,
|
||||||
numbers,
|
numbers,
|
||||||
|
multiplier: 1,
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn winning_number_matches(&self) -> BTreeSet<u8> {
|
pub fn num_matches(&self) -> usize {
|
||||||
self.numbers
|
self.numbers.intersection(&self.winning_numbers).count()
|
||||||
.intersection(&self.winning_numbers)
|
|
||||||
.cloned()
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,32 +72,38 @@ mod tests {
|
||||||
Card {
|
Card {
|
||||||
id: 1,
|
id: 1,
|
||||||
winning_numbers: collection! {17, 41, 48, 83, 86},
|
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 {
|
Card {
|
||||||
id: 2,
|
id: 2,
|
||||||
winning_numbers: collection! {13, 16, 20, 32, 61},
|
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 {
|
Card {
|
||||||
id: 3,
|
id: 3,
|
||||||
winning_numbers: collection! {1, 21, 44, 53, 59},
|
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 {
|
Card {
|
||||||
id: 4,
|
id: 4,
|
||||||
winning_numbers: collection! {41, 69, 73, 84, 92},
|
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 {
|
Card {
|
||||||
id: 5,
|
id: 5,
|
||||||
winning_numbers: collection! {26, 28, 32, 83, 87},
|
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 {
|
Card {
|
||||||
id: 6,
|
id: 6,
|
||||||
winning_numbers: collection! {13 , 18, 31, 56, 72},
|
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,
|
id: 1,
|
||||||
winning_numbers: collection! {41, 48, 83, 86, 17},
|
winning_numbers: collection! {41, 48, 83, 86, 17},
|
||||||
numbers: collection! {83, 86, 6, 31, 17, 9, 48, 53},
|
numbers: collection! {83, 86, 6, 31, 17, 9, 48, 53},
|
||||||
|
multiplier: 1,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,9 +4,9 @@ pub fn part1(input: &[Card]) -> usize {
|
||||||
input
|
input
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
let len = c.winning_number_matches().len();
|
let len = c.num_matches();
|
||||||
if len >=1{
|
if len >= 1 {
|
||||||
2_usize.pow((len-1).try_into().unwrap())
|
2_usize.pow((len - 1).try_into().unwrap())
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -27,31 +27,37 @@ mod tests {
|
||||||
id: 1,
|
id: 1,
|
||||||
winning_numbers: collection! {17, 41, 48, 83, 86},
|
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 {
|
Card {
|
||||||
id: 2,
|
id: 2,
|
||||||
winning_numbers: collection! {13, 16, 20, 32, 61},
|
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 {
|
Card {
|
||||||
id: 3,
|
id: 3,
|
||||||
winning_numbers: collection! {1, 21, 44, 53, 59},
|
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 {
|
Card {
|
||||||
id: 4,
|
id: 4,
|
||||||
winning_numbers: collection! {41, 69, 73, 84, 92},
|
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 {
|
Card {
|
||||||
id: 5,
|
id: 5,
|
||||||
winning_numbers: collection! {26, 28, 32, 83, 87},
|
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 {
|
Card {
|
||||||
id: 6,
|
id: 6,
|
||||||
winning_numbers: collection! {13 , 18, 31, 56, 72},
|
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,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
assert_eq!(part1(&input), 13);
|
assert_eq!(part1(&input), 13);
|
||||||
|
|
|
@ -1,15 +1,65 @@
|
||||||
use crate::parse::*;
|
use crate::parse::*;
|
||||||
|
|
||||||
pub fn part2() -> usize {
|
pub fn part2(input: &[Card]) -> usize {
|
||||||
unimplemented!()
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use collection_literals::collection;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
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