Skip to content

Commit

Permalink
Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeilko committed Dec 15, 2023
1 parent 0ad7321 commit 25aef58
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
132 changes: 132 additions & 0 deletions days/day10.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require_relative '../lib/aoc'
require_relative '../lib/grid'

module Day10
extend AOC::Day

@num = 10
@ans_test1 = 8
@ans_test2 = 10

CONNECTS = {
[1, 0] => ["-", "7", "J"],
[0, 1] => ["|", "L", "J"],
[-1, 0] => ["-", "L", "F"],
[0, -1] => ["|", "7", "F"],
}

NEIGHBORS = {
"|" => [[0, -1], [0, 1]],
"-" => [[-1, 0], [1, 0]],
"L" => [[0, -1], [1, 0]],
"J" => [[0, -1], [-1, 0]],
"7" => [[-1, 0], [0, 1]],
"F" => [[1, 0], [0, 1]],
}

def self.solve_part1(input)
grid = Grid.string_to_grid(input)

s_x, s_y = find_and_replace_start(grid)
visited = find_loop(grid, s_x, s_y)

return visited.values.max
end

def self.solve_part2(input)
grid = Grid.string_to_grid(input)
s_x, s_y = find_and_replace_start(grid)
visited = find_loop(grid, s_x, s_y)

r = 0
grid.each_with_index do |row, y|
in_loop = false
last_corner = nil
row.split("").each_with_index do |c, x|
if visited.key?([x, y])
# This coord is part of the loop
if c == "|" or (c == "J" and last_corner == "F") or (c == "7" and last_corner == "L")
in_loop = !in_loop
end

last_corner = c if ["7", "J", "L", "F"].include?(c)
else
# Not part of the loop
r += 1 if in_loop
end
end
end

return r
end

def self.find_loop(grid, s_x, s_y)
to_visit = {}
visited = {}
to_visit[[s_x, s_y]] = 0
while to_visit.length > 0
coords, cost = to_visit.shift
x, y = coords
visited[[x, y]] = cost

NEIGHBORS[grid[y][x]].each do |dx, dy|
nx = x+dx
ny = y+dy
nc = cost+1

next if visited.key?([nx, ny])

if to_visit.key?([nx, ny])
if to_visit[[nx, ny]] > nc
to_visit[[nx, ny]] = nc
end
else
to_visit[[nx, ny]] = nc
end
end
end

return visited
end

def self.find_and_replace_start(grid)
s_x = s_y = -1
grid.each_with_index do |line, y|
line.split("").each_with_index do |char, x|
if char == "S"
s_x = x
s_y = y
break
end
end
break if s_x != -1
end

s = find_start_symbol(grid, s_x, s_y)
grid[s_y].sub!("S", s)

return [s_x, s_y]
end

def self.find_start_symbol(grid, s_x, s_y)
r = ""
[[1, 0], [0, 1], [-1, 0], [0, -1]].each do |dx, dy|
nx = s_x+dx
ny = s_y+dy
next unless CONNECTS[[dx, dy]].include?(grid[ny][nx])
r += dx.to_s + dy.to_s
end

translate = {
"10-10" => "-",
"100-1" => "L",
"1001" => "F",
"010-1" => "|",
"01-10" => "7",
"-100-1" => "J"
}
return translate[r]
end
end

Day10.run
5 changes: 5 additions & 0 deletions inputs/test/test_input10_p1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ
10 changes: 10 additions & 0 deletions inputs/test/test_input10_p2
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
2 changes: 1 addition & 1 deletion lib/grid.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Grid
def self.string_to_grid(inp)
return inp.lines.map{ |line| line.strip.split("") }
return inp.lines.map{ |line| line.strip }
end

def self.get_neighbours(grid, x, y, include_coords = false)
Expand Down

0 comments on commit 25aef58

Please sign in to comment.