diff --git a/2023/day11.rb b/2023/day11.rb new file mode 100644 index 0000000..521e3ed --- /dev/null +++ b/2023/day11.rb @@ -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}" diff --git a/2023/input/day11.sample b/2023/input/day11.sample new file mode 100644 index 0000000..5f0a689 --- /dev/null +++ b/2023/input/day11.sample @@ -0,0 +1,11 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... + diff --git a/2023/input/day11.txt b/2023/input/day11.txt new file mode 100644 index 0000000..3fbbb0e --- /dev/null +++ b/2023/input/day11.txt @@ -0,0 +1,140 @@ +....................................#..............#.................................#...................................................... +............#......#..........................................................#.....................................#.................#..... +.........................................................................................#...................#.............................. +.....#......................................................#............................................................................... +..........................................................................#........................#.............................#.......... +......................#.................#...............................................................................#................... +........#......................................................#...............................#............................................ +...........................#.................#.......................................#...................#.........#.................#...... +.#................#.......................................#..........#........#............................................................. +............................................................................................................................................ +..................................#......................................................................................................... +.........................................................................................#.................................#................ +.......#.............#................................#........#.....................................#...................................... +..........................................#................................................................................................. +.............#...............................................................................................#..........#................... +....................................#.................................#............#....................#......................#............ +............................................................................................#............................................... +........#...........................................#.......#.............................................................................#. +.....................#.......#..................................................#....................................................#...... +..............#........................#.......#........................#................................................#.................. +.......................................................#..........................................................#......................... +..#..............................................................#..........#.......................#....................................... +..................................................#......................................................#.................................. +......................................................................................................................#..................... +.....#................................#...............................................#..................................................... +...................#.....#...............................#.......................#..............#..........................#................ +.........#.................................#................................................................................................ +..................................................................#.................................#......#.......................#........ +............................#........................................................................................#..................#... +.................#...............................#............#............#................................................................ +................................#................................................................#.......................................... +............................................#.........................................#.......................................#............. +.........#.................................................................................#............................#............#...... +..#....................................................#..........#.............#.........................#................................. +...........................#...................#............................................................................................ +...................................#...................................#..........................................................#......... +................#..................................................................#..................#..........#.......................... +.............................................................#...............................#.............................................. +#.................................................................................................#.......................#................. +.........................#...............#.................................................................................................. +...........................................................................................................#..................#......#...... +.........................................................#.................................................................................# +..............................#.............#......#....................................................................#................... +.....#......................................................................#............................................................... +...............................................................................................................#.......................#.... +............#.........#................#...............#............#...........#...........#........................#...................... +............................................................................................................................................ +................#.................................#............#..........................................................#........#........ +...............................#...............................................................#...........................................# +.........................................#.............................#.............................#...................................... +...................................................................................#.......................#................................ +.........#................#...................#.........................................#...............................#...............#... +....................#................#......................................#.....................#.............#........................... +............................................................................................................................................ +......................................................................#..................................................................... +......#..........................................................#........................................................................#. +.............#.........................#.......................................#............................#.....#......#.................. +.........................#........#.........#......#.................................#.....#................................................ +..#.....................................................#.........................................#......................................... +....................#...............................................#....................................................................... +........................................................................................................#.......................#.......#... +.............................#...................................................#.........................................#................ +....................................................................................................#....................................... +.......#..........#..............................................#........#.................#..................#............................ +....................................#..........#......#.....#.........................................................#.............#....... +............................................................................................................................................ +..............................#................................................................#..........#................................. +..#..............................................................................................................................#.......... +..............................................................................................................#............................. +.................................................#........................#...............................................#................. +...................#.....................................................................................................................#.. +......#....................#......#.....#................#...................................#.............#................................ +.............................................#.......................................#...............................#...................... +.................................................................................................#.......................................... +...#.................................................................#...........#.......................................................... +....................#..............................................................................................................#........ +..........#.....................#...........................................................#.............#...............................#. +............................................................................................................................................ +..................................................#............................................................#...........#................ +...........................................#........................#.........#.....................#....................................... +.#.........................#..............................#................................................................................. +.....................................................................................#.........................................#............ +..............................................................................................#......................#...................#.. +.....#...........#.....#.....................#..............................................................#............................... +..........#....................................................#.....#...................................................................... +#...............................#.....#..................................................................................................... +........................................................#.........................#...................#........#..................#......... +.............................................................................................................................#.............# +.........................................................................#.................................#................................ +.........#.......#................................#..............................................#.....................................#.... +............................................................................................................................................ +................................#............#........................#.........................................#........................... +............#...............................................#........................#................#...............#..................... +..#....................#..........................................#..........................................................#.............. +...................................................#......................................................................................#. +................#....................................................................................................................#...... +.......#..................................................#...................#.............#..............#................................ +.................................................................................................................................#.......... +...................#...........#............................................................................................................ +.........................................#.....................#....................................#.........#.......#..................... +.#........................................................................#..............................#.................................. +............#.....................#.....................................................................................................#... +.......#.................................................................................................................................... +.................................................................#............#...................#........................#......#......... +........................................................................................#.......................#........................... +.........................................#.............................#.................................................................... +..............#......#...............................................................................................................#...... +................................................................................................#......................#.................... +............................#............................#..........................#..................#...................................# +...........#...............................................................................#....................................#........... +............................................................................................................................................ +.................#.......#............#.......................................................................#............................. +............................................................................................................................................ +...#.........................#.................................#...............#............................................#.....#......... +......................................................#...................#..........#.........#.....#....................................#. +...................................#.........#......................................................................#....................... +............................................................................................................................................ +..........#..............#...................................#.............................................................................. +............................................................................................................................................ +.....#...................................................#...............................#...................#..........#..................# +..............................#.......#........#............................................................................................ +.................#...............................................................#.................................#........................ +.....................................................................#........................................................#............. +............#..................................................................................#............................................ +.......................#..................................#..............................................................#.............#.... +...................................#........................................................................................................ +...........................................................................#.......................................................#........ +........#....................#.....................#...............#................................#..............#........................ +...#.........#.............................#.........................................#...................#.................................. +..............................................................................#..............#........................................#..... +..................................#.......................................................................................#................. +........................#................................................................................................................... +.......................................................#.............................................#............#......................... +............................#........#..............................#....................................................................... +...#........................................................#............#............#............................................#........ +.................#..........................#............................................................................................... +..........................................................................................#................................................# +..........#................................................................................................................................. +.........................#............#........................................................#............................................ +...............#...................................#......................#..............................#...........#......................