Skip to content

Commit 0027a0e

Browse files
committed
Comprehensive code linting
1 parent a1df0d8 commit 0027a0e

Some content is hidden

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

63 files changed

+263
-326
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ 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 }
18+
Chunk { iter: self }
1919
}
2020
}
2121

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 exponentation
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 {

src/util/parse.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,12 @@ pub trait ParseOps {
4848
impl<S: AsRef<str>> ParseOps for S {
4949
fn unsigned<T: Unsigned<T>>(&self) -> T {
5050
let str = self.as_ref();
51-
match try_unsigned(&mut str.bytes()) {
52-
Some(t) => t,
53-
None => panic!("Unable to parse \"{str}\""),
54-
}
51+
try_unsigned(&mut str.bytes()).unwrap_or_else(|| panic!("Unable to parse \"{str}\""))
5552
}
5653

5754
fn signed<T: Signed<T>>(&self) -> T {
5855
let str = self.as_ref();
59-
match try_signed(&mut str.bytes()) {
60-
Some(t) => t,
61-
None => panic!("Unable to parse \"{str}\""),
62-
}
56+
try_signed(&mut str.bytes()).unwrap_or_else(|| panic!("Unable to parse \"{str}\""))
6357
}
6458

6559
fn iter_unsigned<T: Unsigned<T>>(&self) -> ParseUnsigned<'_, T> {
@@ -74,11 +68,13 @@ impl<S: AsRef<str>> ParseOps for S {
7468
impl<T: Unsigned<T>> Iterator for ParseUnsigned<'_, T> {
7569
type Item = T;
7670

71+
#[inline]
7772
fn size_hint(&self) -> (usize, Option<usize>) {
7873
let (lower, upper) = self.bytes.size_hint();
7974
(lower / 3, upper.map(|u| u / 3))
8075
}
8176

77+
#[inline]
8278
fn next(&mut self) -> Option<Self::Item> {
8379
try_unsigned(&mut self.bytes)
8480
}
@@ -87,21 +83,21 @@ impl<T: Unsigned<T>> Iterator for ParseUnsigned<'_, T> {
8783
impl<T: Signed<T>> Iterator for ParseSigned<'_, T> {
8884
type Item = T;
8985

86+
#[inline]
9087
fn size_hint(&self) -> (usize, Option<usize>) {
9188
let (lower, upper) = self.bytes.size_hint();
9289
(lower / 3, upper.map(|u| u / 3))
9390
}
9491

92+
#[inline]
9593
fn next(&mut self) -> Option<Self::Item> {
9694
try_signed(&mut self.bytes)
9795
}
9896
}
9997

10098
fn try_unsigned<T: Unsigned<T>>(bytes: &mut Bytes<'_>) -> Option<T> {
10199
let mut n = loop {
102-
let byte = bytes.next()?;
103-
let digit = byte.to_decimal();
104-
100+
let digit = bytes.next()?.to_decimal();
105101
if digit < 10 {
106102
break T::from(digit);
107103
}
@@ -120,9 +116,7 @@ fn try_unsigned<T: Unsigned<T>>(bytes: &mut Bytes<'_>) -> Option<T> {
120116

121117
fn try_signed<T: Signed<T>>(bytes: &mut Bytes<'_>) -> Option<T> {
122118
let (mut n, negative) = loop {
123-
let byte = bytes.next()?;
124-
let digit = byte.to_decimal();
125-
119+
let digit = bytes.next()?.to_decimal();
126120
if digit == 253 {
127121
break (T::ZERO, true);
128122
}

src/util/slice.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ impl<T> SliceOps<T> for &mut [T] {
3131

3232
while i < n {
3333
if c[i] < i {
34-
if i.is_multiple_of(2) {
35-
self.swap(0, i);
36-
} else {
37-
self.swap(c[i], i);
38-
}
34+
let swap_index = if i.is_multiple_of(2) { 0 } else { c[i] };
35+
self.swap(swap_index, i);
3936
callback(self);
4037
c[i] += 1;
4138
i = 1;

src/util/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
66
use std::thread::*;
77

8-
// Usually the number of physical cores.
8+
/// Usually the number of physical cores.
99
fn threads() -> usize {
1010
available_parallelism().unwrap().get()
1111
}

0 commit comments

Comments
 (0)