Skip to content

Commit d839c58

Browse files
committed
Tidy code
1 parent e74aed3 commit d839c58

17 files changed

Lines changed: 77 additions & 120 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ ref_as_ptr = "warn"
246246
ref_binding_to_reference = "warn"
247247
ref_option = "warn"
248248
ref_option_ref = "warn"
249-
ref_patterns = "allow"
249+
ref_patterns = "warn"
250250
renamed_function_params = "warn"
251251
rest_pat_in_fully_bound_structs = "warn"
252252
return_and_then = "allow"

src/util/iter.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ macro_rules! iterator {
3232

3333
#[inline]
3434
fn next(&mut self) -> Option<Self::Item> {
35-
Some([$({
36-
let $var = self.iter.next()?;
37-
$var
38-
}),+])
35+
$(let $var = self.iter.next()?;)+
36+
Some([$($var),+])
3937
}
4038
}
4139
};

src/util/thread.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! [scoped](https://doc.rust-lang.org/stable/std/thread/fn.scope.html)
33
//! threads equal to the number of cores on the machine. Unlike normal threads, scoped threads
44
//! can borrow data from their environment.
5+
use std::iter::repeat_with;
56
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering::Relaxed};
67
use std::thread::*;
78

@@ -17,13 +18,7 @@ where
1718
R: Send,
1819
{
1920
scope(|scope| {
20-
let mut handles = Vec::new();
21-
22-
for _ in 0..threads() {
23-
let handle = scope.spawn(f);
24-
handles.push(handle);
25-
}
26-
21+
let handles: Vec<_> = repeat_with(|| scope.spawn(f)).take(threads()).collect();
2722
handles.into_iter().flat_map(ScopedJoinHandle::join).collect()
2823
})
2924
}
@@ -54,13 +49,8 @@ where
5449
let workers = workers.as_slice();
5550

5651
scope(|scope| {
57-
let mut handles = Vec::new();
58-
59-
for id in 0..threads {
60-
let handle = scope.spawn(move || f(ParIter { id, items, workers }));
61-
handles.push(handle);
62-
}
63-
52+
let handles: Vec<_> =
53+
(0..threads).map(|id| scope.spawn(move || f(ParIter { id, items, workers }))).collect();
6454
handles.into_iter().flat_map(ScopedJoinHandle::join).collect()
6555
})
6656
}

src/year2016/day19.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,9 @@ pub fn parse(input: &str) -> u32 {
9191
}
9292

9393
pub fn part1(input: &u32) -> u32 {
94-
let mut elf = *input;
95-
96-
elf *= 2;
97-
// Remove highest 1 bit.
98-
elf -= 1 << elf.ilog2();
99-
// Elves use 1-based indexing.
100-
elf += 1;
101-
102-
elf
94+
let n = *input * 2;
95+
// Remove highest 1 bit, then add 1 for one-based indexing.
96+
n - (1 << n.ilog2()) + 1
10397
}
10498

