Skip to main content

aoc/year2017/
day03.rs

1//! # Spiral Memory
2//!
3//! ## Part One
4//!
5//! Consider the layout as a sequence of hollow donuts. We find the donut that contains the value
6//! which gives one component of the Manhattan value. The second component is the distance from
7//! the center of each edge.
8//!
9//! For example, say the target value is 20. We find the donut then subtract the inner donuts
10//! to make the values relative then calculate the values modulo the edge size.
11//!
12//! ```none
13//!                                                  <------------
14//! 17  16  15  14  13      7   6   5   4   3        3   2  [1]  0   3  ^
15//! 18   5   4   3  12      8               2     |  0               2  |
16//! 19   6   1   2  11 =>   9               1 =>  | [1]             [1] |
17//! 20   7   8   9  10     10               0     |  2               0  |
18//! 21  22  23  24  25     11  12  13  14  15     v  3   0  [1]  2   3
19//!                                                      ------------>
20//! ```
21//!
22//! The first component is the horizontal or vertical distance from the center to the ring,
23//! in this case 2 steps. The second component is the distance from the target number to the
24//! center of each edge, in this case 2 - 1 = 1.
25//!
26//! ## Part Two
27//!
28//! We use the [`Point`] utility to move in the spiral direction. Values are stored in a hashmap
29//! defaulting to zero if the value doesn't exist yet.
30//!
31//! Note that [OEIS A141481](https://oeis.org/A141481/b141481.txt) shows the sequence well
32//! past the limit requested by the puzzle. However, this is fast enough to solve to not
33//! be worth turning this into a lookup table.
34use crate::util::hash::*;
35use crate::util::parse::*;
36use crate::util::point::*;
37
38pub fn parse(input: &str) -> u32 {
39    input.unsigned()
40}
41
42pub fn part1(input: &u32) -> u32 {
43    let target = *input;
44    // Find the donut that contains the value, the smallest odd width at least sqrt(target).
45    let a = (target - 1).isqrt().next_multiple_of(2) + 1;
46    let b = a - 1;
47    let c = a - 2;
48
49    // Distance to donut plus distance to center of edge.
50    (b / 2) + (c / 2).abs_diff((target - c * c - 1) % b)
51}
52
53pub fn part2(input: &u32) -> u32 {
54    let target = *input;
55    let mut values = FastMap::build([(ORIGIN, 1)]);
56    let mut position = ORIGIN;
57    let mut direction = RIGHT;
58
59    loop {
60        position += direction;
61
62        let next = DIAGONAL.iter().filter_map(|&d| values.get(&(position + d))).sum();
63
64        if next > target {
65            return next;
66        }
67        values.insert(position, next);
68
69        // Turn left whenever the square on that side is still empty.
70        let left = direction.counter_clockwise();
71        if !values.contains_key(&(position + left)) {
72            direction = left;
73        }
74    }
75}