Skip to content

Commit

Permalink
Year 2016 Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Oct 27, 2024
1 parent d2483a3 commit 02a4846
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 2016/day01.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
line = gets.chomp

DIR_CHANGE = {
[0, -1] => { 'L' => [-1, 0], 'R' => [1, 0] },
[1, 0] => { 'L' => [0, -1], 'R' => [0, 1] },
[0, 1] => { 'L' => [1, 0], 'R' => [-1, 0] },
[-1, 0] => { 'L' => [0, 1], 'R' => [0, -1] },
}

pos = [0, 0]
dir = [0, -1]
visited = Set.new([pos.to_s])
p2 = nil
line.split(', ').each do |s|
dir = DIR_CHANGE[dir][s[0]]
step_count = s[1..].to_i
step_count.times do
pos[0] += dir[0]
pos[1] += dir[1]
p2 = pos.map(&:abs).sum if visited.include?(pos.to_s) && p2.nil?
visited.add(pos.to_s)
end
end

puts pos.map(&:abs).sum
puts p2
1 change: 1 addition & 0 deletions 2016/examples/day01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
R8, R4, R4, R8

0 comments on commit 02a4846

Please sign in to comment.