diff --git a/2022/src/common.rs b/2022/src/common.rs index 15b0154..085cf22 100644 --- a/2022/src/common.rs +++ b/2022/src/common.rs @@ -1,6 +1,10 @@ //! Common helper utilities to all days use std::cmp::Ordering; +use std::ops::Add; +use std::ops::Index; +use std::ops::IndexMut; +use std::ops::Sub; use anyhow::Result; use nom::combinator::map; @@ -172,3 +176,38 @@ impl IndexSet { } } } + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub struct Vec2(pub [i32; 2]); + +impl Add for Vec2 { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self([self[0] + rhs[0], self[1] + rhs[1]]) + } +} + +impl Sub for Vec2 { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + Self([self[0] - rhs[0], self[1] - rhs[1]]) + } +} + +impl Index for Vec2 { + type Output = i32; + + #[inline] + fn index(&self, index: usize) -> &Self::Output { + &self.0[index] + } +} + +impl IndexMut for Vec2 { + #[inline] + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.0[index] + } +} diff --git a/2022/src/day09.rs b/2022/src/day09.rs index 51460ac..267752a 100644 --- a/2022/src/day09.rs +++ b/2022/src/day09.rs @@ -1,8 +1,3 @@ -use std::ops::Add; -use std::ops::Index; -use std::ops::IndexMut; -use std::ops::Sub; - use ahash::AHashSet; use anyhow::Result; use nom::bytes::complete::tag; @@ -15,6 +10,7 @@ use nom::sequence::terminated; use nom::IResult; use crate::common::parse_input; +use crate::common::Vec2; #[derive(Copy, Clone)] enum Direction { @@ -60,41 +56,6 @@ fn parse_moves(input: &[u8]) -> IResult<&[u8], Vec<(Direction, u32)>> { ))(input) } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -struct Vec2(pub [i32; 2]); - -impl Add for Vec2 { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self([self[0] + rhs[0], self[1] + rhs[1]]) - } -} - -impl Sub for Vec2 { - type Output = Self; - - fn sub(self, rhs: Self) -> Self::Output { - Self([self[0] - rhs[0], self[1] - rhs[1]]) - } -} - -impl Index for Vec2 { - type Output = i32; - - #[inline] - fn index(&self, index: usize) -> &Self::Output { - &self.0[index] - } -} - -impl IndexMut for Vec2 { - #[inline] - fn index_mut(&mut self, index: usize) -> &mut Self::Output { - &mut self.0[index] - } -} - fn part_generic(input: &[u8]) -> Result { let moves = parse_input(input, parse_moves)?; diff --git a/2022/src/day14.rs b/2022/src/day14.rs index 1278dea..2ae8b3d 100644 --- a/2022/src/day14.rs +++ b/2022/src/day14.rs @@ -91,7 +91,7 @@ fn parse_cave(input: &[u8]) -> IResult<&[u8], Cave> { let mut input = input; - while input != &[][..] { + while input != &b""[..] { let new_input = terminated( reduce_many1( terminated(parse_pair, opt(tag(" -> "))),