From a5e6c4c1452fa7ec889f4ba1ad50f38b5a754cca Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 7 Dec 2020 18:31:33 -0800 Subject: [PATCH] Add utility to draw the state of a hash. --- src/advent/util.cr | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/advent/util.cr b/src/advent/util.cr index 00acd6b..e99fd0a 100644 --- a/src/advent/util.cr +++ b/src/advent/util.cr @@ -54,6 +54,9 @@ struct Tuple(*T) { {% for t, i in T %} self[{{i}}].signum, {% end %} } {% end %} end + + def abs + end end struct Number @@ -62,3 +65,17 @@ struct Number self // self.abs end end + +class Hash(K, V) + def draw(maps) + min_x, max_x = keys.minmax_of &.[0] + min_y, max_y = keys.minmax_of &.[1] + (max_y-min_y+1).times do |y| + (max_x-min_x+1).times do |x| + next unless has_key?({x,y}) + print maps[self[{x,y}]] + end + puts + end + end +end