Compare commits

...

2 commits

Author SHA1 Message Date
bb2ee520ab
2023: Day 11, part 2 2023-12-11 19:19:40 +10:00
083b47c23b
2023: Day 11, part 1 2023-12-11 18:04:18 +10:00
3 changed files with 223 additions and 0 deletions

72
2023/day11.rb Normal file
View file

@ -0,0 +1,72 @@
MULTIPLIER = ENV["MULTIPLIER"].to_i || 2
input = ARGF.read.strip
by_row = input.lines.map(&:rstrip).map { |line| line.split("") }
def needs_expand(data)
expand = []
data.each_with_index { |row, i| expand << i if row.all? { |c| c == "." || c == "," } }
expand
end
def print_cosmos(cosmos)
cosmos.each do |line|
puts line.join("")
end
end
# do expansion
row_expand = needs_expand(by_row)
row_expand.reverse.each do |i|
by_row[i] = ("," * by_row[i].length).split("") # add expansion marker
end
by_column = by_row.transpose
col_expand = needs_expand(by_column)
col_expand.reverse.each do |i|
by_column[i] = ("," * by_column[i].length).split("") # add expansion marker
end
cosmos = by_column.transpose
print_cosmos(cosmos)
# find galaxies
galaxies = []
(0...cosmos.first.length).each do |x|
(0...cosmos.length).each do |y|
galaxies << [x, y] if cosmos[y][x] == "#"
end
end
def galaxy_distance(a, b)
(a[0] - b[0]).abs + (a[1] - b[1]).abs
end
def cosmos_at(x, y, cosmos)
cosmos[y][x]
end
distances = galaxies.combination(2).map do |combo|
a, b = combo.sort
distance = galaxy_distance(a, b)
# see how many millions are crossed in x
x_millions = (a[0]..b[0]).filter_map do |x|
loc = cosmos_at(x, a[1], cosmos)
loc if loc == ","
end
# now y component
range = if a[1] > b[1]
b[1]..a[1]
else
a[1]..b[1]
end
y_millions = range.filter_map do |y|
loc = cosmos_at(b[0], y, cosmos)
loc if loc == ","
end
distance + ((x_millions.length + y_millions.length) * MULTIPLIER) - x_millions.length - y_millions.length
end
puts "Sum of distances with multiplier #{MULTIPLIER}: #{distances.sum}"

11
2023/input/day11.sample Normal file
View file

@ -0,0 +1,11 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

140
2023/input/day11.txt Normal file
View file

