Skip to content

Commit 3702221

Browse files
committed
Comprehensive code linting
1 parent a1df0d8 commit 3702221

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+296
-429
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
9090
| 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Source](src/year2024/day13.rs) | 14 |
9191
| 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [Source](src/year2024/day14.rs) | 74 |
9292
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 303 |
93-
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 390 |
93+
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 350 |
9494
| 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [Source](src/year2024/day17.rs) | 2 |
9595
| 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [Source](src/year2024/day18.rs) | 42 |
9696
| 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [Source](src/year2024/day19.rs) | 110 |
@@ -124,7 +124,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
124124
| 15 | [Lens Library](https://adventofcode.com/2023/day/15) | [Source](src/year2023/day15.rs) | 84 |
125125
| 16 | [The Floor Will Be Lava](https://adventofcode.com/2023/day/16) | [Source](src/year2023/day16.rs) | 160 |
126126
| 17 | [Clumsy Crucible](https://adventofcode.com/2023/day/17) | [Source](src/year2023/day17.rs) | 2379 |
127-
| 18 | [Lavaduct Lagoon](https://adventofcode.com/2023/day/18) | [Source](src/year2023/day18.rs) | 17 |
127+
| 18 | [Lavaduct Lagoon](https://adventofcode.com/2023/day/18) | [Source](src/year2023/day18.rs) | 11 |
128128
| 19 | [Aplenty](https://adventofcode.com/2023/day/19) | [Source](src/year2023/day19.rs) | 100 |
129129
| 20 | [Pulse Propagation](https://adventofcode.com/2023/day/20) | [Source](src/year2023/day20.rs) | 6 |
130130
| 21 | [Step Counter](https://adventofcode.com/2023/day/21) | [Source](src/year2023/day21.rs) | 182 |
@@ -145,7 +145,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
145145
| 4 | [Camp Cleanup](https://adventofcode.com/2022/day/4) | [Source](src/year2022/day04.rs) | 8 |
146146
| 5 | [Supply Stacks](https://adventofcode.com/2022/day/5) | [Source](src/year2022/day05.rs) | 13 |
147147
| 6 | [Tuning Trouble](https://adventofcode.com/2022/day/6) | [Source](src/year2022/day06.rs) | 3 |
148-
| 7 | [No Space Left On Device](https://adventofcode.com/2022/day/7) | [Source](src/year2022/day07.rs) | 14 |
148+
| 7 | [No Space Left On Device](https://adventofcode.com/2022/day/7) | [Source](src/year2022/day07.rs) | 11 |
149149
| 8 | [Treetop Tree House](https://adventofcode.com/2022/day/8) | [Source](src/year2022/day08.rs) | 51 |
150150
| 9 | [Rope Bridge](https://adventofcode.com/2022/day/9) | [Source](src/year2022/day09.rs) | 107 |
151151
| 10 | [Cathode-Ray Tube](https://adventofcode.com/2022/day/10) | [Source](src/year2022/day10.rs) | 2 |
@@ -209,7 +209,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
209209
| 4 | [Passport Processing](https://adventofcode.com/2020/day/4) | [Source](src/year2020/day04.rs) | 38 |
210210
| 5 | [Binary Boarding](https://adventofcode.com/2020/day/5) | [Source](src/year2020/day05.rs) | 11 |
211211
| 6 | [Custom Customs](https://adventofcode.com/2020/day/6) | [Source](src/year2020/day06.rs) | 35 |
212-
| 7 | [Handy Haversacks](https://adventofcode.com/2020/day/7) | [Source](src/year2020/day07.rs) | 69 |
212+
| 7 | [Handy Haversacks](https://adventofcode.com/2020/day/7) | [Source](src/year2020/day07.rs) | 58 |
213213
| 8 | [Handheld Halting](https://adventofcode.com/2020/day/8) | [Source](src/year2020/day08.rs) | 8 |
214214
| 9 | [Encoding Error](https://adventofcode.com/2020/day/9) | [Source](src/year2020/day09.rs) | 9 |
215215
| 10 | [Adapter Array](https://adventofcode.com/2020/day/10) | [Source](src/year2020/day10.rs) | 1 |
@@ -286,7 +286,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
286286
| 17 | [Reservoir Research ](https://adventofcode.com/2018/day/17) | [Source](src/year2018/day17.rs) | 151 |
287287
| 18 | [Settlers of The North Pole](https://adventofcode.com/2018/day/18) | [Source](src/year2018/day18.rs) | 384 |
288288
| 19 | [Go With The Flow](https://adventofcode.com/2018/day/19) | [Source](src/year2018/day19.rs) | 1 |
289-
| 20 | [A Regular Map](https://adventofcode.com/2018/day/20) | [Source](src/year2018/day20.rs) | 36 |
289+
| 20 | [A Regular Map](https://adventofcode.com/2018/day/20) | [Source](src/year2018/day20.rs) | 24 |
290290
| 21 | [Chronal Conversion](https://adventofcode.com/2018/day/21) | [Source](src/year2018/day21.rs) | 66 |
291291
| 22 | [Mode Maze](https://adventofcode.com/2018/day/22) | [Source](src/year2018/day22.rs) | 3197 |
292292
| 23 | [Experimental Emergency Teleportation](https://adventofcode.com/2018/day/23) | [Source](src/year2018/day23.rs) | 506 |

benches/benchmark.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ macro_rules! benchmark {
77
mod $year {$(
88
mod $day {
99
use aoc::$year::$day::*;
10+
use aoc::util::ansi::*;
1011
use std::fs::read_to_string;
1112
use std::sync::LazyLock;
1213
use test::Bencher;
@@ -15,7 +16,10 @@ macro_rules! benchmark {
1516
let year = stringify!($year);
1617
let day = stringify!($day);
1718
let path = format!("input/{year}/{day}.txt");
18-
read_to_string(&path).expect(&path)
19+
20+
read_to_string(&path).unwrap_or_else(|_| {
21+
panic!("Missing input file {BOLD}{WHITE}{path}{RESET}");
22+
})
1923
});
2024

2125
#[bench]

src/main.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ struct Solution {
6666
macro_rules! run {
6767
($year:tt $($day:tt),*) => {
6868
fn $year() -> Vec<Solution> {
69-
vec![$({
70-
let year = stringify!($year).unsigned();
71-
let day = stringify!($day).unsigned();
72-
let wrapper = |data: &str| {
73-
use aoc::$year::$day::*;
74-
75-
let input = parse(&data);
76-
let part1 = part1(&input);
77-
let part2 = part2(&input);
78-
79-
(part1.to_string(), part2.to_string())
80-
};
81-
82-
Solution { year, day, wrapper }
83-
},)*]
69+
vec![$(
70+
Solution {
71+
year: stringify!($year).unsigned(),
72+
day: stringify!($day).unsigned(),
73+
wrapper: |data: &str| {
74+
use aoc::$year::$day::*;
75+
76+
let input = parse(&data);
77+
let part1 = part1(&input).to_string();
78+
let part2 = part2(&input).to_string();
79+
80+
(part1, part2)
81+
}
82+
}
83+
,)*]
8484
}
8585
}
8686
}

src/util/bitset.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ pub trait BitOps<T> {
66
fn biterator(self) -> Bitset<T>;
77
}
88

9-
impl<T> BitOps<T> for T
10-
where
11-
T: Integer<T>,
12-
{
9+
impl<T: Integer<T>> BitOps<T> for T {
1310
fn biterator(self) -> Bitset<T> {
1411
Bitset { t: self }
1512
}
@@ -19,20 +16,17 @@ pub struct Bitset<T> {
1916
t: T,
2017
}
2118

22-
impl<T> Iterator for Bitset<T>
23-
where
24-
T: Integer<T>,
25-
{
19+
impl<T: Integer<T>> Iterator for Bitset<T> {
2620
type Item = usize;
2721

2822
#[inline]
2923
fn next(&mut self) -> Option<Self::Item> {
3024
if self.t == T::ZERO {
31-
return None;
25+
None
26+
} else {
27+
let tz = self.t.trailing_zeros();
28+
self.t = self.t ^ (T::ONE << tz);
29+
Some(tz as usize)
3230
}
33-
34-
let tz = self.t.trailing_zeros();
35-
self.t = self.t ^ (T::ONE << tz);
36-
Some(tz as usize)
3731
}
3832
}

src/util/grid.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ impl Grid<u8> {
3838
#[inline]
3939
pub fn parse(input: &str) -> Self {
4040
let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
41+
4142
let width = raw[0].len() as i32;
4243
let height = raw.len() as i32;
43-
let mut bytes = Vec::with_capacity((width * height) as usize);
44-
raw.iter().for_each(|slice| bytes.extend_from_slice(slice));
44+
let bytes = raw.concat();
45+
4546
Grid { width, height, bytes }
4647
}
4748

src/util/iter.rs

Lines changed: 32 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Add a `chunk` method to [`Iterator`] that duplicates the functionality of the unstable
22
//! [`array_chunks`] method.
33
//!
4-
//! Using Rust's const generics, concrete implementations are provided for sizes 2 to 8 to handle
4+
//! Using Rust's const generics, concrete implementations are provided for sizes 2 to 12 to handle
55
//! the most common situations. Once [`array_chunks`] is stablized then this module can be removed.
66
//!
77
//! [`array_chunks`]: std::iter::Iterator::array_chunks
@@ -15,104 +15,34 @@ pub trait ChunkOps: Iterator + Sized {
1515

1616
impl<I: Iterator> ChunkOps for I {
1717
fn chunk<const N: usize>(self) -> Chunk<Self, N> {
18-
Chunk::<Self, N> { iter: self }
19-
}
20-
}
21-
22-
impl<I: Iterator> Iterator for Chunk<I, 2> {
23-
type Item = [I::Item; 2];
24-
25-
#[inline]
26-
fn next(&mut self) -> Option<Self::Item> {
27-
let a = self.iter.next()?;
28-
let b = self.iter.next()?;
29-
Some([a, b])
30-
}
31-
}
32-
33-
impl<I: Iterator> Iterator for Chunk<I, 3> {
34-
type Item = [I::Item; 3];
35-
36-
#[inline]
37-
fn next(&mut self) -> Option<Self::Item> {
38-
let a = self.iter.next()?;
39-
let b = self.iter.next()?;
40-
let c = self.iter.next()?;
41-
Some([a, b, c])
42-
}
43-
}
44-
45-
impl<I: Iterator> Iterator for Chunk<I, 4> {
46-
type Item = [I::Item; 4];
47-
48-
#[inline]
49-
fn next(&mut self) -> Option<Self::Item> {
50-
let a = self.iter.next()?;
51-
let b = self.iter.next()?;
52-
let c = self.iter.next()?;
53-
let d = self.iter.next()?;
54-
Some([a, b, c, d])
55-
}
56-
}
57-
58-
impl<I: Iterator> Iterator for Chunk<I, 5> {
59-
type Item = [I::Item; 5];
60-
61-
#[inline]
62-
fn next(&mut self) -> Option<Self::Item> {
63-
let a = self.iter.next()?;
64-
let b = self.iter.next()?;
65-
let c = self.iter.next()?;
66-
let d = self.iter.next()?;
67-
let e = self.iter.next()?;
68-
Some([a, b, c, d, e])
69-
}
70-
}
71-
72-
impl<I: Iterator> Iterator for Chunk<I, 6> {
73-
type Item = [I::Item; 6];
74-
75-
#[inline]
76-
fn next(&mut self) -> Option<Self::Item> {
77-
let a = self.iter.next()?;
78-
let b = self.iter.next()?;
79-
let c = self.iter.next()?;
80-
let d = self.iter.next()?;
81-
let e = self.iter.next()?;
82-
let f = self.iter.next()?;
83-
Some([a, b, c, d, e, f])
84-
}
85-
}
86-
87-
impl<I: Iterator> Iterator for Chunk<I, 7> {
88-
type Item = [I::Item; 7];
89-
90-
#[inline]
91-
fn next(&mut self) -> Option<Self::Item> {
92-
let a = self.iter.next()?;
93-
let b = self.iter.next()?;
94-
let c = self.iter.next()?;
95-
let d = self.iter.next()?;
96-
let e = self.iter.next()?;
97-
let f = self.iter.next()?;
98-
let g = self.iter.next()?;
99-
Some([a, b, c, d, e, f, g])
100-
}
101-
}
102-
103-
impl<I: Iterator> Iterator for Chunk<I, 8> {
104-
type Item = [I::Item; 8];
105-
106-
#[inline]
107-
fn next(&mut self) -> Option<Self::Item> {
108-
let a = self.iter.next()?;
109-
let b = self.iter.next()?;
110-
let c = self.iter.next()?;
111-
let d = self.iter.next()?;
112-
let e = self.iter.next()?;
113-
let f = self.iter.next()?;
114-
let g = self.iter.next()?;
115-
let h = self.iter.next()?;
116-
Some([a, b, c, d, e, f, g, h])
117-
}
118-
}
18+
Chunk { iter: self }
19+
}
20+
}
21+
22+
macro_rules! iterator {
23+
($n:literal, $($var:ident),+) => {
24+
impl<I: Iterator> Iterator for Chunk<I, $n> {
25+
type Item = [I::Item; $n];
26+
27+
#[inline]
28+
fn next(&mut self) -> Option<Self::Item> {
29+
Some([$({
30+
let $var = self.iter.next()?;
31+
$var
32+
}),+])
33+
}
34+
}
35+
};
36+
}
37+
38+
iterator!(2, a, b);
39+
iterator!(3, a, b, c);
40+
iterator!(4, a, b, c, d);
41+
iterator!(5, a, b, c, d, e);
42+
iterator!(6, a, b, c, d, e, f);
43+
iterator!(7, a, b, c, d, e, f, g);
44+
iterator!(8, a, b, c, d, e, f, g, h);
45+
iterator!(9, a, b, c, d, e, f, g, h, i);
46+
iterator!(10, a, b, c, d, e, f, g, h, i, j);
47+
iterator!(11, a, b, c, d, e, f, g, h, i, j, k);
48+
iterator!(12, a, b, c, d, e, f, g, h, i, j, k, l);

src/util/math.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub trait SignedMathOps<T: Signed<T>> {
2828

2929
impl<T: Integer<T>> IntegerMathOps<T> for T {
3030
/// Greatest common divisor
31+
#[inline]
3132
fn gcd(self, mut b: T) -> T {
3233
let mut a = self;
3334

@@ -38,40 +39,43 @@ impl<T: Integer<T>> IntegerMathOps<T> for T {
3839
a
3940
}
4041

41-
// Least common multiple
42+
/// Least common multiple
43+
#[inline]
4244
fn lcm(self, b: T) -> T {
4345
self * (b / self.gcd(b))
4446
}
4547

46-
// Modular exponentation
48+
/// Modular exponentiation
49+
#[inline]
4750
fn mod_pow(self, mut e: T, m: T) -> T {
48-
let mut b = self;
49-
let mut c = T::ONE;
51+
let mut base = self;
52+
let mut result = T::ONE;
5053

5154
while e > T::ZERO {
5255
if e & T::ONE == T::ONE {
53-
c = (c * b) % m;
56+
result = (result * base) % m;
5457
}
55-
b = (b * b) % m;
58+
base = (base * base) % m;
5659
e = e >> 1;
5760
}
5861

59-
c
62+
result
6063
}
6164
}
6265

6366
impl<T: Signed<T>> SignedMathOps<T> for T {
64-
// Modular multiplicative inverse
67+
/// Modular multiplicative inverse
68+
#[inline]
6569
fn mod_inv(self, m: T) -> Option<T> {
6670
let mut t = T::ZERO;
67-
let mut newt = T::ONE;
71+
let mut new_t = T::ONE;
6872
let mut r = m;
69-
let mut newr = self;
73+
let mut new_r = self;
7074

71-
while newr != T::ZERO {
72-
let quotient = r / newr;
73-
(t, newt) = (newt, t - quotient * newt);
74-
(r, newr) = (newr, r - quotient * newr);
75+
while new_r != T::ZERO {
76+
let quotient = r / new_r;
77+
(t, new_t) = (new_t, t - quotient * new_t);
78+
(r, new_r) = (new_r, r - quotient * new_r);
7579
}
7680

7781
if r > T::ONE {

0 commit comments

Comments
 (0)