Day 4
This commit is contained in:
parent
4cd6cdf378
commit
650a826e29
77
day4/Cargo.lock
generated
Normal file
77
day4/Cargo.lock
generated
Normal file
@ -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"
|
11
day4/Cargo.toml
Normal file
11
day4/Cargo.toml
Normal file
@ -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"
|
72
day4/src/main.rs
Normal file
72
day4/src/main.rs
Normal file
@ -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<Self> {
|
||||||
|
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::<usize>()?] = 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<Vec<Card>> {
|
||||||
|
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<usize> = 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(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user