NickyMeulemanNime
Metadata
  • Date

  • By

    • Nicky Meuleman
  • Tagged

  • Part of series

  • Older post

    Advent of Code 2022 Day 19

  • Newer post

    Advent of Code 2022 Day 21

Table of contents
  1. Day 20: Grove Positioning System
  2. Parsing
  3. Part 1
    1. Final code
  4. Part 2
    1. Final code
  5. Final code

Advent of Code 2022 Day 20

Day 20: Grove Positioning System

https://adventofcode.com/2022/day/20

Remember we were going to a grove on day 1?

You decide to meet up with the elves there. Your device has the grove’s coordinates, but they are encrypted.

Your puzzle input countains the encryped coordinates.

It’s a bunch of numbers in a circular list.

To decrypt it, mix it.

Mixing: For each number in the list, move it back or forwards a number of positions equal to the value of that number.

  • 5 moves forwards 5 positions
  • -10 moves backwards 10 positions

Remember, it’s a circular list, so moving off one end means appearing at the other.

An example input looks like this:

input.txt
1
2
-3
3
-2
0
4

The moving operations happen in the order of the original list.

In this example:

  • the original list: 1, 2, -3, 3, -2, 0, 4

  • 1 moves 1 position to the right: 2, 1, -3, 3, -2, 0, 4

  • 2 moves 2 positions to the right: 1, -3, 2, 3, -2, 0, 4

  • -3 moves 3 positions to the left: 1, 2, 3, -2, -3, 0, 4

  • 4 moves 4 positions to the right: 1, 2, -3, 4, 0, 3, -2

The sneaky thing here is that the real input has duplicate numbers. We should treat every number like it’s unique.

Parsing

Probably doesn’t need to be a seperate function, but I’ve been applying this pattern for a couple of days now. It works, I’m sticking with it.

Parse the input into a list of numbers.

day_20.rs
fn parse() -> Vec<i64> {
let input = std::fs::read_to_string("src/day20.txt").unwrap();
input.lines().map(|line| line.parse().unwrap()).collect()
}

Part 1

The grove coordinates can be found by looking at the 1000th, 2000th, and 3000th numbers after the value 0, wrapping around the list as necessary.

The question asks what the sum of the 3 grove coordinates is.

So after the *mixing process, we get a sick beat the position of the 0 in the mixed list of numbers.

Keep the original list of numbers seperately.

Create a mixed list of indexes to the corresponding number in the original list. This is the key to treating every number in the list as a unique number even if there are repeats.

This is the list that will be operated on during the mixing process.

We loop over every index/number combo in the original list, and apply the mixing step for each one.

We first find the index in mixed where the current number’s index into nums is.

Yes, lots of “index” today. It’s a bit mindbendy.

  • nums holds the original numbers

  • mixed holds indexes to a number in the nums list

  • Remove the item at the found index in the mixed list

  • Add the current num to that index

  • Insert the item back into mixed at that new index, wrapping around the list if necessary.

After that loop is done and the mixing is over, find the index of the 0 in the original list. The item in mixed with that index represents the 0.

Then, apply the 1000, 2000, and 3000 offset to that number in the mixed list. Remember, mixed holds indexes into nums. So the number we actually want is in nums, indexed by whatever we just found by applying the offset to a number in mixed.

Finish by summing those numbers.

Final code

day_20.rs
pub fn part_1() -> i64 {
let nums = parse();
// indexes into nums
let mut mixed: Vec<_> = (0..nums.len()).collect();
for (idx, &num) in nums.iter().enumerate() {
// find mixed that corresponds to the number in nums
let mixed_idx = mixed.iter().position(|&mix_num| mix_num == idx).unwrap();
// remove that index from mixed
mixed.remove(mixed_idx);
// add num offset to that number and add it back
let new_mixed_idx = (mixed_idx as i64 + num).rem_euclid(mixed.len() as i64) as usize;
mixed.insert(new_mixed_idx, idx);
}
let zero_idx = nums.iter().position(|&num| num == 0).unwrap();
let zero_mixed_idx = mixed
.iter()
.position(|&mix_num| mix_num == zero_idx)
.unwrap();
[1000, 2000, 3000]
.iter()
.map(|offset| {
let mixed_idx = (zero_mixed_idx + offset) % mixed.len();
let nums_idx = mixed[mixed_idx];
nums[nums_idx]
})
.sum()
}

Part 2

First, every number in the input has to be multiplied by an encryption key, 811589153.

let encryption_key = 811_589_153;

