From 650a826e29bbe0ef64e63aa96d4dccc108b0624d Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Thu, 7 Dec 2023 11:57:25 +0100 Subject: [PATCH] Day 4 --- day4/Cargo.lock | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ day4/Cargo.toml | 11 +++++++ day4/src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 day4/Cargo.lock create mode 100644 day4/Cargo.toml create mode 100644 day4/src/main.rs diff --git a/day4/Cargo.lock b/day4/Cargo.lock new file mode 100644 index 0000000..08feed9 --- /dev/null +++ b/day4/Cargo.lock @@ -0,0 +1,77 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "day4" +version = "0.1.0" +dependencies = [ + "anyhow", + "itertools", + "regex", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" diff --git a/day4/Cargo.toml b/day4/Cargo.toml new file mode 100644 index 0000000..1ef16dd --- /dev/null +++ b/day4/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day4" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.75" +itertools = "0.12.0" +regex = "1.10.2" diff --git a/day4/src/main.rs b/day4/src/main.rs new file mode 100644 index 0000000..fb1c089 --- /dev/null +++ b/day4/src/main.rs @@ -0,0 +1,72 @@ +use std::{io::stdin, str::FromStr}; +use anyhow::{anyhow, Result, Error}; +use itertools::Itertools; + +struct Card { + id: u8, + drawn: [bool; 100], + chosen:[bool; 100], + wins: u8, +} + +impl Card { + fn value(&self) -> usize { + self.wins + .checked_sub(1) + .map(|e| 2_usize.pow(e as u32)) + .unwrap_or(0) + } +} + +impl FromStr for Card { + type Err = Error; + fn from_str(s: &str) -> Result { + let (id_s, drawn_s, chosen_s) = s.split(&[':','|']) + .collect_tuple() + .ok_or_else(||anyhow!("Bad line format {s}"))?; + let id = id_s.strip_prefix("Card ") + .ok_or_else(|| anyhow!("Bad header {id_s}"))? + .trim() + .parse()?; + + let mut drawn = [false; 100]; + let mut chosen = [false; 100]; + let mut wins = 0; + + for d in drawn_s.trim().split_whitespace() { + drawn[d.parse::()?] = true; + } + for c in chosen_s.trim().split_whitespace() { + let c: usize = c.parse()?; + chosen[c] = true; + if drawn[c] { wins += 1 } + } + + Ok(Card { id, drawn, chosen, wins }) + } +} + +fn read() -> Result> { + stdin() + .lines() + .map(|s| s?.parse()) + .collect() +} + +fn main() -> Result<()> { + let cards = read()?; + let score1: usize = cards.iter().map(Card::value).sum(); + println!("score is {score1}"); + + let mut counts: Vec = cards.iter().map(|_| 1).collect(); + for i in 0..counts.len() { + let amount = counts[i]; + let wins = cards[i].wins as usize; + for other in &mut counts[i+1..][..wins] { + *other += amount; + } + } + let total: usize = counts.iter().sum(); + println!("Total cards {total}"); + Ok(()) +}