From 08fd489704409e1d7414ff51bdac5d789cabb668 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 6 Dec 2018 12:07:00 +0000 Subject: [day06] Done --- day06/part2 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 day06/part2 (limited to 'day06/part2') diff --git a/day06/part2 b/day06/part2 new file mode 100755 index 0000000..dc74890 --- /dev/null +++ b/day06/part2 @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +def manhattan_distance(x, y, p) + return (x - p.first).abs + (y - p.last).abs +end + +input = $stdin.readlines.map(&:chomp).map{|i|i.split(", ").map(&:to_i)} +MAX_DISTANCE = 10000 + +xoff = input.map(&:first).min - 1 +yoff = input.map(&:last).min - 1 + +input.each do |p| + p[0] -= xoff + p[1] -= yoff +end + +wid = input.map(&:first).max + 1 +hei = input.map(&:last).max + 1 + +grid = Array.new(wid) { Array.new(hei, false) } + +(0...wid).each do |x| + (0...hei).each do |y| + dist_sum = input.map{|p|manhattan_distance(x, y, p)}.sum + grid[x][y] = true if dist_sum < MAX_DISTANCE + end +end + +puts grid.flatten.count(true) -- cgit v1.2.1