@ -0,0 +1,140 @@
....................................#..............#.................................#......................................................
............#......#..........................................................#.....................................#.................#.....
.........................................................................................#...................#..............................
.....#......................................................#...............................................................................
..........................................................................#........................#.............................#..........
......................#.................#...............................................................................#...................
........#......................................................#...............................#............................................
...........................#.................#.......................................#...................#.........#.................#......
.#................#.......................................#..........#........#.............................................................
............................................................................................................................................
..................................#.........................................................................................................
.........................................................................................#.................................#................
.......#.............#................................#........#.....................................#......................................
..........................................#.................................................................................................
.............#...............................................................................................#..........#...................
....................................#.................................#............#....................#......................#............
............................................................................................#...............................................
........#...........................................#.......#.............................................................................#.
.....................#.......#..................................................#....................................................#......
..............#........................#.......#........................#................................................#..................
.......................................................#..........................................................#.........................
..#..............................................................#..........#.......................#.......................................
..................................................#......................................................#..................................
......................................................................................................................#.....................
.....#................................#...............................................#.....................................................
...................#.....#...............................#.......................#..............#..........................#................
.........#.................................#................................................................................................
..................................................................#.................................#......#.......................#........
............................#........................................................................................#..................#...
.................#...............................#............#............#................................................................
................................#................................................................#..........................................
............................................#.........................................#.......................................#.............
.........#.................................................................................#............................#............#......
..#....................................................#..........#.............#.........................#.................................
...........................#...................#............................................................................................
...................................#...................................#..........................................................#.........
................#..................................................................#..................#..........#..........................
.............................................................#...............................#..............................................
#.................................................................................................#.......................#.................
.........................#...............#..................................................................................................
...........................................................................................................#..................#......#......
.........................................................#.................................................................................#
..............................#.............#......#....................................................................#...................
.....#......................................................................#...............................................................
...............................................................................................................#.......................#....
............#.........#................#...............#............#...........#...........#........................#......................
............................................................................................................................................
................#.................................#............#..........................................................#........#........
...............................#...............................................................#...........................................#
.........................................#.............................#.............................#......................................
...................................................................................#.......................#................................
.........#................#...................#.........................................#...............................#...............#...
....................#................#......................................#.....................#.............#...........................
............................................................................................................................................
......................................................................#.....................................................................
......#..........................................................#........................................................................#.
.............#.........................#.......................................#............................#.....#......#..................
.........................#........#.........#......#.................................#.....#................................................
..#.....................................................#.........................................#.........................................
....................#...............................................#.......................................................................
........................................................................................................#.......................#.......#...
.............................#...................................................#.........................................#................
....................................................................................................#.......................................
.......#..........#..............................................#........#.................#..................#............................
....................................#..........#......#.....#.........................................................#.............#.......
............................................................................................................................................
..............................#................................................................#..........#.................................
..#..............................................................................................................................#..........
..............................................................................................................#.............................
.................................................#........................#...............................................#.................
...................#.....................................................................................................................#..
......#....................#......#.....#................#...................................#.............#................................
.............................................#.......................................#...............................#......................
.................................................................................................#..........................................
...#.................................................................#...........#..........................................................
....................#..............................................................................................................#........
..........#.....................#...........................................................#.............#...............................#.
............................................................................................................................................
..................................................#............................................................#...........#................
...........................................#........................#.........#.....................#.......................................
.#.........................#..............................#.................................................................................
.....................................................................................#.........................................#............
..............................................................................................#......................#...................#..
.....#...........#.....#.....................#..............................................................#...............................
..........#....................................................#.....#......................................................................
#...............................#.....#.....................................................................................................
........................................................#.........................#...................#........#..................#.........
.............................................................................................................................#.............#
.........................................................................#.................................#................................
.........#.......#................................#..............................................#.....................................#....
............................................................................................................................................
................................#............#........................#.........................................#...........................
............#...............................................#........................#................#...............#.....................
..#....................#..........................................#..........................................................#..............
...................................................#......................................................................................#.
................#....................................................................................................................#......
.......#..................................................#...................#.............#..............#................................
.................................................................................................................................#..........
...................#...........#............................................................................................................
.........................................#.....................#....................................#.........#.......#.....................
.#........................................................................#..............................#..................................
............#.....................#.....................................................................................................#...
.......#....................................................................................................................................
.................................................................#............#...................#........................#......#.........
........................................................................................#.......................#...........................
.........................................#.............................#....................................................................
..............#......#...............................................................................................................#......
................................................................................................#......................#....................
............................#............................#..........................#..................#...................................#
...........#...............................................................................#....................................#...........
............................................................................................................................................
.................#.......#............#.......................................................................#.............................
............................................................................................................................................
...#.........................#.................................#...............#............................................#.....#.........
......................................................#...................#..........#.........#.....#....................................#.
...................................#.........#......................................................................#.......................
............................................................................................................................................
..........#..............#...................................#..............................................................................
............................................................................................................................................
.....#...................................................#...............................#...................#..........#..................#
..............................#.......#........#............................................................................................
.................#...............................................................#.................................#........................
.....................................................................#........................................................#.............
............#..................................................................................#............................................
.......................#..................................#..............................................................#.............#....
...................................#........................................................................................................
...........................................................................#.......................................................#........
........#....................#.....................#...............#................................#..............#........................
...#.........#.............................#.........................................#...................#..................................
..............................................................................#..............#........................................#.....
..................................#.......................................................................................#.................
........................#...................................................................................................................
.......................................................#.............................................#............#.........................
............................#........#..............................#.......................................................................
...#........................................................#............#............#............................................#........
.................#..........................#...............................................................................................
..........................................................................................#................................................#
..........#.................................................................................................................................
.........................#............#........................................................#............................................
...............#...................................#......................#..............................#...........#......................