Only then can the mixing begin. And that mixing process has to do 10 full loops on those encrypted numbers.

The question asks what the sum of the 3 grove coordinates is again.

As a reminder, the grove coordinates can be found by looking at the 1000th, 2000th, and 3000th numbers after the value 0 in the mixed list.

It’s a day where making the naive changes is enough, no exploding complexity today.

Final code

day_20.rs
pub fn part_2() -> i64 {
let decryption_key = 811_589_153;
let nums: Vec<_> = parse().iter().map(|num| num * decryption_key).collect();
// indexes into nums
let mut mixed: Vec<_> = (0..nums.len()).collect();
for _ in 0..10 {
for (idx, &num) in nums.iter().enumerate() {
// find mixed that corresponds to the number in nums
let mixed_idx = mixed.iter().position(|&mix_num| mix_num == idx).unwrap();
// remove that index from mixed
mixed.remove(mixed_idx);
// add num offset to that number and add it back
let new_mixed_idx = (mixed_idx as i64 + num).rem_euclid(mixed.len() as i64) as usize;
mixed.insert(new_mixed_idx, idx);
}
}
let zero_idx = nums.iter().position(|&num| num == 0).unwrap();
let zero_mixed_idx = mixed
.iter()
.position(|&mix_num| mix_num == zero_idx)
.unwrap();
[1000, 2000, 3000]
.iter()
.map(|offset| {
let mixed_idx = (zero_mixed_idx + offset) % mixed.len();
let nums_idx = mixed[mixed_idx];
nums[nums_idx]
})
.sum()
}

Final code

day_20.rs
1fn parse() -> Vec<i64> {
2 let input = std::fs::read_to_string("src/day20.txt").unwrap();
3 input.lines().map(|line| line.parse().unwrap()).collect()
4}
5
6pub fn part_1() -> i64 {
7 let nums = parse();
8 // indexes into nums
9 let mut mixed: Vec<_> = (0..nums.len()).collect();
10 for (idx, &num) in nums.iter().enumerate() {
11 // find mixed that corresponds to the number in nums
12 let mixed_idx = mixed.iter().position(|&mix_num| mix_num == idx).unwrap();
13 // remove that index from mixed
14 mixed.remove(mixed_idx);
15 // add num offset to that number and add it back
16 let new_mixed_idx = (mixed_idx as i64 + num).rem_euclid(mixed.len() as i64) as usize;
17 mixed.insert(new_mixed_idx, idx);
18 }
19
20 let zero_idx = nums.iter().position(|&num| num == 0).unwrap();
21 let zero_mixed_idx = mixed
22 .iter()
23 .position(|&mix_num| mix_num == zero_idx)
24 .unwrap();
25
26 [1000, 2000, 3000]
27 .iter()
28 .map(|offset| {
29 let mixed_idx = (zero_mixed_idx + offset) % mixed.len();
30 let nums_idx = mixed[mixed_idx];
31 nums[nums_idx]
32 })
33 .sum()
34}
35
36pub fn part_2() -> i64 {
37 let decryption_key = 811_589_153;
38 let nums: Vec<_> = parse().iter().map(|num| num * decryption_key).collect();
39 // indexes into nums
40 let mut mixed: Vec<_> = (0..nums.len()).collect();
41 for _ in 0..10 {
42 for (idx, &num) in nums.iter().enumerate() {
43 // find mixed that corresponds to the number in nums
44 let mixed_idx = mixed.iter().position(|&mix_num| mix_num == idx).unwrap();
45 // remove that index from mixed
46 mixed.remove(mixed_idx);
47 // add num offset to that number and add it back
48 let new_mixed_idx = (mixed_idx as i64 + num).rem_euclid(mixed.len() as i64) as usize;
49 mixed.insert(new_mixed_idx, idx);
50 }
51 }
52
53 let zero_idx = nums.iter().position(|&num| num == 0).unwrap();
54 let zero_mixed_idx = mixed
55 .iter()
56 .position(|&mix_num| mix_num == zero_idx)
57 .unwrap();
58
59 [1000, 2000, 3000]
60 .iter()
61 .map(|offset| {
62 let mixed_idx = (zero_mixed_idx + offset) % mixed.len();
63 let nums_idx = mixed[mixed_idx];
64 nums[nums_idx]
65 })
66 .sum()
67}

Series navigation for: Advent of Code 2022

1. Advent of Code 2022 Day 1

Designed and developed by Nicky Meuleman

Built with Gatsby. Hosted on Netlify.