Skip to content

Commit 42e71de

Browse files
committed
Tidy code
1 parent 6dc0804 commit 42e71de

File tree

8 files changed

+47
-39
lines changed

8 files changed

+47
-39
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
macro_rules! library {
1414
($year:tt $description:literal $($day:tt),*) => {
1515
#[doc = concat!("# ", $description)]
16-
pub mod $year {$(pub mod $day;)*}
16+
pub mod $year {
17+
$(pub mod $day;)*
18+
}
1719
}
1820
}
1921

src/main.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@ use aoc::util::parse::*;
33
use aoc::*;
44
use std::env::args;
55
use std::fs::read_to_string;
6-
use std::iter::empty;
76
use std::time::{Duration, Instant};
87

98
fn main() {
109
// Parse command line options
11-
let args: Vec<_> = args().collect();
12-
let args: Vec<_> = args.iter().map(String::as_str).collect();
13-
let mut iter = args.iter().flat_map(|arg| arg.iter_unsigned::<u32>());
10+
let mut iter = args().flat_map(|arg| arg.as_str().iter_unsigned().collect::<Vec<u32>>());
1411
let (year, day) = (iter.next(), iter.next());
1512

13+
let solutions = [
14+
year2015(),
15+
year2016(),
16+
year2017(),
17+
year2018(),
18+
year2019(),
19+
year2020(),
20+
year2021(),
21+
year2022(),
22+
year2023(),
23+
year2024(),
24+
];
25+
1626
// Filter solutions then pretty print output.
17-
let (stars, duration) = empty()
18-
.chain(year2015())
19-
.chain(year2016())
20-
.chain(year2017())
21-
.chain(year2018())
22-
.chain(year2019())
23-
.chain(year2020())
24-
.chain(year2021())
25-
.chain(year2022())
26-
.chain(year2023())
27-
.chain(year2024())
28-
.filter(|solution| year.is_none_or(|y| y == solution.year))
29-
.filter(|solution| day.is_none_or(|d| d == solution.day))
27+
let (stars, duration) = solutions
28+
.into_iter()
29+
.flatten()
30+
.filter(|s| year.is_none_or(|y| y == s.year))
31+
.filter(|s| day.is_none_or(|d| d == s.day))
3032
.fold((0, Duration::ZERO), |(stars, duration), Solution { year, day, wrapper }| {
3133
let path = format!("input/year{year}/day{day:02}.txt");
3234

@@ -50,7 +52,7 @@ fn main() {
5052
});
5153

5254
// Optionally print totals.
53-
if args.contains(&"--totals") {
55+
if args().any(|arg| arg == "--totals") {
5456
println!("{BOLD}{YELLOW}⭐ {}{RESET}", stars);
5557
println!("{BOLD}{WHITE}🕓 {} ms{RESET}", duration.as_millis());
5658
}

src/year2016/day06.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub fn parse(input: &str) -> Input {
1818
input.iter().skip(offset).step_by(stride).for_each(|&b| freq[to_index(b)] += 1);
1919

2020
let (max, _) =
21-
freq.iter().enumerate().filter(|(_, f)| **f > 0).max_by_key(|(_, f)| **f).unwrap();
21+
freq.iter().enumerate().filter(|&(_, &f)| f > 0).max_by_key(|&(_, &f)| f).unwrap();
2222
let (min, _) =
23-
freq.iter().enumerate().filter(|(_, f)| **f > 0).min_by_key(|(_, f)| **f).unwrap();
23+
freq.iter().enumerate().filter(|&(_, &f)| f > 0).min_by_key(|&(_, &f)| f).unwrap();
2424

2525
(to_char(max), to_char(min))
2626
})

src/year2017/day07.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn parse(input: &str) -> Input<'_> {
6060
}
6161

6262
// The root is the only node without a parent.
63-
let part_one = indices.iter().find(|(_, v)| !nodes[**v].has_parent).unwrap().0;
63+
let part_one = indices.iter().find(|&(_, &v)| !nodes[v].has_parent).unwrap().0;
6464
let mut part_two = 0;
6565

6666
while let Some(index) = todo.pop_front() {

src/year2018/day04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn choose(input: &Input, strategy: fn(&(&usize, &[u32; 60])) -> u32) -> usize {
4343
// Find the guard using a specific strategy.
4444
let (id, minutes) = input.iter().max_by_key(strategy).unwrap();
4545
// Find the minute spent asleep the most
46-
let (minute, _) = minutes.iter().enumerate().max_by_key(|(_, m)| **m).unwrap();
46+
let (minute, _) = minutes.iter().enumerate().max_by_key(|&(_, &m)| m).unwrap();
4747
// Return result
4848
id * minute
4949
}

src/year2023/day16.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl BitSet {
5858
}
5959

6060
fn union(&mut self, other: &BitSet) {
61-
self.bits.iter_mut().zip(other.bits.iter()).for_each(|(a, b)| *a |= b);
61+
self.bits.iter_mut().zip(&other.bits).for_each(|(a, b)| *a |= b);
6262
}
6363

6464
fn size(&self) -> u32 {
@@ -236,8 +236,8 @@ fn strong_connect(graph: &mut Graph, position: Point) -> usize {
236236
{
237237
let other = &graph.nodes[next];
238238
node.tiles.union(&other.tiles);
239-
node.from.extend(other.from.iter());
240-
node.to.extend(other.to.iter());
239+
node.from.extend(&other.from);
240+
node.to.extend(&other.to);
241241
}
242242

243243
// Mark node as done.

src/year2023/day23.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ pub fn part1(input: &Input) -> u32 {
209209

210210
for y in 0..6 {
211211
for x in 0..6 {
212-
let left = if x == 0 { 0 } else { total[y][x - 1] + input.horizontal[y][x - 1] };
213-
let above = if y == 0 { 0 } else { total[y - 1][x] + input.vertical[y - 1][x] };
212+
let left = if x > 0 { total[y][x - 1] + input.horizontal[y][x - 1] } else { 0 };
213+
let above = if y > 0 { total[y - 1][x] + input.vertical[y - 1][x] } else { 0 };
214214
total[y][x] = left.max(above);
215215
}
216216
}
@@ -255,15 +255,17 @@ pub fn part2(input: &Input) -> u32 {
255255
for ((row, gap), steps) in current.drain() {
256256
for &(next_row, next_gap, horizontal, vertical) in &graph[&row] {
257257
// Only 1 gap total is allowed, otherwise we can make a longer path.
258-
if !(gap && next_gap) {
259-
// The bit sets represent the horizonal and vertical moves from the
260-
// previous row.
261-
let extra = horizontal.biterator().map(|x| input.horizontal[y][x]).sum::<u32>()
262-
+ vertical.biterator().map(|x| input.vertical[y][x]).sum::<u32>();
263-
264-
let e = next.entry((next_row, gap || next_gap)).or_insert(0);
265-
*e = (*e).max(steps + extra);
258+
if gap && next_gap {
259+
continue;
266260
}
261+
262+
// The bit sets represent the horizontal and vertical moves from the previous row.
263+
let extra = horizontal.biterator().map(|x| input.horizontal[y][x]).sum::<u32>()
264+
+ vertical.biterator().map(|x| input.vertical[y][x]).sum::<u32>();
265+
266+
// De-duplicate states so that each row has at most 76 states.
267+
let e = next.entry((next_row, gap || next_gap)).or_insert(0);
268+
*e = (*e).max(steps + extra);
267269
}
268270
}
269271

@@ -274,7 +276,7 @@ pub fn part2(input: &Input) -> u32 {
274276
input.extra + current[&(end, true)]
275277
}
276278

277-
/// Convert maze to unidrected graph.
279+
/// Convert maze to undirected graph.
278280
fn compress(input: &str) -> Graph {
279281
let mut grid = Grid::parse(input);
280282
let width = grid.width;
@@ -390,7 +392,7 @@ fn graph_to_grid(graph: &Graph) -> Input {
390392

391393
let (&next, _) = edges
392394
.iter()
393-
.find(|(k, v)| v.contains(&above) && v.contains(&left) && seen.insert(**k))
395+
.find(|&(&k, v)| v.contains(&above) && v.contains(&left) && seen.insert(k))
394396
.unwrap();
395397

396398
grid[y][x] = next;

tests/test.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
macro_rules! test {
3232
($year:tt $($day:tt),*) => {
33-
pub mod $year {$(pub mod $day;)*}
33+
pub mod $year {
34+
$(pub mod $day;)*
35+
}
3436
}
3537
}
3638

0 commit comments

Comments
 (0)