Skip to content

Commit 09839f5

Browse files
committed
Tidy 2016
1 parent 9337b5f commit 09839f5

5 files changed

Lines changed: 25 additions & 47 deletions

File tree

src/year2016/day06.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Signals and Noise
22
//!
3-
//! The cardinality of uppercase letters is only 26 so we can use a fixed-size array to
3+
//! The cardinality of lowercase letters is only 26 so we can use a fixed-size array to
44
//! count the frequency of each character efficiently.
55
type Input = (String, String);
66

src/year2016/day09.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! # Explosives in Cyberspace
22
//!
33
//! The only difference between part one and two is that we recursively decompress inner sequences.
4-
use crate::util::parse::*;
5-
64
pub fn parse(input: &str) -> &[u8] {
75
input.trim().as_bytes()
86
}
@@ -35,15 +33,8 @@ fn decompress(mut slice: &[u8], part_two: bool) -> usize {
3533
}
3634

3735
fn number(slice: &[u8]) -> (&[u8], usize) {
38-
// Parse number digit by digit.
39-
let mut index = 0;
40-
let mut acc = 0;
41-
42-
while slice[index].is_ascii_digit() {
43-
acc = 10 * acc + slice[index].to_decimal() as usize;
44-
index += 1;
45-
}
46-
36+
let end = slice.iter().position(|b| !b.is_ascii_digit()).unwrap();
37+
let n = slice[..end].iter().fold(0, |n, &b| 10 * n + (b - b'0') as usize);
4738
// Skip over trailing delimiter.
48-
(&slice[index + 1..], acc)
39+
(&slice[end + 1..], n)
4940
}

src/year2016/day11.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ pub fn parse(input: &str) -> u32 {
118118
if non_empty && pair == PAIR1 {
119119
steps += 12;
120120
} else {
121-
if (pair & PAIR1) != 0 {
122-
non_empty = true;
123-
}
121+
non_empty |= pair & PAIR1 != 0;
124122
floors[i] = pair;
125123
i += 1;
126124
}
@@ -155,6 +153,16 @@ fn bfs(start: State, steps: u32) -> u32 {
155153

156154
// Iterate over items that can be moved.
157155
let items = state.pairs & (FLOOR1 << state.elevator);
156+
let mut push = |up: bool, mask: u64| -> bool {
157+
if let Some(next) = state.move_floor(up, mask)
158+
&& seen.insert(next)
159+
{
160+
todo.push_back((next, steps + 1));
161+
true
162+
} else {
163+
false
164+
}
165+
};
158166

159167
// When moving down, try one item first; try two only if one didn't work.
160168
// Don't move down from bottom floor, or down into empty region.
@@ -165,22 +173,12 @@ fn bfs(start: State, steps: u32) -> u32 {
165173
let mut added = false;
166174

167175
for i in items.biterator() {
168-
if let Some(next) = state.move_floor(false, 1 << i)
169-
&& seen.insert(next)
170-
{
171-
added = true;
172-
todo.push_back((next, steps + 1));
173-
}
176+
added |= push(false, 1 << i);
174177
}
175-
176178
if !added {
177179
for i in items.biterator() {
178180
for j in items.biterator().take_while(|&j| j < i) {
179-
if let Some(next) = state.move_floor(false, (1 << i) | (1 << j))
180-
&& seen.insert(next)
181-
{
182-
todo.push_back((next, steps + 1));
183-
}
181+
push(false, (1 << i) | (1 << j));
184182
}
185183
}
186184
}
@@ -193,22 +191,12 @@ fn bfs(start: State, steps: u32) -> u32 {
193191

194192
for i in items.biterator() {
195193
for j in items.biterator().take_while(|&j| j < i) {
196-
if let Some(next) = state.move_floor(true, (1 << i) | (1 << j))
197-
&& seen.insert(next)
198-
{
199-
added = true;
200-
todo.push_back((next, steps + 1));
201-
}
194+
added |= push(true, (1 << i) | (1 << j));
202195
}
203196
}
204-
205197
if !added {
206198
for i in items.biterator() {
207-
if let Some(next) = state.move_floor(true, 1 << i)
208-
&& seen.insert(next)
209-
{
210-
todo.push_back((next, steps + 1));
211-
}
199+
push(true, 1 << i);
212200
}
213201
}
214202
}

src/year2016/day15.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ pub fn parse(input: &str) -> Pair {
2020
[part1, part2]
2121
}
2222

23-
pub fn part1(results: &Pair) -> usize {
24-
results[0]
23+
pub fn part1(input: &Pair) -> usize {
24+
input[0]
2525
}
2626

27-
pub fn part2(results: &Pair) -> usize {
28-
results[1]
27+
pub fn part2(input: &Pair) -> usize {
28+
input[1]
2929
}
3030

3131
fn solve(discs: &[Pair], offset: usize, mut time: usize, mut step: usize) -> (usize, usize) {

src/year2016/day16.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
//! showing how to utilize that approach. However, the logarithmic solution shown here is
4242
//! fast enough to not need to worry about askalski's comment "I have no idea why it works,
4343
//! only that it does work."
44-
use crate::util::parse::*;
4544
4645
/// Build a prefix sum of the number of ones at each length in the pattern
4746
/// including zero at the start.
@@ -50,7 +49,7 @@ pub fn parse(input: &str) -> Vec<usize> {
5049
let mut ones = vec![0];
5150

5251
for b in input.trim().bytes() {
53-
sum += b.to_decimal() as usize;
52+
sum += (b & 1) as usize;
5453
ones.push(sum);
5554
}
5655

@@ -71,7 +70,7 @@ pub fn part2(input: &[usize]) -> String {
7170
/// ones in each interval to give the checksum.
7271
pub fn checksum(input: &[usize], disk_size: usize) -> String {
7372
// Determine how many blocks and how big each one is, by lowest 1-bit in disk_size.
74-
let step_size = disk_size & (!disk_size + 1);
73+
let step_size = 1 << disk_size.trailing_zeros();
7574
let blocks = disk_size / step_size;
7675

7776
let counts: Vec<_> = (0..blocks + 1).map(|i| count(input, i * step_size)).collect();

0 commit comments

Comments
 (0)