This commit is contained in:
parent
28a88e1aa7
commit
5e8b974137
@ -32,7 +32,7 @@ enum Quadrant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn nom_i64(input: &str) -> IResult<&str, i64> {
|
fn nom_i64(input: &str) -> IResult<&str, i64> {
|
||||||
let (i, number) = map_res(recognize(preceded(opt(tag("-")), digit1)), |s| i64::from_str(s))(input)?;
|
let (i, number) = map_res(recognize(preceded(opt(tag("-")), digit1)), i64::from_str)(input)?;
|
||||||
Ok((i, number))
|
Ok((i, number))
|
||||||
}
|
}
|
||||||
fn nom_i64_pair(input: &str) -> IResult<&str, (i64, i64)> {
|
fn nom_i64_pair(input: &str) -> IResult<&str, (i64, i64)> {
|
||||||
|
@ -16,7 +16,7 @@ impl Display for Warehouse {
|
|||||||
|
|
||||||
impl Warehouse {
|
impl Warehouse {
|
||||||
fn step_robot(&mut self, dir: Move) {
|
fn step_robot(&mut self, dir: Move) {
|
||||||
let start = self.robot_pos.clone();
|
let start = self.robot_pos;
|
||||||
if self.push(&start, &dir) {
|
if self.push(&start, &dir) {
|
||||||
self.robot_pos = &self.robot_pos + dir.ofs();
|
self.robot_pos = &self.robot_pos + dir.ofs();
|
||||||
}
|
}
|
||||||
@ -40,12 +40,12 @@ impl Warehouse {
|
|||||||
// move both parts
|
// move both parts
|
||||||
self.push(&target, dir);
|
self.push(&target, dir);
|
||||||
self.push(&(&target + (-1, 0)), dir);
|
self.push(&(&target + (-1, 0)), dir);
|
||||||
self.map.swap(&target, pos);
|
self.map.swap(target, pos);
|
||||||
}
|
}
|
||||||
b'[' => {
|
b'[' => {
|
||||||
self.push(&target, dir);
|
self.push(&target, dir);
|
||||||
self.push(&(&target + (1, 0)), dir);
|
self.push(&(&target + (1, 0)), dir);
|
||||||
self.map.swap(&target, pos);
|
self.map.swap(target, pos);
|
||||||
}
|
}
|
||||||
c => panic!("unexpected char {}", c),
|
c => panic!("unexpected char {}", c),
|
||||||
}
|
}
|
||||||
|
14
src/day16.rs
14
src/day16.rs
@ -1,9 +1,9 @@
|
|||||||
use aoc_runner_derive::aoc;
|
use aoc_runner_derive::aoc;
|
||||||
use grid::Grid;
|
use grid::Grid;
|
||||||
use std::{
|
use std::{
|
||||||
|
cmp::Ordering,
|
||||||
collections::{BinaryHeap, HashMap},
|
collections::{BinaryHeap, HashMap},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
usize,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type CoordType = i16;
|
type CoordType = i16;
|
||||||
@ -73,7 +73,7 @@ impl Ord for PathState {
|
|||||||
}
|
}
|
||||||
impl PartialOrd for PathState {
|
impl PartialOrd for PathState {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
self.state.partial_cmp(&other.state)
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,15 +177,19 @@ impl Maze {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if state.position == finish {
|
if state.position == finish {
|
||||||
if state.cost < best_cost {
|
match state.cost.cmp(&best_cost) {
|
||||||
|
Ordering::Less => {
|
||||||
path.push(state.position);
|
path.push(state.position);
|
||||||
best_paths.clear();
|
best_paths.clear();
|
||||||
best_paths.push(path);
|
best_paths.push(path);
|
||||||
best_cost = state.cost
|
best_cost = state.cost
|
||||||
} else if state.cost == best_cost {
|
}
|
||||||
|
Ordering::Equal => {
|
||||||
path.push(state.position);
|
path.push(state.position);
|
||||||
best_paths.push(path);
|
best_paths.push(path);
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +208,7 @@ impl Maze {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (best_cost, best_paths);
|
(best_cost, best_paths)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use num_traits::Signed;
|
use num_traits::Signed;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::ops::{Add, AddAssign};
|
use std::ops::{Add, AddAssign};
|
||||||
use std::fmt::{Debug, Display};
|
|
||||||
|
|
||||||
/// Wrapped signed integer with custom upper bound with wrapping of 0s to the upper bound
|
/// Wrapped signed integer with custom upper bound with wrapping of 0s to the upper bound
|
||||||
#[derive(Eq, Clone, Copy)]
|
#[derive(Eq, Clone, Copy)]
|
||||||
@ -71,7 +71,10 @@ impl<T: Signed + PartialOrd + Copy> PartialOrd<T> for CustomWrapped<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Display + Signed + Copy> Display for CustomWrapped<T> where T: Display {
|
impl<T: Display + Signed + Copy> Display for CustomWrapped<T>
|
||||||
|
where
|
||||||
|
T: Display,
|
||||||
|
{
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.val.fmt(f)
|
self.val.fmt(f)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user