Skip to content

Commit 632f10a

Browse files
committed
update maneatingape code
1 parent 4b83b94 commit 632f10a

File tree

13 files changed

+46
-23
lines changed

13 files changed

+46
-23
lines changed

src/maneatingape/ansi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/ansi.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/ansi.rs
22

33
//! [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
44
//!

src/maneatingape/bitset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/bitset.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/bitset.rs
22

33
//! Add `biterator` method that treats an integer as a set, iterating over each element where
44
//! the respective bit is set. For example `1101` would return 0, 2 and 3.

src/maneatingape/grid.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/grid.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/grid.rs
22

33
//! Fast 2 dimensional Grid backed by a single `vec`. This module is designed to work with [`Point`].
44
//!
@@ -19,13 +19,13 @@
1919
//! ```
2020
//!
2121
//! A convenience [`parse`] method creates a `Grid` directly from a 2 dimenionsal set of
22-
//! ASCII characters, a common occurence in Advent of Code inputs. The [`default_copy`] function
22+
//! ASCII characters, a common occurence in Advent of Code inputs. The [`same_size_with`] function
2323
//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
2424
//! location or for tracking cost in Djikstra.
2525
//!
26-
//! [`Point`]: crate::util::point
26+
//! [`Point`]: crate::maneatingape::point
2727
//! [`parse`]: Grid::parse
28-
//! [`default_copy`]: Grid::default_copy
28+
//! [`same_size_with`]: Grid::same_size_with
2929
use crate::maneatingape::point::*;
3030
use std::ops::{Index, IndexMut};
3131

@@ -37,6 +37,7 @@ pub struct Grid<T> {
3737
}
3838

3939
impl Grid<u8> {
40+
#[inline]
4041
pub fn parse(input: &str) -> Self {
4142
let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
4243
let width = raw[0].len() as i32;
@@ -49,9 +50,21 @@ impl Grid<u8> {
4950
bytes,
5051
}
5152
}
53+
54+
pub fn print(&self) {
55+
for y in 0..self.height {
56+
for x in 0..self.width {
57+
let point = Point::new(x, y);
58+
print!("{}", self[point] as char);
59+
}
60+
println!();
61+
}
62+
println!();
63+
}
5264
}
5365

5466
impl<T: Copy + PartialEq> Grid<T> {
67+
#[inline]
5568
pub fn find(&self, needle: T) -> Option<Point> {
5669
let to_point = |index| {
5770
let x = (index as i32) % self.width;
@@ -73,11 +86,12 @@ impl<T: Copy> Grid<T> {
7386
}
7487

7588
impl<T> Grid<T> {
76-
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
89+
#[inline]
90+
pub fn same_size_with<U: Copy>(&self, value: U) -> Grid<U> {
7791
Grid {
7892
width: self.width,
7993
height: self.height,
80-
bytes: vec![U::default(); (self.width * self.height) as usize],
94+
bytes: vec![value; (self.width * self.height) as usize],
8195
}
8296
}
8397

src/maneatingape/hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/hash.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/hash.rs
22

33
//! Provides fast [`HashSet`] and [`HashMap`] implementations based on a simplified implementation of
44
//! the fast [Rust C hash algorithm](https://github.com/rust-lang/rustc-hash) also used by
@@ -8,7 +8,7 @@
88
//! resistant but slower hashing algorithm. [`FxHasher`] is much faster (between 2x to 5x from my testing).
99
use std::collections::{HashMap, HashSet};
1010
use std::hash::{BuildHasher, Hash, Hasher};
11-
use std::ops::BitXor;
11+
use std::ops::BitXor as _;
1212

1313
/// Type alias for [`HashSet`] using [`FxHasher`].
1414
pub type FastSet<T> = HashSet<T, BuildFxHasher>;

src/maneatingape/heap.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/heap.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/heap.rs
22

33
//! [Min heap] more suitable for algorithms such as [Dijkstra] and [A*] than Rust's default
44
//! max heap. Splits the sorting key and value, so that you can order items without having
@@ -65,4 +65,9 @@ impl<K: Ord, V> MinHeap<K, V> {
6565
pub fn pop(&mut self) -> Option<(K, V)> {
6666
self.heap.pop().map(|w| (w.key, w.value))
6767
}
68+
69+
#[inline]
70+
pub fn peek(&self) -> Option<(&K, &V)> {
71+
self.heap.peek().map(|w| (&w.key, &w.value))
72+
}
6873
}

src/maneatingape/integer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/integer.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/integer.rs
22

33
//! Combines common [operators](https://doc.rust-lang.org/book/appendix-02-operators.html)
44
//! and constants `0`, `1` and `10` to enable generic methods on integer types.

src/maneatingape/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/iter.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/iter.rs
22

33
//! Add a `chunk` method to [`Iterator`] that duplicates the functionality of the unstable
44
//! [`array_chunks`] method.

src/maneatingape/math.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/math.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/math.rs
22

33
//! Extended mathematical operations.
44
//!
@@ -29,7 +29,7 @@ pub trait UnsignedMathOps<T: Unsigned<T>> {
2929
}
3030

3131
pub trait SignedMathOps<T: Signed<T>> {
32-
fn mod_inv(self, m: T) -> T;
32+
fn mod_inv(self, m: T) -> Option<T>;
3333
}
3434

3535
impl<T: Integer<T>> IntegerMathOps<T> for T {
@@ -86,7 +86,7 @@ impl<T: Unsigned<T>> UnsignedMathOps<T> for T {
8686

8787
impl<T: Signed<T>> SignedMathOps<T> for T {
8888
// Modular multiplicative inverse
89-
fn mod_inv(self, m: T) -> T {
89+
fn mod_inv(self, m: T) -> Option<T> {
9090
let mut t = T::ZERO;
9191
let mut newt = T::ONE;
9292
let mut r = m;
@@ -98,9 +98,13 @@ impl<T: Signed<T>> SignedMathOps<T> for T {
9898
(r, newr) = (newr, r - quotient * newr);
9999
}
100100

101+
if r > T::ONE {
102+
return None;
103+
}
101104
if t < T::ZERO {
102105
t = t + m;
103106
}
104-
t
107+
108+
Some(t)
105109
}
106110
}

src/maneatingape/md5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/md5.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/md5.rs
22

33
//! MD5 hash algorithm
44
//!

src/maneatingape/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/parse.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/parse.rs
22

33
//! Extracts and parses signed and unsigned integers from surrounding text and whitespace.
44
//!

src/maneatingape/point.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/point.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/point.rs
22

33
//! Comprehensive 2 dimensional point implementation. This class is designed to work together
44
//! with the [`Grid`] class.
@@ -26,7 +26,7 @@
2626
//! [`clockwise`]: Point::clockwise
2727
//! [`counter_clockwise`]: Point::counter_clockwise
2828
//! [`manhattan`]: Point::manhattan
29-
//! [`Grid`]: crate::util::grid
29+
//! [`Grid`]: crate::maneatingape::grid
3030
use std::hash::{Hash, Hasher};
3131
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
3232

src/maneatingape/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/slice.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/slice.rs
22

33
//! Extension methods for slices.
44
//!

src/maneatingape/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/thread.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/177fc32fbfc3ce814b26b10263b2cc081e121b50/src/util/thread.rs
22

33
//! Utility methods to spawn a number of
44
//! [scoped](https://doc.rust-lang.org/stable/std/thread/fn.scope.html)

0 commit comments

Comments
 (0)