Skip to content

Commit ce010db

Browse files
author
Robb Kidd
committed
refactor into a class to do the maths
Of course my part 1 was too naive for part 2. Refactoring in prep for computing differently.
1 parent 759313f commit ce010db

File tree

1 file changed

+61
-63
lines changed

1 file changed

+61
-63
lines changed

2023/ruby/day11.rb

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@ class Day11 < Day # >
66
# @example
77
# day.part1 #=> 374
88
def part1
9-
expanded_universe =
10-
Grid.new(
11-
expand_cosmically(
12-
scan(input)
13-
)
14-
)
9+
universe = CosmicExpansion.new(input)
1510

16-
galaxies = []
17-
expanded_universe.parse do |coords, value|
18-
galaxies << coords if value == GALAXY
19-
end
20-
21-
galaxies
11+
universe
12+
.galaxies
2213
.combination(2)
23-
.map { |here, there| expanded_universe.manhattan_distance(here, there) }
14+
.map { |here, there| universe.distance_between(here, there) }
2415
.reduce(&:+)
2516
end
2617

@@ -29,23 +20,73 @@ def part1
2920
def part2
3021
end
3122

23+
EXAMPLE_INPUT = <<~INPUT
24+
...#......
25+
.......#..
26+
#.........
27+
..........
28+
......#...
29+
.#........
30+
.........#
31+
..........
32+
.......#..
33+
#...#.....
34+
INPUT
35+
36+
EXPANDED_INPUT = <<~INPUT
37+
....#........
38+
.........#...
39+
#............
40+
.............
41+
.............
42+
........#....
43+
.#...........
44+
............#
45+
.............
46+
.............
47+
.........#...
48+
#....#.......
49+
INPUT
50+
end
51+
52+
class CosmicExpansion
53+
attr_reader :galaxies
54+
55+
def initialize(input)
56+
@input = input
57+
@galaxies = []
58+
@grid =
59+
Grid.new(
60+
expand_cosmically(
61+
scan(input)
62+
)
63+
)
64+
@grid.parse do |coords, value|
65+
galaxies << coords if value == GALAXY
66+
end
67+
end
68+
69+
EMPTY_SPACE = '.'
70+
GALAXY = '#'
71+
72+
def distance_between(here, there)
73+
@grid.manhattan_distance(here, there)
74+
end
3275

3376
# @example
3477
# simple_input = "...\n.#.\n...\n"
35-
# day.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]]
78+
# universe = CosmicExpansion.new(simple_input)
79+
# universe.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]]
3680
def scan(image)
3781
image
3882
.split("\n")
3983
.map(&:chars)
4084
end
4185

42-
EMPTY_SPACE = '.'
43-
GALAXY = '#'
44-
4586
# @example
46-
# scanned_image = day.scan(EXAMPLE_INPUT)
87+
# scanned_image = day.scan(Day11::EXAMPLE_INPUT)
4788
# input_expanded = day.expand_cosmically(scanned_image)
48-
# input_expanded #=> day.scan(EXPANDED_INPUT)
89+
# input_expanded #=> day.scan(Day11::EXPANDED_INPUT)
4990
def expand_cosmically(scanned_image)
5091
# dupe empty-space rows
5192
expanded_rows = []
@@ -65,51 +106,8 @@ def expand_cosmically(scanned_image)
65106
end
66107

67108
# pivot things back around to original orientation
68-
# unnecessary for computing distances, but wanted for
69-
# visualizing output
109+
# gotta get rid of this entirely for part 2, though
70110
3.times { expanded_columns = expanded_columns.transpose }
71111
expanded_columns
72112
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
115113
end

0 commit comments

Comments
 (0)