about summary refs log tree commit diff
path: root/day08.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 /day08.rb
parent05050c3b5e20d5d475894788df6c31f867d2c51a (diff)
downloadadventofcode2018-master.tar.gz
adventofcode2018-master.tar.xz
adventofcode2018-master.zip
add rest HEAD master
Diffstat (limited to 'day08.rb')
-rw-r--r--day08.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/day08.rb b/day08.rb
new file mode 100644
index 0000000..a51543b
--- /dev/null
+++ b/day08.rb
@@ -0,0 +1,43 @@
+d=File.read("day08").split.map(&:to_i)
+
+def t(d)
+  return 0  if d.empty?
+
+  len = d.shift
+  md = d.shift
+
+  mds = 0
+  (0...len).map {
+    mds += t(d)
+  }
+  mds += d.shift(md).sum
+end
+
+def s(d)
+  return 0  if d.empty?
+
+  len = d.shift
+  md = d.shift
+
+  mds = (0...len).map {
+    s(d)
+  }
+  
+  m = d.shift(md)
+
+  if len == 0
+    m.sum
+  else
+    m.map { |i|
+      if i <=0 || i > mds.size
+        0
+      else
+        mds[i-1]
+      end
+    }.sum
+  end
+end
+
+p t(d.clone)  # 35852
+p s(d.clone)  # 33422
+