10599
pub fn part2(input: &u32) -> u32 {

src/year2017/day20.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct Vector {
2424

2525
impl Vector {
2626
#[inline]
27-
fn new(cs: [i32; 3]) -> Self {
28-
Vector { x: cs[0], y: cs[1], z: cs[2] }
27+
fn new([x, y, z]: [i32; 3]) -> Self {
28+
Vector { x, y, z }
2929
}
3030

3131
#[inline]
@@ -72,11 +72,11 @@ pub fn parse(input: &str) -> Vec<Particle> {
7272
.chunk::<3>()
7373
.chunk::<3>()
7474
.enumerate()
75-
.map(|(id, cs)| Particle {
75+
.map(|(id, [p, v, a])| Particle {
7676
id,
77-
position: Vector::new(cs[0]),
78-
velocity: Vector::new(cs[1]),
79-
acceleration: Vector::new(cs[2]),
77+
position: Vector::new(p),
78+
velocity: Vector::new(v),
79+
acceleration: Vector::new(a),
8080
})
8181
.collect()
8282
}

src/year2018/day04.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ pub fn parse(input: &str) -> Input {
3131

3232
/// Find the guard with the greatest total minutes asleep.
3333
pub fn part1(input: &Input) -> usize {
34-
choose(input, |(_, m)| m.iter().sum())
34+
choose(input, |m| m.iter().sum())
3535
}
3636

3737
/// Find the guard with the highest single minute asleep.
3838
pub fn part2(input: &Input) -> usize {
39-
choose(input, |(_, m)| *m.iter().max().unwrap())
39+
choose(input, |m| *m.iter().max().unwrap())
4040
}
4141

42-
fn choose(input: &Input, strategy: fn(&(&usize, &[u32; 60])) -> u32) -> usize {
42+
fn choose(input: &Input, strategy: impl Fn(&[u32; 60]) -> u32) -> usize {
4343
// Find the guard using a specific strategy.
44-
let (id, minutes) = input.iter().max_by_key(strategy).unwrap();
44+
let (id, minutes) = input.iter().max_by_key(|(_, m)| strategy(m)).unwrap();
4545
// Find the minute spent asleep the most.
4646
let (minute, _) = minutes.iter().enumerate().max_by_key(|&(_, &m)| m).unwrap();
4747
// Return the result.

src/year2018/day22.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub fn part1(input: &Input) -> i32 {
109109

110110
/// A* search for the shortest path to the target.
111111
pub fn part2(input: &Input) -> i32 {
112-
let &Input { height, width, cave: ref erosion } = input;
113-
let target = Point::new(width, height);
112+
let erosion = &input.cave;
113+
let target = Point::new(input.width, input.height);
114114

115115
// Initialize bucket queue with pre-allocated capacity to reduce reallocations needed.
116116
let mut base = 0;
@@ -122,8 +122,8 @@ pub fn part2(input: &Input) -> i32 {
122122
// we implicitly disallow it during the A* search as the time to reach the square will
123123
// always be greater than zero.
124124
let mut cave = Grid::new(erosion.width, erosion.height, [i32::MAX; 3]);
125-
for (i, level) in erosion.bytes.iter().enumerate() {
126-
cave.bytes[i][*level as usize] = 0;
125+
for (i, &level) in erosion.bytes.iter().enumerate() {
126+
cave.bytes[i][level as usize] = 0;
127127
}
128128

129129
// Start at origin with the torch equipped.

src/year2019/day02.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ type Input = [i32; 3];
1414

1515
pub fn parse(input: &str) -> Input {
1616
let code: Vec<_> = input.iter_unsigned().collect();
17-
18-
let c = check(&code, 0, 0) as i32;
19-
let a = check(&code, 1, 0) as i32;
20-
let b = check(&code, 0, 1) as i32;
17+
let a = check(&code, 1, 0);
18+
let b = check(&code, 0, 1);
19+
let c = check(&code, 0, 0);
2120

2221
[a - c, b - c, c]
2322
}
@@ -35,22 +34,18 @@ pub fn part2([a, b, c]: &Input) -> i32 {
3534
.unwrap()
3635
}
3736

38-
fn check(input: &[usize], first: usize, second: usize) -> usize {
39-
let mut code = input.to_vec();
37+
fn check(code: &[usize], first: usize, second: usize) -> i32 {
38+
let mut pc = 0;
39+
let code = &mut code.to_vec()[..];
40+
4041
code[1] = first;
4142
code[2] = second;
4243

43-
execute(&mut code)
44-
}
45-
46-
fn execute(code: &mut [usize]) -> usize {
47-
let mut pc = 0;
48-
4944
loop {
5045
match code[pc] {
5146
1 => code[code[pc + 3]] = code[code[pc + 1]] + code[code[pc + 2]],
5247
2 => code[code[pc + 3]] = code[code[pc + 1]] * code[code[pc + 2]],
53-
_ => break code[0],
48+
_ => break code[0] as i32,
5449
}
5550
pc += 4;
5651
}

src/year2019/day12.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,20 @@ pub fn part2(input: &Input) -> usize {
4545
let [mut a, mut b, mut c] = [0, 0, 0];
4646
let mut count = 0;
4747

48-
while a * b * c == 0 {
49-
count += 1;
50-
51-
if a == 0 {
52-
x = step(x);
53-
if stopped(x) {
54-
a = count;
55-
}
56-
}
57-
58-
if b == 0 {
59-
y = step(y);
60-
if stopped(y) {
61-
b = count;
48+
let update = |axis: &mut Axis, period: &mut usize, count: usize| {
49+
if *period == 0 {
50+
*axis = step(*axis);
51+
if stopped(*axis) {
52+
*period = count;
6253
}
6354
}
55+
};
6456

65-
if c == 0 {
66-
z = step(z);
67-
if stopped(z) {
68-
c = count;
69-
}
70-
}
57+
while a * b * c == 0 {
58+
count += 1;
59+
update(&mut x, &mut a, count);
60+
update(&mut y, &mut b, count);
61+
update(&mut z, &mut c, count);
7162
}
7263

7364
// a, b and c are the half period, so multiply by 2 to get final result.

src/year2019/day14.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub fn parse(input: &str) -> Vec<Reaction> {
3434
indices.insert("FUEL", 0);
3535
indices.insert("ORE", 1);
3636

37+
let mut lookup = |s| {
38+
let size = indices.len();
39+
*indices.entry(s).or_insert(size)
40+
};
41+
3742
for line in lines {
3843
let mut tokens = line
3944
.split(|c: char| !c.is_ascii_alphanumeric())
@@ -43,18 +48,15 @@ pub fn parse(input: &str) -> Vec<Reaction> {
4348

4449
// Assigns other indices in the arbitrary order that chemicals are encountered.
4550
let [kind, amount] = tokens.next().unwrap();
46-
let size = indices.len();
47-
let chemical = *indices.entry(kind).or_insert(size);
51+
let chemical = lookup(kind);
4852

4953
let reaction = &mut reactions[chemical];
5054
reaction.amount = amount.unsigned();
5155
reaction.chemical = chemical;
5256

5357
for [kind, amount] in tokens {
54-
let amount = amount.unsigned();
55-
let size = indices.len();
56-
let chemical = *indices.entry(kind).or_insert(size);
57-
reaction.ingredients.push(Ingredient { amount, chemical });
58+
let chemical = lookup(kind);
59+
reaction.ingredients.push(Ingredient { amount: amount.unsigned(), chemical });
5860
}
5961
}
6062

0 commit comments

Comments
 (0)