Created
December 7, 2018 23:11
-
-
Save rooZzz/95de73c0ba04419185b24dab3d99476a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Coordinate | |
| attr_accessor :id, :x, :y, :area, :blacklisted | |
| def initialize(x, y, id) | |
| @id = id | |
| @x = x | |
| @y = y | |
| @area = 0 | |
| @blacklisted = false | |
| end | |
| end | |
| ids = [*'a'..'z', *'A'..'Z'] | |
| coordinates = File.read('input.txt').each_line.map do |line| | |
| x, y = line.split(', ').map(&:to_i) | |
| Coordinate.new(x, y, ids.shift) | |
| end | |
| max_x = coordinates.max_by(&:x).x | |
| max_y = coordinates.max_by(&:y).y | |
| (0..max_y).each do |y| | |
| (0..max_x).each do |x| | |
| distances = coordinates.group_by {|coordinate| (coordinate.x - x).abs + (coordinate.y - y).abs} | |
| closest = distances.min | |
| if closest.last.size == 1 | |
| closest_coordinate = closest.last.first | |
| closest_coordinate.blacklisted ||= [0, max_x].include?(x) || [0, max_y].include?(y) | |
| closest_coordinate.area += 1 | |
| print closest_coordinate.id | |
| else | |
| print '.' | |
| end | |
| end | |
| puts | |
| end | |
| coordinate = coordinates.reject(&:blacklisted).sort_by(&:area).last | |
| puts "Coordinate #{coordinate.id}, area: #{coordinate.area}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment