Skip to content

Commit 72d04c1

Browse files
committed
Tidy code
1 parent c64abfd commit 72d04c1

File tree

19 files changed

+96
-166
lines changed

19 files changed

+96
-166
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8080
| 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [Source](src/year2024/day03.rs) | 8 |
8181
| 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Source](src/year2024/day04.rs) | 77 |
8282
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
83-
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 439 |
83+
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 405 |
8484
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 136 |
8585
| 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Source](src/year2024/day08.rs) | 8 |
8686
| 9 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | [Source](src/year2024/day09.rs) | 106 |

src/year2016/day03.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ pub fn part1(input: &[u32]) -> usize {
1717
}
1818

1919
pub fn part2(input: &[u32]) -> usize {
20-
let first = count(input.iter().step_by(3));
21-
let second = count(input.iter().skip(1).step_by(3));
22-
let third = count(input.iter().skip(2).step_by(3));
23-
first + second + third
20+
(0..3).map(|skip| count(input.iter().skip(skip).step_by(3))).sum()
2421
}
2522

2623
fn count<'a, I>(iter: I) -> usize

src/year2017/day06.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,10 @@ pub fn parse(input: &str) -> Input {
5555
// the mask unchanged.
5656
// If some nibbles have that bit set, then we will "narrow" the mask to only consider
5757
// those nibbles.
58-
let mut mask = 0x8888888888888888;
59-
60-
let first = memory & mask;
61-
mask = if first == 0 { mask } else { first };
62-
63-
let second = (memory << 1) & mask;
64-
mask = if second == 0 { mask } else { second };
65-
66-
let third = (memory << 2) & mask;
67-
mask = if third == 0 { mask } else { third };
68-
69-
let fourth = (memory << 3) & mask;
70-
mask = if fourth == 0 { mask } else { fourth };
58+
let mask = (0..4).fold(0x8888888888888888, |mask, shift| {
59+
let result = (memory << shift) & mask;
60+
if result == 0 { mask } else { result }
61+
});
7162

7263
// The mask will have a 1 bit set for each of the joint highest values.
7364
// Choose the lowest index which is the most significant bit set.

src/year2017/day13.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,13 @@ pub fn parse(input: &str) -> Input {
6565

6666
/// Leaving at time zero the packet will encounter each scanner at time `depth`.
6767
pub fn part1(input: &Input) -> u32 {
68-
let mut result = 0;
69-
70-
for &[depth, range] in input {
71-
let period = 2 * (range - 1);
72-
if depth.is_multiple_of(period) {
73-
result += depth * range;
74-
}
75-
}
76-
77-
result
68+
input
69+
.iter()
70+
.filter_map(|&[depth, range]| {
71+
let period = 2 * (range - 1);
72+
depth.is_multiple_of(period).then_some(depth * range)
73+
})
74+
.sum()
7875
}
7976

8077
/// Sieves possible values at each scanner stage to reduce the number of possible values.

src/year2018/day07.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,7 @@ pub fn parse(input: &str) -> Input {
3535
pub fn part1(input: &Input) -> String {
3636
// Move all steps with no dependencies to the `ready` map. A `BTreeMap` is sorted by key
3737
// so will retrieve steps in alphabetical order.
38-
let mut ready = BTreeMap::new();
39-
let mut blocked = FastMap::new();
40-
41-
for (key, step) in input.clone() {
42-
if step.remaining == 0 {
43-
ready.insert(key, step);
44-
} else {
45-
blocked.insert(key, step);
46-
}
47-
}
48-
38+
let (mut ready, mut blocked) = split_by_readiness(input);
4939
let mut done = String::new();
5040

5141
while let Some((key, step)) = ready.pop_first() {
@@ -75,16 +65,7 @@ pub fn part2(input: &Input) -> u32 {
7565

7666
pub fn part2_testable(input: &Input, max_workers: usize, base_duration: u32) -> u32 {
7767
// Same as part one, move all tasks that are root nodes to the `ready` map.
78-
let mut ready = BTreeMap::new();
79-
let mut blocked = FastMap::new();
80-
81-
for (key, step) in input.clone() {
82-
if step.remaining == 0 {
83-
ready.insert(key, step);
84-
} else {
85-
blocked.insert(key, step);
86-
}
87-
}
68+
let (mut ready, mut blocked) = split_by_readiness(input);
8869

8970
// Loop until there are no more steps available and all workers are idle.
9071
let mut time = 0;
@@ -123,3 +104,18 @@ pub fn part2_testable(input: &Input, max_workers: usize, base_duration: u32) ->
123104

124105
time
125106
}
107+
108+
fn split_by_readiness(input: &Input) -> (BTreeMap<u8, Step>, FastMap<u8, Step>) {
109+
let mut ready = BTreeMap::new();
110+
let mut blocked = FastMap::new();
111+
112+
for (key, step) in input.clone() {
113+
if step.remaining == 0 {
114+
ready.insert(key, step);
115+
} else {
116+
blocked.insert(key, step);
117+
}
118+
}
119+
120+
(ready, blocked)
121+
}

src/year2018/day16.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn part2(input: &Input) -> usize {
7272
// that only has one possibility.
7373
let mut convert = [0; 16];
7474

75-
while let Some(index) = masks.iter().position(|m| m.count_ones() == 1) {
75+
while let Some(index) = masks.iter().position(|&n| n.count_ones() == 1) {
7676
let mask = masks[index];
7777
// This opcode has only 1 possible mapping, so remove possibility from other opcodes.
7878
masks.iter_mut().for_each(|m| *m &= !mask);

src/year2020/day06.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,23 @@ pub fn parse(input: &str) -> Vec<u32> {
2020
}
2121

2222
pub fn part1(input: &[u32]) -> u32 {
23-
let mut total = 0;
24-
let mut group = u32::MIN;
25-
26-
for &passenger in input {
27-
if passenger == 0 {
28-
total += group.count_ones();
29-
group = u32::MIN;
30-
} else {
31-
group |= passenger;
32-
}
33-
}
34-
35-
total + group.count_ones()
23+
count_answers(input, u32::MIN, |group, passenger| group | passenger)
3624
}
3725

3826
pub fn part2(input: &[u32]) -> u32 {
27+
count_answers(input, u32::MAX, |group, passenger| group & passenger)
28+
}
29+
30+
fn count_answers(input: &[u32], initial: u32, combine: fn(u32, u32) -> u32) -> u32 {
3931
let mut total = 0;
40-
let mut group = u32::MAX;
32+
let mut group = initial;
4133

4234
for &passenger in input {
4335
if passenger == 0 {
4436
total += group.count_ones();
45-
group = u32::MAX;
37+
group = initial;
4638
} else {
47-
group &= passenger;
39+
group = combine(group, passenger);
4840
}
4941
}
5042

src/year2020/day09.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! # Custom Customs
1+
//! # Encoding Error
22
//!
33
//! Part one is solved with a brute force search over every possible pair in the preamble, using a
44
//! sliding window to advance to each number. To allow testing with the sample data that uses a

src/year2021/day07.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ pub fn part2(input: &[i32]) -> i32 {
2424
(n * (n + 1)) / 2
2525
};
2626

27-
let first: i32 = input.iter().map(|&x| triangle(x, mean)).sum();
28-
let second: i32 = input.iter().map(|&x| triangle(x, mean + 1)).sum();
29-
let third: i32 = input.iter().map(|&x| triangle(x, mean - 1)).sum();
30-
first.min(second).min(third)
27+
(-1..=1).map(|delta| input.iter().map(|&x| triangle(x, mean + delta)).sum()).min().unwrap()
3128
}
3229

3330
fn median(input: &[i32]) -> i32 {

src/year2021/day11.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,8 @@ fn simulate(input: &Input, predicate: fn(usize, usize) -> bool) -> (usize, usize
4949
for y in 0..10 {
5050
for x in 0..10 {
5151
let index = 12 * (y + 1) + (x + 1);
52-
53-
if grid[index] < 9 {
54-
grid[index] += 1;
55-
flashed[index] = false;
56-
} else {
57-
grid[index] = 0;
58-
flashed[index] = true;
59-
todo.push(index);
60-
}
52+
flashed[index] = false;
53+
bump_octopus(&mut grid, &mut flashed, &mut todo, index);
6154
}
6255
}
6356

@@ -75,16 +68,8 @@ fn simulate(input: &Input, predicate: fn(usize, usize) -> bool) -> (usize, usize
7568
index - 12,
7669
index - 13,
7770
] {
78-
if flashed[next] {
79-
continue;
80-
}
81-
82-
if grid[next] < 9 {
83-
grid[next] += 1;
84-
} else {
85-
grid[next] = 0;
86-
flashed[next] = true;
87-
todo.push(next);
71+
if !flashed[next] {
72+
bump_octopus(&mut grid, &mut flashed, &mut todo, next);
8873
}
8974
}
9075
}
@@ -95,3 +80,15 @@ fn simulate(input: &Input, predicate: fn(usize, usize) -> bool) -> (usize, usize
9580

9681
(total, steps)
9782
}
83+
84+
/// Increments an octopus's energy. If it reaches 10, it flashes and is added to the queue.
85+
#[inline]
86+
fn bump_octopus(grid: &mut [u8], flashed: &mut [bool], todo: &mut Vec<usize>, index: usize) {
87+
if grid[index] < 9 {
88+
grid[index] += 1;
89+
} else {
90+
grid[index] = 0;
91+
flashed[index] = true;
92+
todo.push(index);
93+
}
94+
}

0 commit comments

Comments
 (0)