Skip to content

Instantly share code, notes, and snippets.

@samuel-massinon
Created December 2, 2016 16:37
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 samuel-massinon/2abd7bdc49238904cd96d2f31344a0c6 to your computer and use it in GitHub Desktop.
Save samuel-massinon/2abd7bdc49238904cd96d2f31344a0c6 to your computer and use it in GitHub Desktop.
Advent Day 2
import Base.strip
function do_stuff(input)
tokens = strip(split(input, "\n"))
get_code(tokens)
println()
get_code_weird(tokens)
end
function get_code(tokens)
coordinates = [2, 2]
numpad = [1 2 3; 4 5 6; 7 8 9]
for token in tokens
for char in token
if char == 'U'
coordinates[2] = max(coordinates[2] - 1, 1)
elseif char == 'R'
coordinates[1] = min(coordinates[1] + 1, 3)
elseif char == 'D'
coordinates[2] = min(coordinates[2] + 1, 3)
elseif char == 'L'
coordinates[1] = max(coordinates[1] - 1, 1)
else
error( string("Bad Char ", char))
end
end
# coordinates
print(numpad[coordinates[2], coordinates[1]])
end
end
function get_code_weird(tokens)
coordinates = [1, 3]
numpad = [0xF 0xF 0x1 0xF 0xF; 0xF 0x2 0x3 0x4 0xF; 0x5 0x6 0x7 0x8 0x9; 0xF 0xA 0xB 0xC 0xF; 0xF 0xF 0xD 0xF 0xF]
println(tokens)
for token in tokens
for char in token
if char == 'U'
x_change = 0
y_change = -1
elseif char == 'R'
x_change = 1
y_change = 0
elseif char == 'D'
x_change = 0
y_change = 1
elseif char == 'L'
x_change = -1
y_change = 0
else
error( string("Bad Char ", char))
end
if good_numpad_index(numpad, coordinates, y_change, x_change)
coordinates[2] += y_change
coordinates[1] += x_change
end
end
# coordinates
print(uppercase(hex(numpad[coordinates[2], coordinates[1]])))
end
end
function good_numpad_index(numpad, coordinates, y_change, x_change)
return size(numpad, 2) >= coordinates[2] + y_change > 0 &&
size(numpad, 1) >= coordinates[1] + x_change > 0 &&
numpad[coordinates[2] + y_change, coordinates[1] + x_change] != 0xF
end
file = open("input.txt")
input = readstring(file)
do_stuff(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment