about summary refs log tree commit diff
path: root/day20.rb
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2019-11-28 17:38:10 +0100
committerLeah Neukirchen <leah@vuxu.org>2019-11-28 17:38:10 +0100
commite12436c99bb04897d6344b6351d0336e4c627573 (patch)
tree74f8afb9de5d2a466e5fe6225df3661e1280519e /day20.rb
parent05050c3b5e20d5d475894788df6c31f867d2c51a (diff)
downloadadventofcode2018-master.tar.gz
adventofcode2018-master.tar.xz
adventofcode2018-master.zip
add rest HEAD master
Diffstat (limited to 'day20.rb')
-rw-r--r--day20.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/day20.rb b/day20.rb
new file mode 100644
index 0000000..1b4fc78
--- /dev/null
+++ b/day20.rb
@@ -0,0 +1,48 @@
+r = File.read("day20")
+
+#r = "^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$"
+#r = "^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$"
+
+r.gsub!(/^\^|\$$/, '')
+
+
+def dist(str)
+  seen = []
+  x = y = 0
+  str.chars.each { |c|
+    case c
+    when "E"; x += 1
+    when "W"; x -= 1
+    when "S"; y += 1
+    when "N"; y -= 1
+    end
+    seen << [x,y]
+  }
+  seen.uniq.size
+end
+
+p1 = 0
+p2 = 0
+
+stk = [""]
+r.chars.each { |c|
+  case c
+  when "E", "S", "W", "N"
+    stk.last << c
+    p1 = [p1, dist(stk.last)].max
+    p2 += 1  if dist(stk.last) > 1000
+  when "("
+    stk << stk.last.dup
+  when "|"
+    stk.pop
+    stk << stk.last.dup
+  when ")"
+    stk.pop
+  end
+}
+
+p p1  # 3788
+p p2  # 8568
+
+# 9558 too high
+# 9557 too high