Skip to content

Commit 99fcfce

Browse files
author
mflinn-broad
committed
clippy
1 parent f73d7a1 commit 99fcfce

File tree

18 files changed

+165
-42
lines changed

18 files changed

+165
-42
lines changed

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ edition = "2021"
77

88
[dependencies]
99
itertools = "0.10.3"
10+
pathfinding = "3.0.5"
1011
petgraph = "0.6.0"

src/days/day1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ fn process_input(input: String) -> Vec<i64> {
77
.collect()
88
}
99

10-
fn part_1(data: &Vec<i64>) -> usize {
10+
fn part_1(data: &[i64]) -> usize {
1111
get_num_increasing_windows(2, data)
1212
}
1313

14-
fn part_2(data: &Vec<i64>) -> usize {
14+
fn part_2(data: &[i64]) -> usize {
1515
get_num_increasing_windows(4, data)
1616
}
1717

18-
fn get_num_increasing_windows(window_size: usize, data: &Vec<i64>) -> usize {
18+
fn get_num_increasing_windows(window_size: usize, data: &[i64]) -> usize {
1919
data.windows(window_size)
2020
.filter(|window| window[0] < window[window_size - 1])
2121
.count()

src/days/day10.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn process(input: &str) -> Vec<Vec<char>> {
1515
input.lines().map(|line| line.chars().collect()).collect()
1616
}
1717

18-
fn part_1(input: &Vec<Vec<char>>) -> usize {
18+
fn part_1(input: &[Vec<char>]) -> usize {
1919
let mut error_tracker: HashMap<char, usize> = HashMap::new();
2020
input.iter().for_each(|line| {
2121
let mut stack: Vec<char> = Vec::new();
@@ -48,7 +48,7 @@ fn part_1(input: &Vec<Vec<char>>) -> usize {
4848
})
4949
}
5050

51-
fn part_2(input: &Vec<Vec<char>>) -> usize {
51+
fn part_2(input: &[Vec<char>]) -> usize {
5252
// remove corrupted and complete lines
5353
let completion_scores: Vec<usize> = input
5454
.iter()

src/days/day11.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Octopus {
3131
}
3232
}
3333

34-
fn get_adjacent_octopi(pos: Position, grid: &Vec<Vec<Octopus>>) -> Vec<Position> {
34+
fn get_adjacent_octopi(pos: Position, grid: &[Vec<Octopus>]) -> Vec<Position> {
3535
let mut adjacents: Vec<Position> = Vec::new();
3636
if pos.row != 0 {
3737
let position = Position {
@@ -99,7 +99,7 @@ fn get_adjacent_octopi(pos: Position, grid: &Vec<Vec<Octopus>>) -> Vec<Position>
9999
fn propagate_flash(pos: Position, grid: &mut Vec<Vec<Octopus>>) {
100100
let mut flash_stack: Vec<Position> = vec![pos];
101101
while let Some(flash_pos) = flash_stack.pop() {
102-
let adjacents = get_adjacent_octopi(flash_pos, &grid);
102+
let adjacents = get_adjacent_octopi(flash_pos, grid);
103103
adjacents.iter().for_each(|adjacent| {
104104
if !grid[adjacent.row][adjacent.col].flashed_on_step {
105105
grid[adjacent.row][adjacent.col].increase();

src/days/day12.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ fn process(input: &str) -> Vec<(&str, &str)> {
2222
.collect()
2323
}
2424

25-
fn part_1(input: &Vec<(&str, &str)>) -> usize {
25+
fn part_1(input: &[(&str, &str)]) -> usize {
2626
let cave_map = UnGraphMap::<&str, ()>::from_edges(input);
2727
let mut visit_tracker: HashSet<&str> = HashSet::new();
2828
visit_tracker.insert("start");
2929

3030
count_simple_paths(&cave_map, "start", &mut visit_tracker)
3131
}
3232

33-
fn part_2(input: &Vec<(&str, &str)>) -> usize {
33+
fn part_2(input: &[(&str, &str)]) -> usize {
3434
let cave_map = UnGraphMap::<&str, ()>::from_edges(input);
3535
let mut visit_tracker: HashSet<&str> = HashSet::new();
3636
visit_tracker.insert("start");
@@ -70,7 +70,7 @@ fn count_simple_paths<'a>(
7070
visited_small_caves.insert(connected_cave);
7171
}
7272
}
73-
count += count_simple_paths(&cave_system, connected_cave, visited_small_caves);
73+
count += count_simple_paths(cave_system, connected_cave, visited_small_caves);
7474
visited_small_caves.remove(connected_cave);
7575
}
7676
count
@@ -102,7 +102,7 @@ fn count_paths_v2<'a>(
102102
}
103103
}
104104

105-
if let Some(_) = twive_visited_cave {
105+
if twive_visited_cave.is_some() {
106106
count += count_simple_paths(cave_system, connected_cave, visited_small_caves);
107107
} else {
108108
count += count_paths_v2(
@@ -123,7 +123,7 @@ fn count_paths_v2<'a>(
123123
count
124124
}
125125

126-
fn make_diagram(input: &Vec<(&str, &str)>) -> std::io::Result<()> {
126+
fn make_diagram(input: &[(&str, &str)]) -> std::io::Result<()> {
127127
let cave_map = UnGraphMap::<&str, ()>::from_edges(input);
128128

129129
let cave_diagram = Dot::with_config(&cave_map, &[Config::EdgeNoLabel]);

src/days/day13.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn run() {
1111

1212
fn part_1(input: (&Vec<Point>, &Vec<Fold>)) -> usize {
1313
let (points, folds) = input;
14-
compute_fold(points, folds[0]).iter().count()
14+
compute_fold(points, folds[0]).len()
1515
}
1616

1717
fn part_2(input: (Vec<Point>, &Vec<Fold>)) -> String {
@@ -51,7 +51,7 @@ struct Point {
5151
y: usize,
5252
}
5353

54-
fn compute_fold(points: &Vec<Point>, fold: Fold) -> Vec<Point> {
54+
fn compute_fold(points: &[Point], fold: Fold) -> Vec<Point> {
5555
let mapped_points: Vec<Point> = match fold.axis {
5656
Axis::X => points
5757
.iter()

src/days/day14.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn polymer_strength(input: Input, steps: usize) -> usize {
3434
let single_count = singles.get_mut(single).unwrap();
3535
*single_count += pair_count;
3636

37-
let new_pair_a = format!("{}{}", pair.chars().nth(0).unwrap(), single);
37+
let new_pair_a = format!("{}{}", pair.chars().next().unwrap(), single);
3838
let new_pair_b = format!("{}{}", single, pair.chars().nth(1).unwrap());
3939

4040
let new_pair_a_count = new_pairs.entry(new_pair_a).or_insert(0);
@@ -52,7 +52,6 @@ fn polymer_strength(input: Input, steps: usize) -> usize {
5252
} else {
5353
0
5454
}
55-
5655
}
5756

5857
fn process(input: &str) -> Input {

src/days/day15.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use crate::util;
2+
use pathfinding::directed::dijkstra;
3+
4+
const SURROUNDING_POINTS: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
5+
6+
pub fn run() {
7+
let raw_input = util::read_input("inputs/day15.txt").unwrap();
8+
let input = process(&raw_input);
9+
println!("Part 1: {}", part_1(input.clone()));
10+
println!("Part 2: {}", part_2(input));
11+
}
12+
13+
fn part_1(input: Vec<Vec<i32>>) -> i32 {
14+
let start = (0, 0);
15+
let goal = (input.len() as i32 - 1, input[0].len() as i32 - 1);
16+
17+
dijkstra::dijkstra(
18+
&start,
19+
|(x, y)| {
20+
SURROUNDING_POINTS
21+
.iter()
22+
.map(|(x_move, y_move)| {
23+
input
24+
.get((x + x_move) as usize)
25+
.and_then(|row| row.get((y + y_move) as usize))
26+
.map(|risk| ((x + x_move, y + y_move), *risk))
27+
})
28+
.flatten()
29+
.collect::<Vec<_>>()
30+
},
31+
|&node| node == goal,
32+
)
33+
.unwrap()
34+
.1
35+
}
36+
37+
fn part_2(input: Vec<Vec<i32>>) -> i32 {
38+
let square_size = input.len();
39+
let start = (0, 0);
40+
let goal = (square_size as i32 * 5 - 1, square_size as i32 * 5 - 1);
41+
42+
dijkstra::dijkstra(
43+
&start,
44+
|(x, y)| {
45+
SURROUNDING_POINTS
46+
.iter()
47+
.map(|(x_move, y_move)| ((x + x_move) as usize, (y + y_move) as usize))
48+
.filter(|(x, y)| (x / 5 < square_size && y / 5 < square_size))
49+
.map(|(x, y)| {
50+
input
51+
.get(x % square_size)
52+
.and_then(|row| row.get(y % square_size))
53+
.map(|risk| {
54+
(
55+
(x as i32, y as i32),
56+
((*risk as usize + (x / square_size) + (y / square_size) - 1) % 9
57+
+ 1) as i32,
58+
)
59+
})
60+
})
61+
.flatten()
62+
.collect::<Vec<_>>()
63+
},
64+
|&node| node == goal,
65+
)
66+
.unwrap()
67+
.1
68+
}
69+
70+
fn process(input: &str) -> Vec<Vec<i32>> {
71+
input
72+
.lines()
73+
.map(|line| {
74+
line.trim()
75+
.chars()
76+
.map(|ch| ch.to_string().parse().unwrap())
77+
.collect()
78+
})
79+
.collect()
80+
}

src/days/day2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn process_input(input: String) -> Vec<(Direction, i64)> {
2828
.collect()
2929
}
3030

31-
fn part_1(input: &Vec<(Direction, i64)>) -> i64 {
31+
fn part_1(input: &[(Direction, i64)]) -> i64 {
3232
let position = input.iter().fold((0, 0), |acc, (dir, amt)| match dir {
3333
Direction::Forward => (acc.0 + amt, acc.1),
3434
Direction::Down => (acc.0, acc.1 + amt),
@@ -37,7 +37,7 @@ fn part_1(input: &Vec<(Direction, i64)>) -> i64 {
3737
position.0 * position.1
3838
}
3939

40-
fn part_2(input: &Vec<(Direction, i64)>) -> i64 {
40+
fn part_2(input: &[(Direction, i64)]) -> i64 {
4141
let coordinates = input.iter().fold((0, 0, 0), |acc, (dir, amt)| match dir {
4242
Direction::Forward => (acc.0 + amt, acc.1 + amt * acc.2, acc.2),
4343
Direction::Down => (acc.0, acc.1, acc.2 + amt),

src/days/day4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct BingoCard {
1111
}
1212

1313
impl BingoCard {
14-
fn new(rows: &Vec<Vec<u64>>) -> Self {
14+
fn new(rows: &[Vec<u64>]) -> Self {
1515
let card: Vec<Vec<Cell>> = rows
1616
.iter()
1717
.map(|line| line.iter().map(|val| Cell::new(*val)).collect())
@@ -115,7 +115,7 @@ fn process(input: &str) -> (CalledNumbers, Vec<BingoCard>) {
115115
let mut chunks = input.split("\n\n");
116116

117117
let called_numbers = process_header(chunks.next().unwrap());
118-
let cards: Vec<BingoCard> = chunks.map(|card| process_card(card)).collect();
118+
let cards: Vec<BingoCard> = chunks.map(process_card).collect();
119119
(called_numbers, cards)
120120
}
121121

src/days/day5.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn part_1(input: LineList) -> usize {
3737
let lines = get_verticals_and_horizontals(input);
3838
let mut point_tracker: HashMap<Point, usize> = HashMap::new();
3939
lines.iter().for_each(|line| match line_type(line) {
40-
LineType::Vertical => update_vertical(&line, &mut point_tracker),
41-
LineType::Horizontal => update_horizontal(&line, &mut point_tracker),
40+
LineType::Vertical => update_vertical(line, &mut point_tracker),
41+
LineType::Horizontal => update_horizontal(line, &mut point_tracker),
4242
_ => panic!("unreachable"),
4343
});
4444
count_overlaps(&point_tracker)
@@ -47,9 +47,9 @@ fn part_1(input: LineList) -> usize {
4747
fn part_2(lines: LineList) -> usize {
4848
let mut point_tracker: HashMap<Point, usize> = HashMap::new();
4949
lines.iter().for_each(|line| match line_type(line) {
50-
LineType::Vertical => update_vertical(&line, &mut point_tracker),
51-
LineType::Horizontal => update_horizontal(&line, &mut point_tracker),
52-
_ => update_diagonal(&line, &mut point_tracker),
50+
LineType::Vertical => update_vertical(line, &mut point_tracker),
51+
LineType::Horizontal => update_horizontal(line, &mut point_tracker),
52+
_ => update_diagonal(line, &mut point_tracker),
5353
});
5454
count_overlaps(&point_tracker)
5555
}

src/days/day6.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ fn process_generations(state: [u64; 9], num_generations: usize) -> usize {
3030
let mut temp_state: [u64; 9] = [0; 9];
3131
temp_state.copy_from_slice(&curr_state);
3232

33-
for bucket in 1..9 {
34-
curr_state[bucket - 1] = temp_state[bucket];
35-
}
33+
curr_state[..(9 - 1)].clone_from_slice(&temp_state[1..9]);
3634

3735
curr_state[6] += temp_state[0];
3836
curr_state[8] = temp_state[0];

src/days/day7.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ fn process(input: &str) -> Vec<usize> {
1717
}
1818

1919
fn part_1(input: &mut Vec<usize>) -> usize {
20-
input.sort();
20+
input.sort_unstable();
2121

2222
let median = input[(input.len() / 2) + (input.len() % 2)];
23-
cost_at(median, &input)
23+
cost_at(median, input)
2424
}
2525

26-
fn part_2(input: &Vec<usize>) -> usize {
26+
fn part_2(input: &[usize]) -> usize {
2727
let mean: usize = input.iter().sum::<usize>() / input.len();
2828
(mean - 5..mean + 5)
2929
.map(|pos| cost_at_v2(pos, input))
3030
.min()
3131
.unwrap()
3232
}
3333

34-
fn cost_at(pos: usize, crabs: &Vec<usize>) -> usize {
34+
fn cost_at(pos: usize, crabs: &[usize]) -> usize {
3535
crabs.iter().map(|crab_pos| pos.abs_diff(*crab_pos)).sum()
3636
}
3737

38-
fn cost_at_v2(pos: usize, crabs: &Vec<usize>) -> usize {
38+
fn cost_at_v2(pos: usize, crabs: &[usize]) -> usize {
3939
crabs
4040
.iter()
4141
.map(|crab_pos| {

0 commit comments

Comments
 (0)