Day 04 Part 1

trunk
Erin 2023-12-04 23:03:07 +01:00
parent 9af86b1970
commit f685f51b09
2 changed files with 42 additions and 4 deletions

View File

@ -1,8 +1,10 @@
use color_eyre::{
eyre::{bail, ensure},
Result,
use {
color_eyre::{
eyre::{bail, ensure},
Result,
},
std::{cell::Cell, collections::HashMap, io::stdin, rc::Rc},
};
use std::{cell::Cell, collections::HashMap, io::stdin, rc::Rc};
#[derive(Clone, Copy)]
struct SliceCols<'a, T> {

36
src/bin/day04.rs Normal file
View File

@ -0,0 +1,36 @@
use {color_eyre::eyre::ContextCompat, color_eyre::Result, std::io::stdin};
fn main() -> Result<()> {
Ok(println!(
"{}",
stdin()
.lines()
.map(|ln| -> Result<_> {
let ln = ln?;
let (winning, nums) = ln
.split_once(':')
.context("Invalid record: missing ':' separator")?
.1
.split_once('|')
.context("Invalid record: missing '|' separator")?;
let winning = winning
.split_whitespace()
.map(str::parse)
.collect::<Result<Box<[u64]>, _>>()?;
let n = nums
.split_whitespace()
.map(str::parse)
.map(|item| item.map(|item| winning.contains(&item)))
.try_fold::<_, _, Result<_>>(0, |acc, x| Ok(acc + x? as u64))?;
if n != 0 {
Ok(2_u64.pow(u32::try_from(n)?.saturating_sub(1)))
} else {
Ok(0)
}
})
.try_fold::<_, _, Result<_>>(0, |acc, x| Ok(acc + x?))?
))
}