Skip to content

Commit

Permalink
day 10 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 11, 2023
1 parent 51d296c commit cd05978
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,5 @@ Style/NumericLiterals:
Enabled: false
Style/MutableConstant:
Enabled: false
Naming/MethodParameterName:
Enabled: false
9 changes: 9 additions & 0 deletions examples/10_c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
...........
.S-------7.
.|F-----7|.
.||.....||.
.||.....||.
.|L-7.F-J|.
.|..|.|..|.
.L--J.L--J.
...........
9 changes: 9 additions & 0 deletions examples/10_d.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
..........
.S------7.
.|F----7|.
.||....||.
.||....||.
.|L-7F-J|.
.|..||..|.
.L--JL--J.
..........
10 changes: 10 additions & 0 deletions examples/10_e.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L
160 changes: 138 additions & 22 deletions lib/days/10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
class Day10

START = 'S'
TOP_LEFT = 'F'
TOP_RIGHT = '7'
BOT_LEFT = 'L'
BOT_RIGHT = 'J'
UP_RIGHT = 'F'
UP_LEFT = '7'
DOWN_RIGHT = 'L'
DOWN_LEFT = 'J'
HORIZONTAL = '-'
VERTICAL = '|'

# Point = Struct.new(:x, :y)

def part1(input)
def parse(input)
grid = []

input.split("\n").each do |row|
Expand Down Expand Up @@ -43,25 +41,33 @@ def part1(input)
path << found
end

# print_grid(grid, path)
[grid, path]
end

def part1(input)
_grid, path = parse(input)

path.length / 2
end

COLORS = [:red, :yellow, :green, :cyan, :blue, :magenta]

def print_grid(grid, path)
def print_grid(grid, path, free_cells = [])
grid.first.length.times do |y|
grid.length.times do |x|
strings = grid.length.times.map do |x|
char = grid[x][y]
if path.include?([x, y])
color = path.index([x, y]) % COLORS.length
print char.send(COLORS[color])
char.send(COLORS[color])
elsif char == '*'
char.grey
elsif free_cells.include?([x, y])
'O'.light_white
else
print char
'I'.colorize(color: :white, mode: :bold, background: :light_blue)
end
end
print "\n"
puts strings.join
end
end

Expand All @@ -71,38 +77,148 @@ def find_available_paths(grid, position)
current = grid[position[0]][position[1]]

if (position[1] - 1) > -1 &&
[TOP_LEFT, TOP_RIGHT, VERTICAL].include?(grid[position[0]][position[1] - 1]) &&
[VERTICAL, BOT_LEFT, BOT_RIGHT, START].include?(current)
[UP_RIGHT, UP_LEFT, VERTICAL].include?(grid[position[0]][position[1] - 1]) &&
[VERTICAL, DOWN_RIGHT, DOWN_LEFT, START].include?(current)
# travels up
paths << [position[0], position[1] - 1]
end

if (position[0] + 1) < grid.length &&
[HORIZONTAL, TOP_RIGHT, BOT_RIGHT].include?(grid[position[0] + 1][position[1]]) &&
[HORIZONTAL, BOT_LEFT, TOP_LEFT, START].include?(current)
[HORIZONTAL, UP_LEFT, DOWN_LEFT].include?(grid[position[0] + 1][position[1]]) &&
[HORIZONTAL, DOWN_RIGHT, UP_RIGHT, START].include?(current)
# travel right
paths << [position[0] + 1, position[1]]
end

if (position[1] + 1 < grid.first.length) &&
[VERTICAL, BOT_LEFT, BOT_RIGHT].include?(grid[position[0]][position[1] + 1]) &&
[VERTICAL, TOP_LEFT, TOP_RIGHT, START].include?(current)
[VERTICAL, DOWN_RIGHT, DOWN_LEFT].include?(grid[position[0]][position[1] + 1]) &&
[VERTICAL, UP_RIGHT, UP_LEFT, START].include?(current)
# travel down
paths << [position[0], position[1] + 1]
end

