From b713c33bb7ae39dbf63d31da1909c90c10196790 Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Mon, 4 Dec 2023 21:42:48 +0000 Subject: [PATCH] day 4: [rust] completed --- day4/Cargo.toml | 9 ++++--- day4/src/card.rs | 10 +------- day4/src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/day4/Cargo.toml b/day4/Cargo.toml index 72192b1..f57f769 100644 --- a/day4/Cargo.toml +++ b/day4/Cargo.toml @@ -1,3 +1,6 @@ -[package] -name = "aoc2023-day4" -version = "1.0.0" +[package] +name = "aoc2023-day4" +version = "1.0.0" + +[dependencies] +clap = { version="4.4.11", features=["derive"] } diff --git a/day4/src/card.rs b/day4/src/card.rs index 9dad87a..6c3c454 100644 --- a/day4/src/card.rs +++ b/day4/src/card.rs @@ -3,7 +3,6 @@ use std::str::FromStr; #[derive(Debug, PartialEq)] pub struct Card { pub id: u32, - pub count: u32, pub winning: Vec, pub numbers: Vec, } @@ -52,7 +51,7 @@ impl FromStr for Card { let (wins, vals) = nums.split_once("|").unwrap(); let winning: Vec = wins.trim().split_whitespace().map(|x| x.parse::().unwrap()).collect(); let values: Vec = vals.trim().split_whitespace().map(|x| x.parse::().unwrap()).collect(); - Ok( Card {id: id, count: 1, winning: winning, numbers: values} ) + Ok( Card {id: id, winning: winning, numbers: values} ) } } @@ -79,11 +78,4 @@ mod test { let card = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53".parse::().unwrap(); assert_eq!(card.matches(), 4); } - - #[test] - fn test_count_change() { - let mut card = Card {id: 1, count: 1, winning: vec![], numbers: vec![] }; - card.count += 1; - assert_eq!(card.count, 2); - } } \ No newline at end of file diff --git a/day4/src/main.rs b/day4/src/main.rs index 20d9bfc..41dcd15 100644 --- a/day4/src/main.rs +++ b/day4/src/main.rs @@ -1,26 +1,64 @@ -pub mod card; +extern crate clap; +pub mod card; use card::Card; use std::error::Error; -use std::env; use std::fs::File; use std::io::{prelude::*, BufReader}; +use clap::{Parser, ArgAction}; + +/// Advent of Code 2023 day 4 +#[derive(Parser, Default, Debug)] +struct Arguments { + /// specify the input data filename + filename: String, + #[arg(short, long, action=ArgAction::SetTrue)] + /// enable additional debug output + debug: Option, +} + +/// Calculate the answer for part 1 of the puzzle. +fn part1(cards: &Vec) -> u32{ + cards.iter().map(|c| c.points()).sum() +} + +/// Calculate the answer for part 2 +fn part2(cards: &Vec) -> u32{ + let mut counts = vec![1; cards.len()]; + for card in cards { + let current_ndx = (card.id - 1) as usize; + let current_count = counts[current_ndx]; + // get the cards we won based on the number of matches in the + // current card. + let num = card.matches(); + let begin = (card.id) as usize; + let end = (card.id + num) as usize; + for ndx in begin..end { + // for each of the won cards, update the count of these + // cards with the number of the current card we have. + counts[ndx] += current_count; + } + } + counts.iter().sum() +} + fn main() -> Result<(), Box> { - let args: Vec = env::args().collect(); - if args.len() > 1 { - let input = File::open(&args[1]).expect("no such file"); - let buffered = BufReader::new(input); - let cards: Vec = buffered.lines() - .map(|x| x.expect("something").parse::().unwrap()) - .collect(); + let args = Arguments::parse(); + + let input = File::open(args.filename).expect("no such file"); + let buffered = BufReader::new(input); + let cards: Vec = buffered.lines() + .map(|x| x.expect("something").parse::().unwrap()) + .collect(); + if args.debug.is_some() { for card in &cards { println!("{:?}", card); } - let points: u32 = cards.iter().map(|c| c.points()).sum(); - println!("part 1: {}", points); - } + println!("part 1: {}", part1(&cards)); + println!("part 2: {}", part2(&cards)); + Ok(()) } -- 2.23.0