From 896addebb0843d99d9845140eb3b1167bf6e9787 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sun, 18 Dec 2022 14:34:19 +0000 Subject: Day 18, part 1 --- day18/part1.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day18/part1.rb (limited to 'day18/part1.rb') diff --git a/day18/part1.rb b/day18/part1.rb new file mode 100644 index 0000000..dd4ba41 --- /dev/null +++ b/day18/part1.rb @@ -0,0 +1,29 @@ +require 'set' + +grid = Hash.new + +$stdin.readlines.each do |line| + x, y, z = line.strip.split(?,).map(&:to_i) + grid[x] = Hash.new if grid[x].nil? + grid[x][y] = Set.new if grid[x][y].nil? + grid[x][y] << z +end + +sa = 0 + +grid.each do |x, yzs| + yzs.each do |y, zs| + zs.each do |z| + tsa = 6 + tsa -= 1 if !grid[x-1].nil? && grid[x-1][y]&.include?(z) + tsa -= 1 if !grid[x+1].nil? && grid[x+1][y]&.include?(z) + tsa -= 1 if grid[x][y-1]&.include?(z) + tsa -= 1 if grid[x][y+1]&.include?(z) + tsa -= 1 if grid[x][y].include?(z-1) + tsa -= 1 if grid[x][y].include?(z+1) + sa += tsa + end + end +end + +puts sa -- cgit v1.2.1