Day 04 Part 1
This commit is contained in:
parent
9af86b1970
commit
f685f51b09
|
@ -1,8 +1,10 @@
|
||||||
use color_eyre::{
|
use {
|
||||||
eyre::{bail, ensure},
|
color_eyre::{
|
||||||
Result,
|
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)]
|
#[derive(Clone, Copy)]
|
||||||
struct SliceCols<'a, T> {
|
struct SliceCols<'a, T> {
|
||||||
|
|
36
src/bin/day04.rs
Normal file
36
src/bin/day04.rs
Normal 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?))?
|
||||||
|
))
|
||||||
|
}
|
Loading…
Reference in a new issue