Day 04 Part 1

This commit is contained in:
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::{ use {
color_eyre::{
eyre::{bail, ensure}, eyre::{bail, ensure},
Result, Result,
},
std::{cell::Cell, collections::HashMap, io::stdin, rc::Rc},
}; };
use std::{cell::Cell, collections::HashMap, io::stdin, rc::Rc};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct SliceCols<'a, T> { 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?))?
))
}