Skip to content

Instantly share code, notes, and snippets.

@jstrube
Created December 15, 2015 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstrube/3d54e15f7d051b72032b to your computer and use it in GitHub Desktop.
Save jstrube/3d54e15f7d051b72032b to your computer and use it in GitHub Desktop.
adventofcode
function santa(current, next)
if next == '^' current[1] += 1
elseif next == 'v' current[1] -= 1
elseif next == '<' current[2] += 1
elseif next == '>' current[2] -= 1
else println("not known:", next) end
current
end
#assign variable grid = input from http://adventofcode.com/day/3/input
### This solution doesn't work
x = Set{ Array{Int64,2}}()
start = [0 0]
push!(x, start)
for i in grid
start = santa(start, i)
push!(x, start)
end
println(length(x))
### This solution works!
x = Set{ ASCIIString }()
start = [0 0]
push!(x, string(start))
for i in grid
start = santa(start, i)
push!(x, string(start))
end
println(length(x))
@tomasaschan
Copy link

If you rewrite the solution to use tuples, it works too:

### This solution works too!
function santa(current, next)
    next == '^' && return (current[1] + 1, current[2])
    next == 'v' && return (current[1] - 1, current[2])
    next == '<' && return (current[1], current[2] + 1)
    next == '>' && return (current[1], current[2] - 1)
    println("not known: ", next)
end

x = Set{Tuple{Int64,Int64}}()
start = (0,0)
push!(x, start)

for i in grid
    start = santa(start, i)
    push!(x, start)
end
println(length(x))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment