AoC 2017 Day 13: Firewall Puncher

Source: Packet Scanners

Part 1: Multiple layers are defined with rules of the form:

  • {index}: {depth}

Each layer will start at position 0, then once per tick will advance towards depth. Once it hits depth-1, it will return to position 0, taking 2*depth-1 per full cycle.

Calculate the sum of index * depth for any scanners that are at position 0 when you pass through them given an initial starting time.

read more...


AoC 2017 Day 12: Gridlock

Source: Digital Plumber

Part 1: A network of nodes is defined by a list of lines formatted as such:

2 <-> 0, 3, 4


> In this case, node `2` is connected to `0`, `3`, and `4` and vice versa.

> How many nodes are in the group that contains the node `0`?





First, load the data into an [[wiki:adjacency map]]():

```python
nodes = set()
neighbors = collections.defaultdict(set)

for line in lib.input():
    source, destinations = line.split('<->')
    source = int(source.strip())
    nodes.add(source)

    for destination in destinations.strip().split(','):
        destination = int(destination.strip())
        nodes.add(destination)

        neighbors[source].add(destination)
        neighbors[destination].add(source)

Then, write a function that can take a node and recursively expand until it finds all nodes in the same group:

read more...


AoC 2017 Day 11: It's Full Of Hexagons

Source: Hex Ed1

Part 1: Work on a hex grid:

\ n / nw +–+ ne /
-+ +- \ / sw +–+ se / s \


> Given a series of steps (`n`, `se`, `ne`) etc, how many steps away from the origin do you end up?





This problem mostly comes down to representation. [This post from Red Blob games](https://www.redblobgames.com/grids/hexagons/) has the best write up of how to use hex coordinate systems I've ever seen. Since it handles distances better, I'm going to use a cube coordinate system, where each hex actually has an `x`, `y`, and `z` coordinate. Now that is decided, we can write up a map to translate directions into offsets, a way to add points together, and a distance function:

```python
neighbors = {
    'n' : (0, 1, -1),
    'ne': (1, 0, -1),
    'se': (1, -1, 0),
    's' : (0, -1, 1),
    'sw': (-1, 0, 1),
    'nw': (-1, 1, 0),
}

def add(p1, p2):
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    return (x1 + x2, y1 + y2, z1 + z2)

def move(p, d):
    return add(p, neighbors[d.strip()])

def distance(p1, p2):
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    return max(abs(x1 - x2), abs(y1 - y2), abs(z1 - z2))

That makes code for the actual question nice and clean:

read more...


AoC 2017 Day 10: Knot Cool

Source: Knot Hash

Part 1: Starting with a list of the numbers from 1 to n and a list of lengths (as input):

  1. Initialize current_position and skip_size to 0
  2. For each length element in the lengths list:
  3. Reverse the first length elements of the list (starting at current_position)
  4. Move forward by length plus skip_size
  5. Increment skip_size by 1

After applying the above algorithm, what is the product of the first two elements in the list (from the original first position, not the current_position)?

read more...


AoC 2017 Day 9: Garbage Gobbler

Source: Stream Processing

Part 1: An input stream can contain:

  • groups are delimited by { and }, groups are nestable and may contain garbage or data (objects within a group are comma delimited)
  • garbage is delimited by < and >, groups cannot be nested within garbage, a ! within garbage is an escape character: !> does not end a garbage segment

The score of a single group is equal to how many times it is nested (the innermost group of {{{}}} has score 3).

The score of a stream is the sum of the scores of all groups in that stream.

What is the total score of your input?

read more...


AoC 2017 Day 7: Tree

Source: Recursive Circus

Part 1: A tree is defined as such:

  • node (weight) -> child1, child2, ...
  • node (weight)

Where a node always has a weight, but may or may not have child nodes.

What is the name of the root node of the tree (the node without a parent)?

read more...