if (position[0] - 1) > -1 &&
[HORIZONTAL, BOT_LEFT, TOP_LEFT].include?(grid[position[0] - 1][position[1]]) &&
[HORIZONTAL, TOP_RIGHT, BOT_RIGHT, START].include?(current)
[HORIZONTAL, DOWN_RIGHT, UP_RIGHT].include?(grid[position[0] - 1][position[1]]) &&
[HORIZONTAL, UP_LEFT, DOWN_LEFT, START].include?(current)
# travel left
paths << [position[0] - 1, position[1]]
end

paths
end

def build_2x(grid, path)
@grid = []

# puts '🛠️ Building 2x grid...'
grid.length.times do |x|
grid.first.length.times do |y|
@grid[(x * 2)] ||= []
@grid[(x * 2) + 1] ||= []
@grid[x * 2][y * 2] = grid[x][y]
@grid[(x * 2) + 1][y * 2] = '*'
@grid[(x * 2) + 1][(y * 2) + 1] = '*'
@grid[(x * 2)][(y * 2) + 1] = '*'
end
end

# puts '🛠️ Building 2x path...'
@path = []
path.each_with_index do |step, index|
@path << [
step[0] * 2, step[1] * 2
]

a_x = path[index][0]
a_y = path[index][1]

if path[index + 1]
b_x = path[index + 1][0]
b_y = path[index + 1][1]
else
b_x = path[0][0]
b_y = path[0][1]
end

d_x = b_x - a_x
d_y = b_y - a_y

@path << [
(step[0] * 2) + d_x,
(step[1] * 2) + d_y
]
end
end

def part2(input)
'TODO'
@visited = Set.new

grid, path = parse(input)

build_2x(grid, path)

@grid_width = @grid.length
@grid_height = @grid.first.length

@free_cells = Set.new

cells_to_check = []

@grid_width.times do |x|
cells_to_check << [x, 0]
cells_to_check << [x, @grid.first.length - 1]
end

@grid_height.times do |y|
cells_to_check << [0, y]
cells_to_check << [@grid.length - 1, y]
end

cells_to_check.uniq!.reject! do |c|
@path.include?(c)
end

loop do
cells_to_check = cells_to_check.map do |n|
check_cell(n)
end

cells_to_check = cells_to_check.compact.flatten(1).uniq.reject { @free_cells.include?(_1) }

break if cells_to_check.none?
end

count = 0

# puts '💯 Counting results'

@grid.first.length.times do |y|
@grid.length.times do |x|
count += 1 if @grid[x][y] != '*' && !@path.include?([x, y]) && !@free_cells.include?([x, y])
end
end

print_grid(@grid, @path, @free_cells)

count
end

def check_cell(point)
@free_cells << point

neighbor_points(point).reject do |n|
@free_cells.include?(n) || @path.include?(n)
end
end

def neighbor_points(point)
points = []

points << [point[0] - 1, point[1]] if point[0].positive?
points << [point[0], point[1] - 1] if point[1].positive?
points << [point[0] + 1, point[1]] if point[0] + 1 < @grid_width
points << [point[0], point[1] + 1] if point[1] + 1 < @grid_height

points.reject { |p| @path.include?(p) }
end
end
13 changes: 10 additions & 3 deletions test/10_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ def test_part1
assert_equal @day.part1(data_b), 8
end

# def test_part2
# assert_equal @day.part2(@data), ''
# end
def test_part2
data_c = File.read(File.join(APP_ROOT, 'examples', '10_c.txt')).rstrip
assert_equal @day.part2(data_c), 4

data_d = File.read(File.join(APP_ROOT, 'examples', '10_d.txt')).rstrip
assert_equal @day.part2(data_d), 4

data_e = File.read(File.join(APP_ROOT, 'examples', '10_e.txt')).rstrip
assert_equal @day.part2(data_e), 10
end
end

0 comments on commit cd05978

Please sign in to comment.