Skip to content

Commit 759313f

Browse files
author
Robb Kidd
committed
day 11, part 1
Horray for having a grid implementation already.
1 parent 123483e commit 759313f

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

2023/ruby/day11.rb

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
require_relative 'day'
2+
require_relative 'grid'
3+
4+
class Day11 < Day # >
5+
6+
# @example
7+
# day.part1 #=> 374
8+
def part1
9+
expanded_universe =
10+
Grid.new(
11+
expand_cosmically(
12+
scan(input)
13+
)
14+
)
15+
16+
galaxies = []
17+
expanded_universe.parse do |coords, value|
18+
galaxies << coords if value == GALAXY
19+
end
20+
21+
galaxies
22+
.combination(2)
23+
.map { |here, there| expanded_universe.manhattan_distance(here, there) }
24+
.reduce(&:+)
25+
end
26+
27+
# @example
28+
# day.part2 #=> 'how are you'
29+
def part2
30+
end
31+
32+
33+
# @example
34+
# simple_input = "...\n.#.\n...\n"
35+
# day.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]]
36+
def scan(image)
37+
image
38+
.split("\n")
39+
.map(&:chars)
40+
end
41+
42+
EMPTY_SPACE = '.'
43+
GALAXY = '#'
44+
45+
# @example
46+
# scanned_image = day.scan(EXAMPLE_INPUT)
47+
# input_expanded = day.expand_cosmically(scanned_image)
48+
# input_expanded #=> day.scan(EXPANDED_INPUT)
49+
def expand_cosmically(scanned_image)
50+
# dupe empty-space rows
51+
expanded_rows = []
52+
scanned_image
53+
.each do |row|
54+
n = row.all?(EMPTY_SPACE) ? 2 : 1
55+
n.times { expanded_rows << row }
56+
end
57+
58+
# pivot the table, dupe empty-space columns
59+
expanded_columns = []
60+
expanded_rows
61+
.transpose
62+
.each do |column|
63+
n = column.all?(EMPTY_SPACE) ? 2 : 1
64+
n.times { expanded_columns << column }
65+
end
66+
67+
# pivot things back around to original orientation
68+
# unnecessary for computing distances, but wanted for
69+
# visualizing output
70+
3.times { expanded_columns = expanded_columns.transpose }
71+
expanded_columns
72+
end
73+
74+
# @example simple input
75+
# simple_input = "...\n.#.\n...\n"
76+
# scanned_image = day.scan(simple_input)
77+
# scanned_image #=> [[".",".","."],[".","#","."],[".",".","."]]
78+
# day.display(scanned_image) #=> simple_input
79+
# @example example input
80+
# scanned_image = day.scan(EXAMPLE_INPUT)
81+
# day.display(scanned_image) #=> EXAMPLE_INPUT
82+
def display(scanned_image)
83+
scanned_image
84+
.map { |row| row.join('') }
85+
.join("\n") + "\n"
86+
end
87+
88+
EXAMPLE_INPUT = <<~INPUT
89+
...#......
90+
.......#..
91+
#.........
92+
..........
93+
......#...
94+
.#........
95+
.........#
96+
..........
97+
.......#..
98+
#...#.....
99+
INPUT
100+
101+
EXPANDED_INPUT = <<~INPUT
102+
....#........
103+
.........#...
104+
#............
105+
.............
106+
.............
107+
........#....
108+
.#...........
109+
............#
110+
.............
111+
.............
112+
.........#...
113+
#....#.......
114+
INPUT
115+
end

2023/ruby/grid.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,19 @@ def path_to_direction_map(path)
170170
# @example
171171
#
172172
def parse
173-
@input
174-
.split("\n")
175-
.map { |line| line.chars }
173+
split_input =
174+
case @input
175+
when Array # assume the lines and chars have been split already
176+
@input
177+
when String
178+
@input
179+
.split("\n")
180+
.map { |line| line.chars }
181+
else
182+
raise("don't know how to parse #{input.inspect}")
183+
end
184+
185+
split_input
176186
.each_with_index do |row, r|
177187
row.each_with_index do |char, c|
178188
@the_grid[[r, c]] = char

0 commit comments

Comments
 (0)