Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 18, 2023
1 parent 28fd756 commit bc69447
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/18.txt
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)
105 changes: 105 additions & 0 deletions lib/days/18.rb
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
18 changes: 18 additions & 0 deletions test/18_test.rb
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

0 comments on commit bc69447

Please sign in to comment.