-
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
28fd756
commit bc69447
Showing
3 changed files
with
137 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
R 6 (#70c710) | ||
D 5 (#0dc571) | ||
L 2 (#5713f0) | ||
D 2 (#d2c081) | ||
R 2 (#59c680) | ||
D 2 (#411b91) | ||
L 5 (#8ceee2) | ||
U 2 (#caa173) | ||
L 1 (#1b58a2) | ||
U 2 (#caa171) | ||
R 2 (#7807d2) | ||
U 3 (#a77fa3) | ||
L 2 (#015232) | ||
U 2 (#7a21e3) |
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,105 @@ | ||
class Day18 | ||
|
||
Point = Data.define(:x, :y) do | ||
def at?(x, y) | ||
self.x == x && self.y == y | ||
end | ||
|
||
def neighbors | ||
[ | ||
Point.new(x - 1, y), | ||
Point.new(x + 1, y), | ||
Point.new(x, y - 1), | ||
Point.new(x, y + 1) | ||
].reject { |p| p.x.negative? || p.y.negative? } | ||
end | ||
end | ||
|
||
GridInfo = Data.define(:min_x, :max_x, :min_y, :max_y) do | ||
def valid?(x, y) | ||
x >= min_x && x <= max_x && y >= min_y && y < max_y | ||
end | ||
|
||
def size | ||
(max_x - min_x + 1) * (max_y - min_y + 1) | ||
end | ||
end | ||
|
||
UP = 'U' | ||
DOWN = 'D' | ||
LEFT = 'L' | ||
RIGHT = 'R' | ||
|
||
def part1(input) | ||
trench = Set.new | ||
current = Point.new(0, 0) | ||
trench.add(current) | ||
|
||
input.split("\n").each do |line| | ||
direction, count, = line.split | ||
count = count.to_i | ||
|
||
case direction | ||
when UP | ||
count.times do | ||
current = Point.new(current.x, current.y - 1) | ||
trench.add current | ||
end | ||
when DOWN | ||
count.times do | ||
current = Point.new(current.x, current.y + 1) | ||
trench.add current | ||
end | ||
when LEFT | ||
count.times do | ||
current = Point.new(current.x - 1, current.y) | ||
trench.add current | ||
end | ||
when RIGHT | ||
count.times do | ||
current = Point.new(current.x + 1, current.y) | ||
trench.add current | ||
end | ||
end | ||
end | ||
|
||
info = GridInfo.new( | ||
trench.map(&:x).min, | ||
trench.map(&:x).max, | ||
trench.map(&:y).min, | ||
trench.map(&:y).max | ||
) | ||
|
||
outside = Set.new | ||
|
||
(info.min_x..info.max_x).each do |x| | ||
(info.min_y..info.max_y).each do |y| | ||
point = Point.new(x, y) | ||
|
||
if (x == info.min_x || y == info.min_y || x == info.max_x || y == info.max_y) && !trench.include?(point) | ||
outside.add(point) | ||
end | ||
end | ||
end | ||
|
||
outside.to_a.each do |point| | ||
check_neighbors(point, trench, outside, info) | ||
end | ||
|
||
info.size - outside.size | ||
end | ||
|
||
def check_neighbors(point, trench, outside, info) | ||
point.neighbors.each do |p| | ||
next if trench.include?(p) || outside.include?(p) || !info.valid?(p.x, p.y) | ||
|
||
outside.add(p) | ||
check_neighbors(p, trench, outside, info) | ||
end | ||
end | ||
|
||
def part2(input) | ||
'TODO' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require_relative '../app' | ||
|
||
class TestDay18 < Minitest::Test | ||
def setup | ||
@data = File.read(File.join(APP_ROOT, 'examples', '18.txt')).rstrip | ||
@day = Day18.new | ||
end | ||
|
||
def test_part1 | ||
assert_equal @day.part1(@data), 62 | ||
end | ||
|
||
def test_part2 | ||
assert_equal @day.part2(@data), '' | ||
end | ||
end |