Tuesday, December 25, 2018

Advent of Code 2015 - Day 3: Perfectly Spherical Houses in a Vacuum

Part 1

Description

Santa is delivering presents to an infinite two-dimensional grid of houses.
He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.
However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?
For example:
  • > delivers presents to 2 houses: one at the starting location, and one to the east.
  • ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
  • ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.

Input


Solution

For this day's puzzle, I decided to use VBA. It's because I don't think it will be possible to solve it with just formulas. But, first I have to split and translate this input into readable variables, i.e. offsets in Excel. So, going north is translated into offset (-1,0) or one column upward, etc.


Then, I write a VBA code that looks like this:

Sub Perfectly()
    Dim x, y, posx, posy, lastx, lasty, startx, starty As Integer
    Dim lastpos, curpos As Range
    
    ActiveSheet.Range("a11").Select
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(60, 100).Select
    ActiveCell.Value = ActiveCell.Value + 1
Start:
    ActiveCell.Offset(y, x).Select
    ActiveCell.Value = ActiveCell.Value + 1
    posy = ActiveCell.Row
    posx = ActiveCell.Column
    ActiveSheet.Cells(lasty, lastx).Select
    ActiveCell.Offset(-1, 1).Select
If IsEmpty(ActiveCell.Value) = False Then
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(posy, posx).Select
    GoTo Start
    Else: GoTo Ended
End If
Ended:
End Sub

Again, I am not a programmer, only a hobbyist. Therefore I didn't wrote this code for its speed. For me, as long as it works, it's OK. So, let me explain a bit about the code. Line 5-10 are practically for reading the translated input. Line 11-12 do the starting location, by adding value +1 to that location. The Loop between Start: and Ended: is doing the rest, moving left and right, up and down, and every time adding value +1. In the end, I counted (not sum!) all the filled cells, using:

=COUNTIF(A17:EH108,">0")

Part 2

Description

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:
  • ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
  • ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
  • ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.

Solution

Now, for the part two, the VBA code is similar to the one in the part one, only there has to be two different parts reading inputs and moving independently. The code I made looks like this:

Sub Perfectly2()
    Dim x, y, posax, posay, posby, posbx, lastx, lasty, startx, starty As Integer
    Dim lastpos, curpos As Range
    
    ActiveSheet.Range("a11").Select
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(260, 100).Select
    ActiveCell.Value = ActiveCell.Value + 1
    ActiveCell.Offset(y, x).Select
    ActiveCell.Value = ActiveCell.Value + 1
    posay = ActiveCell.Row
    posax = ActiveCell.Column
    ActiveSheet.Cells(lasty, lastx).Select
    ActiveCell.Offset(-1, 1).Select
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(260, 100).Select
    ActiveCell.Offset(y, x).Select
    ActiveCell.Value = ActiveCell.Value + 1
    posby = ActiveCell.Row
    posbx = ActiveCell.Column
    ActiveSheet.Cells(lasty, lastx).Select
    ActiveCell.Offset(-1, 1).Select
Start:
If IsEmpty(ActiveCell.Value) = False Then
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(posay, posax).Select
    ActiveCell.Offset(y, x).Select
    ActiveCell.Value = ActiveCell.Value + 1
    posay = ActiveCell.Row
    posax = ActiveCell.Column
    ActiveSheet.Cells(lasty, lastx).Select
    ActiveCell.Offset(-1, 1).Select
    x = ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    y = ActiveCell.Value
    lasty = ActiveCell.Row
    lastx = ActiveCell.Column
    ActiveSheet.Cells(posby, posbx).Select
    ActiveCell.Offset(y, x).Select
    ActiveCell.Value = ActiveCell.Value + 1
    posby = ActiveCell.Row
    posbx = ActiveCell.Column
    ActiveSheet.Cells(lasty, lastx).Select
    ActiveCell.Offset(-1, 1).Select
    GoTo Start
    Else: GoTo Ended
End If
Ended:
End Sub

And, finally same formula as above to count all filled cells.
Share:

0 komentar:


Recent Posts

Categories

Blog Archive