solve year 2023 day 01 and remove Window iterator and cleanup

This commit is contained in:
fuckwit 2023-12-01 07:21:48 +01:00
parent a1a2023b72
commit 7c6209beab
7 changed files with 1074 additions and 34 deletions

1000
input/year2023/day01.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -18,3 +18,7 @@ pub mod year2022 {
pub mod day04; pub mod day04;
pub mod day05; pub mod day05;
} }
pub mod year2023 {
pub mod day01;
}

View File

@ -69,7 +69,10 @@ fn main() {
} }
fn solutions() -> impl Iterator<Item = Solution> { fn solutions() -> impl Iterator<Item = Solution> {
std::iter::empty().chain(year2016()).chain(year2022()) std::iter::empty()
.chain(year2016())
.chain(year2022())
.chain(year2023())
} }
fn year2016() -> Vec<Solution> { fn year2016() -> Vec<Solution> {
@ -89,3 +92,7 @@ fn year2022() -> Vec<Solution> {
solution!(year2022, day05), solution!(year2022, day05),
] ]
} }
fn year2023() -> Vec<Solution> {
vec![solution!(year2023, day01)]
}

View File

@ -72,30 +72,3 @@ pub trait CombinationsIterator<T>: Iterator<Item = T> + Sized {
} }
impl<I, T> CombinationsIterator<T> for I where I: Iterator<Item = T> {} impl<I, T> CombinationsIterator<T> for I where I: Iterator<Item = T> {}
pub struct Window<const SIZE: usize, I, T>
where
I: Iterator<Item = T>,
{
iter: I,
}
impl<const SIZE: usize, I, T> Window<SIZE, I, T>
where
I: Iterator<Item = T>,
{
pub fn new(iter: I) -> Self {
Self { iter }
}
}
impl<const SIZE: usize, I, T> Iterator for Window<SIZE, I, T>
where
I: Iterator<Item = T>,
{
type Item = [I::Item; SIZE];
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}

View File

@ -50,14 +50,11 @@ pub fn part2(input: &str) -> impl std::fmt::Display {
#[inline] #[inline]
#[rustfmt::skip] #[rustfmt::skip]
fn within(coord: &Ix2) -> bool { fn within(coord: &Ix2) -> bool {
match coord.0 { matches!(coord.0, [0, 2] |
[0, 2] |
[1, 1] | [1, 2] | [1, 3] | [1, 1] | [1, 2] | [1, 3] |
[2, 0] | [2, 1] | [2, 2] | [2, 3] | [2, 4] | [2, 0] | [2, 1] | [2, 2] | [2, 3] | [2, 4] |
[3, 1] | [3, 2] | [3, 3] | [3, 1] | [3, 2] | [3, 3] |
[4, 2] => true, [4, 2])
_ => false
}
} }
#[inline] #[inline]

59
src/year2023/day01.rs Normal file
View File

@ -0,0 +1,59 @@
static NUMS: &[&str] = &[
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
pub fn part1(input: &str) -> impl std::fmt::Display {
input
.lines()
.map(|l| {
let nums = l
.chars()
.filter_map(|c| c.is_ascii_digit().then_some(c as u8 - b'0'))
.collect::<Vec<_>>();
let a = nums.first().unwrap();
let b = nums.last().unwrap();
(a * 10 + b) as u32
})
.sum::<u32>()
}
pub fn part2(input: &str) -> impl std::fmt::Display {
input
.lines()
.map(|l| {
let mut nums = vec![];
for i in 0..l.len() {
let first = l[i..i + 1].chars().next().unwrap();
if first.is_ascii_digit() {
nums.push((first as u8 - b'0') as u32);
}
for (j, e) in NUMS.iter().enumerate() {
if l[i..].starts_with(e) {
nums.push(j as u32 + 1);
}
}
}
let a = nums.first().unwrap();
let b = nums.last().unwrap();
a * 10 + b
})
.sum::<u32>()
}
#[test]
fn test_part1() {
assert_eq!(
"142",
part1("1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet").to_string()
)
}
#[test]
fn test_part2() {
assert_eq!(
"45000",
part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
)
}