Got slightly confused on part 2... nearly there

This commit is contained in:
Luke Else 2023-12-04 07:40:07 +00:00
parent f3c2f6cfd8
commit 6bd170cbde

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{borrow::BorrowMut, collections::HashMap};
use super::Solution;
@ -16,40 +16,41 @@ impl Solution for Day04 {
.collect();
let mut ans = 0;
for cards in cards {
let mut winning_numbers: HashMap<u32, bool> = HashMap::new();
let mut sep_found = false;
let mut card_nums = vec![];
for num in cards.split_whitespace() {
// Are we after the |
if num == "|" {
sep_found = true;
continue;
}
let num = num.parse::<u32>()?;
if sep_found {
if winning_numbers.contains_key(&num) {
card_nums.push(num);
}
} else {
winning_numbers.insert(num, true);
for card in cards {
let winning_nums = self.get_count_winning_numbers(card)?;
if winning_nums > 0 {
ans += 2u32.pow((winning_nums - 1) as u32);
}
}
if !card_nums.is_empty() {
ans += 2u32.pow((card_nums.len() - 1) as u32);
}
}
Ok(Box::new(ans))
}
fn part2(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
// Remove Card XXX: at start and append card index
let mut cards: Vec<(usize, &str)> = input
.iter()
.map(|c| c.split(":").into_iter().last().unwrap())
.into_iter()
.enumerate()
.collect();
for card in cards {}
// for (i, card) in cards.iter() {
// let winning_nums = self.get_count_winning_numbers(card)?;
// if winning_nums == 0 {
// continue;
// }
// for c in cards.clone()[(i + 1)..=(i + 1 + winning_nums)].iter() {
// cards.push(c.clone());
// }
// }
// for i in cards.clone() {
// println!("{:?}", i);
// }
Ok(Box::new(cards.len()))
}
fn get_day(&self) -> u8 {
@ -58,7 +59,7 @@ impl Solution for Day04 {
}
impl Day04 {
fn get_count_winning_numbers(card: &str) -> Result<usize, Box<dyn std::error::Error>> {
fn get_count_winning_numbers(&self, card: &str) -> Result<usize, Box<dyn std::error::Error>> {
let mut winning_numbers: HashMap<u32, bool> = HashMap::new();
let mut sep_found = false;
let mut card_nums = vec![];
@ -120,6 +121,6 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "30");
}
}