2023: Day 11, part 1

This commit is contained in:
Wesley Moore 2023-12-11 18:04:18 +10:00
parent 9d3198e24b
commit 083b47c23b
No known key found for this signature in database
3 changed files with 193 additions and 0 deletions

42
2023/day11.rb Normal file
View file

@ -0,0 +1,42 @@
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 == "." } }
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.insert(i, by_row[i].dup)
end
by_column = by_row.transpose
col_expand = needs_expand(by_column)
col_expand.reverse.each do |i|
by_column.insert(i, by_column[i].dup)
end
cosmos = by_column.transpose
# 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
puts "Part 1: #{galaxies.combination(2).map { |a, b| galaxy_distance(a, b) }.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 @@
....................................#..............#.................................#......................................................
............#......#..........................................................#.....................................#.................#.....
.........................................................................................#...................#..............................
.....#......................................................#...............................................................................
..........................................................................#........................#.............................#..........
......................#.................#...............................................................................#...................
........#......................................................#...............................#............................................
...........................#.................#.......................................#...................#.........#.................#......
.#................#.......................................#..........#........#.............................................................
............................................................................................................................................
..................................#.........................................................................................................
.........................................................................................#.................................#................
.......#.............#................................#........#.....................................#......................................
..........................................#.................................................................................................
.............#...............................................................................................#..........#...................
....................................#.................................#............#....................#......................#............
............................................................................................#...............................................
........#...........................................#.......#.............................................................................#.
.....................#.......#..................................................#....................................................#......
..............#........................#.......#........................#................................................#..................
.......................................................#..........................................................#.........................
..#..............................................................#..........#.......................#.......................................
..................................................#......................................................#..................................
......................................................................................................................#.....................
.....#................................#...............................................#.....................................................
...................#.....#...............................#.......................#..............#..........................#................
.........#.................................#................................................................................................
..................................................................#.................................#......#.......................#........
............................#........................................................................................#..................#...
.................#...............................#............#............#................................................................
................................#................................................................#..........................................
............................................#.........................................#.......................................#.............
.........#.................................................................................#............................#............#......
..#....................................................#..........#.............#.........................#.................................
...........................#...................#............................................................................................
...................................#...................................#..........................................................#.........
................#..................................................................#..................#..........#..........................
.............................................................#...............................#..............................................
#.................................................................................................#.......................#.................
.........................#...............#..................................................................................................
...........................................................................................................#..................#......#......
.........................................................#.................................................................................#
..............................#.............#......#....................................................................#...................
.....#......................................................................#...............................................................
...............................................................................................................#.......................#....
............#.........#................#...............#............#...........#...........#........................#......................
............................................................................................................................................
................#.................................#............#..........................................................#........#........
...............................#...............................................................#...........................................#
.........................................#.............................#.............................#......................................
...................................................................................#.......................#................................
.........#................#...................#.........................................#...............................#...............#...
....................#................#......................................#.....................#.............#...........................
............................................................................................................................................
......................................................................#.....................................................................
......#..........................................................#........................................................................#.
.............#.........................#.......................................#............................#.....#......#..................
.........................#........#.........#......#.................................#.....#................................................
..#.....................................................#.........................................#.........................................
....................#...............................................#.......................................................................
........................................................................................................#.......................#.......#...
.............................#...................................................#.........................................#................
....................................................................................................#.......................................
.......#..........#..............................................#........#.................#..................#............................
....................................#..........#......#.....#.........................................................#.............#.......
............................................................................................................................................
..............................#................................................................#..........#.................................
..#..............................................................................................................................#..........
..............................................................................................................#.............................
.................................................#........................#...............................................#.................
...................#.....................................................................................................................#..
......#....................#......#.....#................#...................................#.............#................................
.............................................#.......................................#...............................#......................
.................................................................................................#..........................................
...#.................................................................#...........#..........................................................
....................#..............................................................................................................#........
..........#.....................#...........................................................#.............#...............................#.
............................................................................................................................................
..................................................#............................................................#...........#................
...........................................#........................#.........#.....................#.......................................
.#.........................#..............................#.................................................................................
.....................................................................................#.........................................#............
..............................................................................................#......................#...................#..
.....#...........#.....#.....................#..............................................................#...............................
..........#....................................................#.....#......................................................................
#...............................#.....#.....................................................................................................
........................................................#.........................#...................#........#..................#.........
.............................................................................................................................#.............#
.........................................................................#.................................#................................
.........#.......#................................#..............................................#.....................................#....
............................................................................................................................................
................................#............#........................#.........................................#...........................
............#...............................................#........................#................#...............#.....................
..#....................#..........................................#..........................................................#..............
...................................................#......................................................................................#.
................#....................................................................................................................#......
.......#..................................................#...................#.............#..............#................................
.................................................................................................................................#..........
...................#...........#............................................................................................................
.........................................#.....................#....................................#.........#.......#.....................
.#........................................................................#..............................#..................................
............#.....................#.....................................................................................................#...
.......#....................................................................................................................................
.................................................................#............#...................#........................#......#.........
........................................................................................#.......................#...........................
.........................................#.............................#....................................................................
..............#......#...............................................................................................................#......
................................................................................................#......................#....................
............................#............................#..........................#..................#...................................#
...........#...............................................................................#....................................#...........
............................................................................................................................................
.................#.......#............#.......................................................................#.............................
............................................................................................................................................
...#.........................#.................................#...............#............................................#.....#.........
......................................................#...................#..........#.........#.....#....................................#.
...................................#.........#......................................................................#.......................
............................................................................................................................................
..........#..............#...................................#..............................................................................
............................................................................................................................................
.....#...................................................#...............................#...................#..........#..................#
..............................#.......#........#............................................................................................
.................#...............................................................#.................................#........................
.....................................................................#........................................................#.............
............#..................................................................................#............................................
.......................#..................................#..............................................................#.............#....
...................................#........................................................................................................
...........................................................................#.......................................................#........
........#....................#.....................#...............#................................#..............#........................
...#.........#.............................#.........................................#...................#..................................
..............................................................................#..............#........................................#.....
..................................#.......................................................................................#.................
........................#...................................................................................................................
.......................................................#.............................................#............#.........................
............................#........#..............................#.......................................................................
...#........................................................#............#............#............................................#........
.................#..........................#...............................................................................................
..........................................................................................#................................................#
..........#.................................................................................................................................
.........................#............#........................................................#............................................
...............#...................................#......................#..............................#...........#......................