Skip to content

Commit 7075b8a

Browse files
committed
Fix clippy warnings and cleanup code
1 parent 64a1a6e commit 7075b8a

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

2021/src/bin/day1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
use advent_lib::{day_main, day_test};
44

5+
#[allow(clippy::ptr_arg)]
56
fn calculate_part1(input: &Vec<u32>) -> usize {
67
input.windows(2).filter(|w| w[1] > w[0]).count()
78
}
89

10+
#[allow(clippy::ptr_arg)]
911
fn calculate_part2(input: &Vec<u32>) -> usize {
1012
input
1113
.windows(3)

2021/src/bin/day2.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(test)]
2-
32
use advent_lib::{day_main, day_test};
43
use nom_parse_macros::parse_from;
54

@@ -26,6 +25,7 @@ impl Position {
2625
}
2726
}
2827

28+
#[allow(clippy::ptr_arg)]
2929
fn calculate_part1(commands: &Vec<Command>) -> i32 {
3030
commands
3131
.iter()
@@ -40,6 +40,7 @@ fn calculate_part1(commands: &Vec<Command>) -> i32 {
4040
.get_result()
4141
}
4242

43+
#[allow(clippy::ptr_arg)]
4344
fn calculate_part2(commands: &Vec<Command>) -> i32 {
4445
commands
4546
.iter()

2021/src/bin/day3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> ParseFrom<&'a [u8]> for Input {
3232
}
3333
}
3434

35-
fn has_majority_ones(numbers: &Vec<u32>, mask: u32) -> bool {
35+
fn has_majority_ones(numbers: &[u32], mask: u32) -> bool {
3636
numbers.iter().filter(|&&nr| (nr & mask) != 0).count() * 2 >= numbers.len()
3737
}
3838

2021/src/bin/day4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Board {
2828
}
2929
}
3030
}
31-
return None;
31+
None
3232
}
3333

3434
fn has_bingo(&self, row: usize, col: usize) -> bool {

2021/src/bin/day5.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(test)]
22

33
use advent_lib::{
4-
geometry::{vector2, Point, Vector},
4+
geometry::{Point, Vector},
55
grid::Grid,
66
lines::LineSegment,
77
*,
@@ -31,37 +31,44 @@ impl Iterator for LineIterator {
3131

3232
fn iter(line: &Line) -> LineIterator {
3333
LineIterator {
34-
line: line.clone(),
35-
next: Some(line.start.clone()),
36-
step: {
37-
let direction = line.end - line.start;
38-
vector2(direction.x().signum(), direction.y().signum())
39-
},
34+
line: *line,
35+
next: Some(line.start),
36+
step: (line.end - line.start).signum(),
4037
}
4138
}
4239

4340
fn is_horizontal_or_vertical(line: &Line) -> bool {
4441
line.start.x() == line.end.x() || line.start.y() == line.end.y()
4542
}
4643

44+
fn write_line_to(grid: &mut Grid<u8>, line: &LineSegment<2, i32>) {
45+
for point in iter(line) {
46+
if let Some(e) = grid.get_mut(point) {
47+
*e += 1;
48+
}
49+
}
50+
}
51+
4752
fn calculate_part1(input: &Vec<Line>) -> u64 {
48-
let mut grid = Grid::<u8>::new_empty(1000, 1000);
53+
let mut grid = Grid::<u8>::new_empty(
54+
input.iter().map(|l| l.max_x()).max().unwrap() + 1,
55+
input.iter().map(|l| l.max_y()).max().unwrap() + 1,
56+
);
4957
for line in input {
5058
if is_horizontal_or_vertical(line) {
51-
for point in iter(line) {
52-
grid.get_mut(point).map(|e| *e += 1);
53-
}
59+
write_line_to(&mut grid, line);
5460
}
5561
}
5662
grid.entries().filter(|(_, &value)| value >= 2).count() as u64
5763
}
5864

5965
fn calculate_part2(input: &Vec<Line>) -> u64 {
60-
let mut grid = Grid::<u8>::new_empty(1000, 1000);
66+
let mut grid = Grid::<u8>::new_empty(
67+
input.iter().map(|l| l.max_x()).max().unwrap() + 1,
68+
input.iter().map(|l| l.max_y()).max().unwrap() + 1,
69+
);
6170
for line in input {
62-
for point in iter(line) {
63-
grid.get_mut(point).map(|e| *e += 1);
64-
}
71+
write_line_to(&mut grid, line);
6572
}
6673
grid.entries().filter(|(_, &value)| value >= 2).count() as u64
6774
}

shared/src/geometry.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ pub struct Vector<const D: usize, T> {
8181
pub coords: [T; D],
8282
}
8383

84+
macro_rules! number_ops {
85+
() => {};
86+
($type:tt $($types:tt )*) => {
87+
impl<const D: usize> Vector<D, $type> {
88+
pub fn abs(self) -> Self { Self { coords: self.coords.map(|nr| nr.abs()) } }
89+
pub fn signum(self) -> Self { Self { coords: self.coords.map(|nr| nr.signum()) } }
90+
}
91+
number_ops!($($types )*);
92+
};
93+
}
94+
95+
number_ops!(i8 i16 i32 i64);
96+
8497
impl<I, E, const D: usize, T> ParseFrom<I, E> for Vector<D, T>
8598
where
8699
T: Default + Copy + ParseFrom<I, E>,

shared/src/iter_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub trait IteratorUtils: Iterator {
2121
Self::Item: Default + Copy,
2222
{
2323
let mut window: [Self::Item; D] = [Default::default(); D];
24-
for ix in 1..D {
24+
for e in &mut window[1..] {
2525
if let Some(value) = self.next() {
26-
window[ix] = value;
26+
*e = value;
2727
} else {
2828
break;
2929
}

0 commit comments

Comments
 (0)