Skip to content

Commit 8babbd0

Browse files
committed
update external template and utils
1 parent c523f95 commit 8babbd0

File tree

18 files changed

+388
-40
lines changed

18 files changed

+388
-40
lines changed

.vscode/launch.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
"args": [
1313
"test",
1414
"--no-run",
15-
// replace `01` here with the solution you like to debug.
16-
"--bin=01",
15+
// replace with binary name (e.g. "01") here if you always
16+
// want to debug one file regardless of the active file in
17+
// the editor.
18+
"--bin=${fileBasenameNoExtension}",
1719
"--package=advent_of_code"
1820
],
1921
},
@@ -27,8 +29,10 @@
2729
"cargo": {
2830
"args": [
2931
"build",
30-
// replace `01` here with the solution you like to debug.
31-
"--bin=01",
32+
// replace with binary name (e.g. "01") here if you always
33+
// want to debug one file regardless of the active file in
34+
// the editor
35+
"--bin=${fileBasenameNoExtension}",
3236
"--package=advent_of_code"
3337
],
3438
},

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ pico-args = "0.5.0"
2828
tinyjson = "2.5.1"
2929

3030
# Solution dependencies
31-
fastrand = "2.0.1"
31+
fastrand = "2.2.0"
3232
regex = "1.11.1"

src/maneatingape/LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22

3-
**Copyright © 2023 maneatingape**
3+
**Copyright © 2023-2024 maneatingape**
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/maneatingape/ansi.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/ansi.rs
2+
3+
//! [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
4+
//!
5+
//! These codes allow command line applications to show colored or styled text in most terminals.
6+
//! Advanced commands can move the cursor or clear the screen.
7+
8+
pub const RESET: &str = "\x1b[0m";
9+
pub const BOLD: &str = "\x1b[1m";
10+
pub const RED: &str = "\x1b[31m";
11+
pub const GREEN: &str = "\x1b[32m";
12+
pub const YELLOW: &str = "\x1b[33m";
13+
pub const BLUE: &str = "\x1b[94m";
14+
pub const WHITE: &str = "\x1b[97m";
15+
pub const HOME: &str = "\x1b[H";
16+
pub const CLEAR: &str = "\x1b[J";

src/maneatingape/bitset.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/bitset.rs
2+
3+
//! Add `biterator` method that treats an integer as a set, iterating over each element where
4+
//! the respective bit is set. For example `1101` would return 0, 2 and 3.
5+
use crate::maneatingape::integer::*;
6+
7+
pub trait BitOps<T> {
8+
fn biterator(self) -> Bitset<T>;
9+
}
10+
11+
impl<T> BitOps<T> for T
12+
where
13+
T: Integer<T>,
14+
{
15+
fn biterator(self) -> Bitset<T> {
16+
Bitset { t: self }
17+
}
18+
}
19+
20+
pub struct Bitset<T> {
21+
t: T,
22+
}
23+
24+
impl<T> Iterator for Bitset<T>
25+
where
26+
T: Integer<T>,
27+
T: TryInto<usize>,
28+
{
29+
type Item = usize;
30+
31+
#[inline]
32+
fn next(&mut self) -> Option<Self::Item> {
33+
if self.t == T::ZERO {
34+
return None;
35+
}
36+
37+
let tz = self.t.trailing_zeros();
38+
self.t = self.t ^ (T::ONE << tz);
39+
40+
tz.try_into().ok()
41+
}
42+
}

src/maneatingape/grid.rs

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

33
//! Fast 2 dimensional Grid backed by a single `vec`. This module is designed to work with [`Point`].
44
//!
@@ -52,14 +52,6 @@ impl Grid<u8> {
5252
}
5353

