Skip to content

Commit f2c6034

Browse files
committed
Simplify code
1 parent 3f5197f commit f2c6034

9 files changed

Lines changed: 23 additions & 41 deletions

File tree

benches/benchmark.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ macro_rules! benchmark {
1313
use test::Bencher;
1414

1515
static DATA: LazyLock<String> = LazyLock::new(|| {
16-
let year = stringify!($year);
17-
let day = stringify!($day);
18-
let path = format!("input/{year}/{day}.txt");
19-
20-
read_to_string(&path).unwrap_or_else(|_| {
21-
panic!("Missing input file {BOLD}{WHITE}{path}{RESET}");
22-
})
16+
let path = format!("input/{}/{}.txt", stringify!($year), stringify!($day));
17+
read_to_string(&path)
18+
.unwrap_or_else(|_| panic!("Missing input file {BOLD}{WHITE}{path}{RESET}"))
2319
});
2420

2521
#[bench]

src/util/grid.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ impl<T: Copy + PartialEq> Grid<T> {
6464
#[must_use]
6565
pub fn find(&self, needle: T) -> Option<Point> {
6666
self.bytes.iter().position(|&h| h == needle).map(|index| {
67-
let x = (index as i32) % self.width;
68-
let y = (index as i32) / self.width;
69-
Point::new(x, y)
67+
let index = index as i32;
68+
Point::new(index % self.width, index / self.width)
7069
})
7170
}
7271
}

src/year2016/day01.rs

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

3333
// Return the point of intersection between two orthogonal segments, if there is one.
3434
fn intersects(&self, other: &Segment) -> Option<Point> {
35-
let overlap =
36-
!(other.x2 < self.x1 || other.x1 > self.x2 || other.y2 < self.y1 || other.y1 > self.y2);
35+
let overlap = other.x2 >= self.x1
36+
&& other.x1 <= self.x2
37+
&& other.y2 >= self.y1
38+
&& other.y1 <= self.y2;
3739
overlap.then_some(Point::new(self.x1.max(other.x1), self.y1.max(other.y1)))
3840
}
3941
}

src/year2016/day04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn parse(input: &str) -> Vec<Room<'_>> {
3333
if b != b'-' {
3434
let index = to_index(b);
3535
let current = freq[index];
36-
let next = freq[index] + 1;
36+
let next = current + 1;
3737

3838
freq[index] = next;
3939
fof[current] -= 1;

src/year2017/day07.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use std::collections::VecDeque;
1515

1616
#[derive(Clone, Copy, Default)]
1717
struct Node {
18-
has_parent: bool,
19-
parent: usize,
18+
parent: Option<usize>,
2019
children: usize,
2120
processed: usize,
2221
weight: i32,
@@ -49,8 +48,7 @@ pub fn parse(input: &str) -> Input<'_> {
4948
for edge in iter {
5049
nodes[i].children += 1;
5150
let child = indices[edge];
52-
nodes[child].parent = i;
53-
nodes[child].has_parent = true;
51+
nodes[child].parent = Some(i);
5452
}
5553

5654
// Start with leaf nodes.
@@ -62,14 +60,15 @@ pub fn parse(input: &str) -> Input<'_> {
6260
// The root is the only node without a parent. Start from any node, and walk up the
6361
// tree until finding the root.
6462
let mut candidate = 0;
65-
while nodes[candidate].has_parent {
66-
candidate = nodes[candidate].parent;
63+
while let Some(parent) = nodes[candidate].parent {
64+
candidate = parent;
6765
}
6866
let part_one = pairs[candidate].0;
6967
let mut part_two = 0;
7068

7169
while let Some(index) = todo.pop_front() {
7270
let Node { parent, weight, total, .. } = nodes[index];
71+
let parent = parent.unwrap();
7372
let node = &mut nodes[parent];
7473

7574
if node.processed < 2 {

src/year2021/day13.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,9 @@ pub fn part2(input: &Input) -> String {
7474

7575
#[inline]
7676
fn apply_fold(fold: Fold, point: Point) -> Point {
77-
let horizontal = |p: Point, x: i32| if p.x < x { p } else { Point::new(2 * x - p.x, p.y) };
78-
let vertical = |p: Point, y: i32| if p.y < y { p } else { Point::new(p.x, 2 * y - p.y) };
79-
8077
match fold {
81-
// Fold point at `x` coordinate, doing nothing if the point is to the left of the fold line.
82-
Fold::Horizontal(x) => horizontal(point, x),
83-
// Fold point at `y` coordinate, doing nothing if the point is above the fold line.
84-
Fold::Vertical(y) => vertical(point, y),
78+
Fold::Horizontal(x) if point.x >= x => Point::new(2 * x - point.x, point.y),
79+
Fold::Vertical(y) if point.y >= y => Point::new(point.x, 2 * y - point.y),
80+
_ => point,
8581
}
8682
}

src/year2021/day22.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub struct RebootStep {
3838

3939
impl RebootStep {
4040
fn from((command, points): (&str, [i32; 6])) -> RebootStep {
41-
let on = command == "on";
42-
let cube = Cube::from(points);
43-
RebootStep { on, cube }
41+
RebootStep { on: command == "on", cube: Cube::from(points) }
4442
}
4543
}
4644

src/year2022/day17.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! We choose an arbitrary length and generate a sequence of that size then search
3636
//! for repeating patterns. Once we find the length of the cycle then we can extrapolate for
3737
//! any `n` greater than the start of the cycle.
38-
use std::iter::{Copied, Cycle};
38+
use std::iter::{Copied, Cycle, once};
3939
use std::slice::Iter;
4040

4141
/// Convenience alias to shorten type name.
@@ -135,14 +135,8 @@ pub fn part2(input: &[u8]) -> usize {
135135
let guess = 1000;
136136
let height: Vec<_> = State::new(input).take(5 * guess).collect();
137137
// We compare based on the *delta* between rows instead of absolute heights.
138-
let deltas: Vec<_> = height
139-
.iter()
140-
.scan(0, |state, &height| {
141-
let delta = height - *state;
142-
*state = height;
143-
Some(delta)
144-
})
145-
.collect();
138+
let deltas: Vec<_> =
139+
once(height[0]).chain(height.array_windows().map(|[a, b]| b - a)).collect();
146140

147141
// Simple brute force check, instead of a
148142
// [cycle detection](https://en.wikipedia.org/wiki/Cycle_detection) algorithm.

src/year2025/day09.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ pub fn part2(tiles: &[Tile]) -> u64 {
142142
let mut intervals_from_descending_edges = vec![];
143143

144144
// Invariants on the input data (defined by the puzzle) result in points arriving in pairs on the same y line:
145-
let mut it = tiles.iter();
146-
147-
while let (Some(&[x0, y]), Some(&[x1, y1])) = (it.next(), it.next()) {
145+
for [&[x0, y], &[x1, y1]] in tiles.iter().chunk::<2>() {
148146
debug_assert_eq!(y, y1);
149147

150148
// Update the descending edges; since we are scanning from top to bottom, and within each line left to right,

0 commit comments

Comments
 (0)