From 6fc6c5cf537db015682485e868d2c58eedee7323 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 7 Dec 2020 18:42:44 -0800 Subject: [PATCH] Add drawing code and elementwise reduction. --- src/advent/util.cr | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/advent/util.cr b/src/advent/util.cr index e99fd0a..52b8c3b 100644 --- a/src/advent/util.cr +++ b/src/advent/util.cr @@ -7,6 +7,12 @@ class Array(T) dest end + def reduce_elementwise(&block) + reduce do |l, r| + l.zip_with(r) { |a, b| yield a, b } + end + end + def union reduce(T.new) do |l, r| l | r @@ -18,6 +24,15 @@ class Array(T) l & r end end + + def draw(width, maps) + self.each_slice(width) do |slice| + slice.each do |c| + print maps[c] + end + puts + end + end end struct Tuple(*T)