diff --git a/common.cr b/common.cr new file mode 100644 index 0000000..bba177d --- /dev/null +++ b/common.cr @@ -0,0 +1,22 @@ +module Enumerable(T) + def count_each(others) + count_map = {} of T => Int32 + each do |other| + count_map[other] = (count_map[other]? || 0) + 1 + end + return count_map + end +end + +class Rectangle + def initialize(@x : Int32, @y : Int32, @width : Int32, @height : Int32) + end + + def each_coord(&block) + (@x...(@x + @width)).each do |x| + (@y...(@y + @height)).each do |y| + yield x, y + end + end + end +end diff --git a/day3.cr b/day3.cr new file mode 100644 index 0000000..5befbc0 --- /dev/null +++ b/day3.cr @@ -0,0 +1,38 @@ +require "./common.cr" + +lines = File.read("day3").split("\n") +lines.pop + +REGEX = /#([0-9]+) @ ([0-9]+),([0-9]+): ([0-9]+)x([0-9]+)/ + +claims = {} of Int32 => Rectangle +lines.each do |line| + match = line.match(REGEX).not_nil! + claim = match[1].to_i32 + x = match[2].to_i32 + y = match[3].to_i32 + width = match[4].to_i32 + height = match[5].to_i32 + claims[claim] = Rectangle.new(x, y, width, height) +end + +claimed_coords = {} of Tuple(Int32, Int32) => Int32 +claims.values.each do |claim| + claim.each_coord do |x, y| + coord = {x, y} + claimed_coords[coord] = (claimed_coords[coord]? || 0) + 1 + end +end + +puts "Overlapping: #{claimed_coords.values.count &.>(1)}" + +claims.each do |k, v| + count_overlapped = 0 + v.each_coord do |x, y| + count_overlapped += 1 if claimed_coords[{x, y}] > 1 + end + if count_overlapped == 0 + puts "No overlaps: #{k}" + exit + end +end