mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Always walk the short way around the circle
This commit is contained in:
@@ -23,6 +23,16 @@ fn step(steps: &[usize], mut start: usize, count: usize) -> usize {
|
|||||||
start
|
start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// While looping around to find a spot to move to, you must ignore the piece itself, but for
|
||||||
|
// computing the answer, you shouldn't. This function does the latter.
|
||||||
|
fn step_inclusive(steps: &[usize], mut start: usize, count: usize) -> usize {
|
||||||
|
for _ in 0..(count % steps.len()) {
|
||||||
|
start = steps[start];
|
||||||
|
}
|
||||||
|
|
||||||
|
start
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn print(encrypted: &[i64], next: &[usize]) {
|
fn print(encrypted: &[i64], next: &[usize]) {
|
||||||
let mut base = String::new();
|
let mut base = String::new();
|
||||||
@@ -75,8 +85,19 @@ fn shuffle(encrypted: &[i64], times: usize) -> Result<String> {
|
|||||||
.chain(0..(encrypted.len() - 1))
|
.chain(0..(encrypted.len() - 1))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let len = encrypted.len() as i64;
|
||||||
|
let half_len = len / 2;
|
||||||
|
|
||||||
for _ in 0..times {
|
for _ in 0..times {
|
||||||
for (i, &value) in encrypted.iter().enumerate() {
|
for (i, &value) in encrypted.iter().enumerate() {
|
||||||
|
let mut value = value % (len - 1);
|
||||||
|
|
||||||
|
if value < -half_len {
|
||||||
|
value += len - 1;
|
||||||
|
} else if value > half_len {
|
||||||
|
value -= len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
match value.cmp(&0) {
|
match value.cmp(&0) {
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
let before = step(&prev, i, (-value) as usize);
|
let before = step(&prev, i, (-value) as usize);
|
||||||
@@ -105,7 +126,7 @@ fn shuffle(encrypted: &[i64], times: usize) -> Result<String> {
|
|||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
|
||||||
for _ in 0..3 {
|
for _ in 0..3 {
|
||||||
start = step(&next, start, 1000);
|
start = step_inclusive(&next, start, 1000);
|
||||||
sum += encrypted[start];
|
sum += encrypted[start];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,4 +153,9 @@ mod tests {
|
|||||||
fn sample_part1() {
|
fn sample_part1() {
|
||||||
assert_eq!(part1(SAMPLE).unwrap(), "3");
|
assert_eq!(part1(SAMPLE).unwrap(), "3");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
assert_eq!(part2(SAMPLE).unwrap(), "1623178306");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user