Skip to content

Instantly share code, notes, and snippets.

@Skarlso
Last active December 1, 2016 09:57
Show Gist options
  • Save Skarlso/eadb2eedb51e42f1fa35eee2fb192f2d to your computer and use it in GitHub Desktop.
Save Skarlso/eadb2eedb51e42f1fa35eee2fb192f2d to your computer and use it in GitHub Desktop.
Advent Of Code 2016 Day 01
path = [[]]
file = File.open("input.txt")
contents = file.read
split = contents.split(", ")
split.each do |s|
m = s.match(/([L|R])(\d+)/)
l = m[1]
n = m[2]
path << [l, n.to_i]
end
x = 0
y = 0
d = 0
path.each do |p|
case p[0]
when 'L'
d = (d - 1) % 4
when 'R'
d = (d + 1) % 4
end
next if p.empty?
case d
when 0
y += p[1]
when 1
x += p[1]
when 2
y -= p[1]
when 3
x -= p[1]
end
end
p x.abs + y.abs
path = [[]]
file = File.open("input.txt")
contents = file.read
split = contents.split(", ")
split.each do |s|
m = s.match(/([L|R])(\d+)/)
l = m[1]
n = m[2]
path << [l, n.to_i]
end
x = 0
y = 0
d = 0
pos = {}
path.each do |p|
case p[0]
when 'L'
d = (d - 1) % 4
when 'R'
d = (d + 1) % 4
end
next if p.empty?
p[1].times do |i|
case d
when 0
y += 1
when 1
x += 1
when 2
y -= 1
when 3
x -= 1
end
if pos[[x,y]] != nil
pos[[x, y]] += 1
else
pos[[x,y]] = 1
end
if pos[[x,y]] == 2
res = x+y
puts res.abs
puts pos[[x,y]]
exit 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment