Missed several updates. Working on 8 pt 2

This commit is contained in:
Connor Johnstone
2023-12-07 23:51:02 -07:00
parent 1793b37364
commit 58e0f7fcea
22 changed files with 4230 additions and 12 deletions

View File

@@ -10,40 +10,40 @@ use nom::{
#[derive(Debug)]
pub struct Card {
winning_set: Vec<u32>,
player_set: Vec<u32>,
winning_set: Vec<u64>,
player_set: Vec<u64>,
}
impl Card {
fn score(&self) -> u32 {
fn score(&self) -> u64 {
let count = self.win_count();
if count == 0 {
0
} else {
2_u32.pow(count as u32 - 1)
2_u64.pow(count as u64 - 1)
}
}
fn win_count(&self) -> u32 {
self.player_set.iter().filter(|num| {self.winning_set.contains(num)}).count() as u32
fn win_count(&self) -> u64 {
self.player_set.iter().filter(|num| {self.winning_set.contains(num)}).count() as u64
}
}
// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
fn parse_line(input: &str) -> IResult<&str, Card> {
let (input, _) = preceded(tag("Card "), preceded(space0, complete::u32))(input)?;
let (input, winning_set) = preceded(tag(": "), preceded(space0, separated_list1(space1, complete::u32)))(input)?;
let (input, player_set) = preceded(tag(" | "), preceded(space0, separated_list1(space1, complete::u32)))(input)?;
let (input, _) = preceded(tag("Card "), preceded(space0, complete::u64))(input)?;
let (input, winning_set) = preceded(tag(": "), preceded(space0, separated_list1(space1, complete::u64)))(input)?;
let (input, player_set) = preceded(tag(" | "), preceded(space0, separated_list1(space1, complete::u64)))(input)?;
Ok((input, Card{winning_set, player_set}))
}
pub fn part1(input: &str) -> String {
input.lines().map(|line| { parse_line(line).unwrap().1.score() }).sum::<u32>().to_string()
input.lines().map(|line| { parse_line(line).unwrap().1.score() }).sum::<u64>().to_string()
}
pub fn part2(input: &str) -> String {
let num_games = input.lines().count();
let mut current_cards = (0..num_games).map(|_| {1}).collect::<Vec<u32>>();
let mut current_cards = (0..num_games).map(|_| {1}).collect::<Vec<u64>>();
current_cards.push(0);
let mut current_index = 0;
let mut cards = input.lines().map(|line| {
@@ -58,7 +58,7 @@ pub fn part2(input: &str) -> String {
current_cards[i] += number_of_current_card;
}
}
current_cards.into_iter().sum::<u32>().to_string()
current_cards.into_iter().sum::<u64>().to_string()
}
pub mod prelude {