Skip to content

Commit

Permalink
Year 2016 Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Oct 29, 2024
1 parent 7060d98 commit 988b14c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 2016/day02.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
lines = STDIN.read.split("\n")

DIFFS = {
'U' => [-1, 0],
'R' => [0, 1],
'D' => [1, 0],
'L' => [0, -1]
}
KEYPAD1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
pos = [1, 1]
puts (
lines.map do |line|
line.chars.each do |instr|
new_pos = pos.zip(DIFFS[instr]).map(&:sum)
next unless [0, 1, 2].include?(new_pos.first)
next unless [0, 1, 2].include?(new_pos.last)
pos = new_pos
end
KEYPAD1[pos.first][pos.last]
end.join
)

KEYPAD2 = [
'..1..',
'.234.',
'56789',
'.ABC.',
'..D..'
]
pos = [2, 0]
puts (
lines.map do |line|
line.chars.each do |instr|
new_pos = pos.zip(DIFFS[instr]).map(&:sum)
next if new_pos.first < 0 || new_pos.first >= KEYPAD2.size
next if new_pos.last < 0 || new_pos.last >= KEYPAD2.size
next if KEYPAD2[new_pos.first][new_pos.last] == '.'
pos = new_pos
end
KEYPAD2[pos.first][pos.last]
end.join
)
4 changes: 4 additions & 0 deletions 2016/examples/day02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ULL
RRDDD
LURDL
UUUUD

0 comments on commit 988b14c

Please sign in to comment.