m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/12/a.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:28:06 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:28:06 +0100
commit13d928de6e5fb1bbc6a7e2daa10d28ecee982689 (patch)
treee9330e7e20a5cad3b73d37d80120382838a22e2e /12/a.rb
parent69a8c7bb9862966b2f21dbb61e86f652f9b238f9 (diff)
Add day 12
Diffstat (limited to '12/a.rb')
-rw-r--r--12/a.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/12/a.rb b/12/a.rb
new file mode 100644
index 0000000..e47babe
--- /dev/null
+++ b/12/a.rb
@@ -0,0 +1,39 @@
+instructions = File.readlines('input.txt').map do |line|
+ [line.slice(0, 1), line.slice(1, line.length).to_i]
+end
+
+n = 0
+e = 0
+direction = 0
+
+instructions.each do |instruction|
+ case instruction[0]
+ when 'N'
+ n += instruction[1]
+ when 'S'
+ n -= instruction[1]
+ when 'E'
+ e += instruction[1]
+ when 'W'
+ e -= instruction[1]
+ when 'F'
+ case direction
+ when 0
+ e += instruction[1]
+ when 90
+ n += instruction[1]
+ when 180
+ e -= instruction[1]
+ when 270
+ n -= instruction[1]
+ end
+ when 'L'
+ direction += instruction[1]
+ direction %= 360
+ when 'R'
+ direction -= instruction[1]
+ direction %= 360
+ end
+end
+
+puts n.abs + e.abs