-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd05978
commit dd71c2e
Showing
5 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
...#...... | ||
.......#.. | ||
#......... | ||
.......... | ||
......#... | ||
.#........ | ||
.........# | ||
.......... | ||
.......#.. | ||
#...#..... |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class Day11 | ||
|
||
def solve(input, factor = 2) | ||
grid = [] | ||
iterations = factor - 1 | ||
|
||
input.split("\n").each do |row| | ||
row.chars.each_with_index do |char, x| | ||
grid[x] ||= [] | ||
grid[x] << char | ||
end | ||
end | ||
|
||
x_expansions = [] | ||
|
||
grid.length.times do |x| | ||
if grid[x].all? { |c| c == '.' } | ||
x_expansions << x | ||
end | ||
end | ||
|
||
y_expansions = [] | ||
range = 0..(grid.length - 1) | ||
|
||
grid.first.length.times do |y| | ||
if range.map { |x| grid[x][y] }.all? { |c| c == '.' } | ||
y_expansions << y | ||
end | ||
end | ||
|
||
points = [] | ||
|
||
grid.length.times do |x| | ||
grid.first.length.times do |y| | ||
points << [x, y] if grid[x][y] == '#' | ||
end | ||
end | ||
|
||
distances = [] | ||
|
||
points.length.times do |index| | ||
p1 = points[index] | ||
|
||
points[(index + 1)..].each do |p2| | ||
x_range = Range.new(*[p1[0], p2[0]].sort) | ||
y_range = Range.new(*[p1[1], p2[1]].sort) | ||
|
||
x_count = x_expansions.count do |exp| | ||
x_range.include?(exp) | ||
end | ||
|
||
y_count = y_expansions.count do |exp| | ||
y_range.include?(exp) | ||
end | ||
|
||
d_x = (p1[0] - p2[0]).abs + (x_count * iterations) | ||
d_y = (p1[1] - p2[1]).abs + (y_count * iterations) | ||
|
||
distances << (d_x + d_y) | ||
end | ||
end | ||
|
||
distances.sum | ||
end | ||
|
||
def part1(input) | ||
solve(input) | ||
end | ||
|
||
def part2(input, factor = 1_000_000) | ||
solve(input, factor) | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require_relative '../app' | ||
|
||
class TestDay11 < Minitest::Test | ||
def setup | ||
@data = File.read(File.join(APP_ROOT, 'examples', '11.txt')).rstrip | ||
@day = Day11.new | ||
end | ||
|
||
def test_part1 | ||
assert_equal @day.part1(@data), 374 | ||
end | ||
|
||
def test_part2 | ||
assert_equal @day.part2(@data, 10), 1030 | ||
|
||
assert_equal @day.part2(@data, 100), 8410 | ||
end | ||
end |