solve year 2023 day 01 and remove Window iterator and cleanup
This commit is contained in:
parent
a1a2023b72
commit
7c6209beab
1000
input/year2023/day01.txt
Normal file
1000
input/year2023/day01.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -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;
|
||||||
|
}
|
||||||
|
@ -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)]
|
||||||
|
}
|
||||||
|
@ -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!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -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
59
src/year2023/day01.rs
Normal 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()
|
||||||
|
)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user