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.
In [29]:
function getUniqLocs(directions)
Moves = [1 0 ; -1 0 ; 0 1 ; 0 -1]
#MoveList = [Moves[search("^v<>",x),:] for x in collect(directions)]
MoveList = map(x -> Moves[search("^v<>",x),:], collect(directions))
Loc = [0 0]
Locs = Set{Tuple}([tuple(Loc...)])
for move in MoveList
Loc += move
push!(Locs, tuple(Loc...))
end
return Locs
end
Out[29]:
In [30]:
Directions = readall("advent03_input.txt")
@time length(getUniqLocs(Directions))
Out[30]:
In [27]:
function move( instr::Char, pos::Tuple{Int64,Int64} )
(x,y) = pos
if instr == '^'
return (x,y-1)
elseif instr == 'v'
return (x,y+1)
elseif instr == '<'
return (x-1,y)
elseif instr == '>'
return (x+1,y)
else
error( @sprintf "%c is not a valid movement" instr )
end
end
function visit( instructions::ASCIIString )
currentPos = (0,0)
visitedLocations = Dict{Tuple{Int64,Int64},Int64}( currentPos => 1 )
for instr in instructions
newPos = move(instr, currentPos)
if haskey(visitedLocations, newPos)
visitedLocations[newPos] += 1
else
visitedLocations[newPos] = 1
end
currentPos = newPos
end
return visitedLocations
end
Out[27]:
In [28]:
f = open("advent03_input.txt")
instructions = chomp(readline(f))
close(f)
#visits = visit(instructions)
@time numHouses = length(keys(visit(instructions)))
Out[28]:
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.
In [23]:
function countRoboSantaLocs(directions)
SantaLocs = getUniqLocs(directions[1:2:end])
RoboLocs = getUniqLocs(directions[2:2:end])
length(union(SantaLocs,RoboLocs))
end
Out[23]:
In [32]:
@time countRoboSantaLocs(Directions)
Out[32]:
In [17]:
function visitWithRobot( instructions::ASCIIString )
currentPos = (0,0)
robotPos = (0,0)
visitedLocations = Dict{Tuple{Int64,Int64},Int64}( currentPos => 2 )
for pair in enumerate(instructions)
(ind, instr) = pair
if ind % 2 == 1
# Move santa
currentPos = move(instr, currentPos)
if haskey(visitedLocations, currentPos)
visitedLocations[currentPos] += 1
else
visitedLocations[currentPos] = 1
end
else
# Move robo-santa
robotPos = move(instr, robotPos)
if haskey(visitedLocations, robotPos)
visitedLocations[robotPos] += 1
else
visitedLocations[robotPos] = 1
end
end
end
return visitedLocations
end
Out[17]:
In [31]:
### Part 2
#visitsWithRobot = visitWithRobot(instructions)
@time numHousesWithRobot = length(keys(visitWithRobot(instructions)))
@printf "Part One: %d\nPart Two: %d\n" numHouses numHousesWithRobot