5454
impl<T: Copy + PartialEq> Grid<T> {
55-
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
56-
Grid {
57-
width: self.width,
58-
height: self.height,
59-
bytes: vec![U::default(); (self.width * self.height) as usize],
60-
}
61-
}
62-
6355
pub fn find(&self, needle: T) -> Option<Point> {
6456
let to_point = |index| {
6557
let x = (index as i32) % self.width;
@@ -68,6 +60,26 @@ impl<T: Copy + PartialEq> Grid<T> {
6860
};
6961
self.bytes.iter().position(|&h| h == needle).map(to_point)
7062
}
63+
}
64+
65+
impl<T: Copy> Grid<T> {
66+
pub fn new(width: i32, height: i32, value: T) -> Grid<T> {
67+
Grid {
68+
width,
69+
height,
70+
bytes: vec![value; (width * height) as usize],
71+
}
72+
}
73+
}
74+
75+
impl<T> Grid<T> {
76+
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
77+
Grid {
78+
width: self.width,
79+
height: self.height,
80+
bytes: vec![U::default(); (self.width * self.height) as usize],
81+
}
82+
}
7183

7284
#[inline]
7385
pub fn contains(&self, point: Point) -> bool {
@@ -79,14 +91,14 @@ impl<T> Index<Point> for Grid<T> {
7991
type Output = T;
8092

8193
#[inline]
82-
fn index(&self, point: Point) -> &Self::Output {
83-
&self.bytes[(self.width * point.y + point.x) as usize]
94+
fn index(&self, index: Point) -> &Self::Output {
95+
&self.bytes[(self.width * index.y + index.x) as usize]
8496
}
8597
}
8698

8799
impl<T> IndexMut<Point> for Grid<T> {
88100
#[inline]
89-
fn index_mut(&mut self, point: Point) -> &mut Self::Output {
90-
&mut self.bytes[(self.width * point.y + point.x) as usize]
101+
fn index_mut(&mut self, index: Point) -> &mut Self::Output {
102+
&mut self.bytes[(self.width * index.y + index.x) as usize]
91103
}
92104
}

src/maneatingape/hash.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/8d753cc778bdb74c18ecd48f6ab116d86a2ed70a/src/util/hash.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/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

src/maneatingape/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// source: // source: https://github.com/maneatingape/advent-of-code-rust/blob/2f5d0220ea44f33545f741e307d4481b50d632c7/src/util/heap.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/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

src/maneatingape/integer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// source: https://github.com/maneatingape/advent-of-code-rust/blob/ff407999fc459d7bbf1d053b61c9ad957a8cdf2f/src/util/integer.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/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.
5-
use std::cmp::{PartialEq, PartialOrd};
6-
use std::ops::{Add, BitAnd, BitOr, Div, Mul, Neg, Rem, Shl, Shr, Sub};
5+
use std::ops::*;
76

87
pub trait Integer<T>:
98
Copy
@@ -13,6 +12,7 @@ pub trait Integer<T>:
1312
+ Add<Output = T>
1413
+ BitAnd<Output = T>
1514
+ BitOr<Output = T>
15+
+ BitXor<Output = T>
1616
+ Div<Output = T>
1717
+ Mul<Output = T>
1818
+ Rem<Output = T>
@@ -25,6 +25,7 @@ pub trait Integer<T>:
2525
const TEN: T;
2626

2727
fn ilog2(self) -> T;
28+
fn trailing_zeros(self) -> T;
2829
}
2930

3031
pub trait Unsigned<T>: Integer<T> {}
@@ -41,7 +42,13 @@ macro_rules! integer {
4142
#[inline]
4243
#[allow(trivial_numeric_casts)]
4344
fn ilog2(self) -> $t {
44-
self.ilog2() as $t
45+
<$t>::ilog2(self) as $t
46+
}
47+
48+
#[inline]
49+
#[allow(trivial_numeric_casts)]
50+
fn trailing_zeros(self) -> $t {
51+
<$t>::trailing_zeros(self) as $t
4552
}
4653
}
4754
)*)

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/589ff1a347c70570c2a1ef5a51dfed105d6756d4/src/util/iter.rs
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb/src/util/iter.rs
22

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

0 commit comments

Comments
 (0)