Skip to content

Commit

Permalink
Solve 2023-21 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc32 committed Dec 21, 2023
1 parent 8a252b2 commit eb081dc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 2023/21/aoc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
Empty file added 2023/21/aoc.out
Empty file.
40 changes: 40 additions & 0 deletions 2023/21/sol1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'set'

$stdout = File.open('aoc.out', 'w')
grid = File.open('aoc.in') do |f|
f.readlines.map do |line|
line.chomp.chars
end
end

def find_start(grid)
grid.size.times.each do |i|
grid[i].size.times.each do |j|
if grid[i][j] == 'S'
return [i, j]
end
end
end
end

start = find_start(grid)

def bfs(grid, start)
reachable = Set.new
reachable << start
64.times do
new_reachable = Set.new
reachable.each do |x, y|
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |dx, dy|
next if grid[x + dx][y + dy] == '#'
next unless (0...grid.size).cover?(x + dx) && (0...grid[0].size).cover?(y + dy)
new_reachable << [x + dx, y + dy]
end
end
reachable = new_reachable
end

reachable.size
end

p bfs(grid, start)
54 changes: 54 additions & 0 deletions 2023/21/sol2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'set'

$stdout = File.open('aoc.out', 'w')
grid = File.open('aoc.in') do |f|
f.readlines.map do |line|
line.chomp.chars
end
end

def find_start(grid)
grid.size.times.each do |i|
grid[i].size.times.each do |j|
if grid[i][j] == 'S'
return [i, j]
end
end
end
end

start = find_start(grid)
N = 26501365

# for repeats of the map that are not in the same (inter-map) row or column as
# the starting point, it is always sufficient to move to the nearest corner from
# the starting map, then move around in the sidewalks, then move from the corner
# to the ending position

def bfs(grid, start)
this = Set.new
this << start
last = Set.new # to keep track of parity
frontier = Set.new
frontier << start

5000.times do
new_reachable = Set.new
reachable.each do |x, y|
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |dx, dy|
next if grid[(x + dx) % grid.size][(y + dy) % grid[0].size] == '#'
next if reached_last_time.include?([x + dx, y + dy])
new_reachable << [x + dx, y + dy]
end
end

# p new_reachable.size - reachable.size
reached_last_time = reachable
reachable = new_reachable
# p reachable.size
end

reachable.size
end

p bfs(grid, start)

0 comments on commit eb081dc

Please sign in to comment.