mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Move Vec2 to common utilities
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
//! Common helper utilities to all days
|
//! Common helper utilities to all days
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
use std::ops::Add;
|
||||||
|
use std::ops::Index;
|
||||||
|
use std::ops::IndexMut;
|
||||||
|
use std::ops::Sub;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use nom::combinator::map;
|
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<Self> for Vec2 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
Self([self[0] + rhs[0], self[1] + rhs[1]])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub<Self> for Vec2 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
|
Self([self[0] - rhs[0], self[1] - rhs[1]])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Index<usize> for Vec2 {
|
||||||
|
type Output = i32;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn index(&self, index: usize) -> &Self::Output {
|
||||||
|
&self.0[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IndexMut<usize> for Vec2 {
|
||||||
|
#[inline]
|
||||||
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||||
|
&mut self.0[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,3 @@
|
|||||||
use std::ops::Add;
|
|
||||||
use std::ops::Index;
|
|
||||||
use std::ops::IndexMut;
|
|
||||||
use std::ops::Sub;
|
|
||||||
|
|
||||||
use ahash::AHashSet;
|
use ahash::AHashSet;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use nom::bytes::complete::tag;
|
use nom::bytes::complete::tag;
|
||||||
@@ -15,6 +10,7 @@ use nom::sequence::terminated;
|
|||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
|
||||||
use crate::common::parse_input;
|
use crate::common::parse_input;
|
||||||
|
use crate::common::Vec2;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
enum Direction {
|
enum Direction {
|
||||||
@@ -60,41 +56,6 @@ fn parse_moves(input: &[u8]) -> IResult<&[u8], Vec<(Direction, u32)>> {
|
|||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
||||||
struct Vec2(pub [i32; 2]);
|
|
||||||
|
|
||||||
impl Add<Self> for Vec2 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
|
||||||
Self([self[0] + rhs[0], self[1] + rhs[1]])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub<Self> for Vec2 {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
|
||||||
Self([self[0] - rhs[0], self[1] - rhs[1]])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<usize> for Vec2 {
|
|
||||||
type Output = i32;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, index: usize) -> &Self::Output {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IndexMut<usize> for Vec2 {
|
|
||||||
#[inline]
|
|
||||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
|
||||||
&mut self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part_generic<const N: usize>(input: &[u8]) -> Result<String> {
|
fn part_generic<const N: usize>(input: &[u8]) -> Result<String> {
|
||||||
let moves = parse_input(input, parse_moves)?;
|
let moves = parse_input(input, parse_moves)?;
|
||||||
|
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ fn parse_cave(input: &[u8]) -> IResult<&[u8], Cave> {
|
|||||||
|
|
||||||
let mut input = input;
|
let mut input = input;
|
||||||
|
|
||||||
while input != &[][..] {
|
while input != &b""[..] {
|
||||||
let new_input = terminated(
|
let new_input = terminated(
|
||||||
reduce_many1(
|
reduce_many1(
|
||||||
terminated(parse_pair, opt(tag(" -> "))),
|
terminated(parse_pair, opt(tag(" -> "))),
|
||||||
|
|||||||
Reference in New Issue
Block a user