@@ -6,21 +6,12 @@ class Day11 < Day # >
6
6
# @example
7
7
# day.part1 #=> 374
8
8
def part1
9
- expanded_universe =
10
- Grid . new (
11
- expand_cosmically (
12
- scan ( input )
13
- )
14
- )
9
+ universe = CosmicExpansion . new ( input )
15
10
16
- galaxies = [ ]
17
- expanded_universe . parse do |coords , value |
18
- galaxies << coords if value == GALAXY
19
- end
20
-
21
- galaxies
11
+ universe
12
+ . galaxies
22
13
. combination ( 2 )
23
- . map { |here , there | expanded_universe . manhattan_distance ( here , there ) }
14
+ . map { |here , there | universe . distance_between ( here , there ) }
24
15
. reduce ( &:+ )
25
16
end
26
17
@@ -29,23 +20,73 @@ def part1
29
20
def part2
30
21
end
31
22
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
32
75
33
76
# @example
34
77
# simple_input = "...\n.#.\n...\n"
35
- # day.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]]
78
+ # universe = CosmicExpansion.new(simple_input)
79
+ # universe.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]]
36
80
def scan ( image )
37
81
image
38
82
. split ( "\n " )
39
83
. map ( &:chars )
40
84
end
41
85
42
- EMPTY_SPACE = '.'
43
- GALAXY = '#'
44
-
45
86
# @example
46
- # scanned_image = day.scan(EXAMPLE_INPUT)
87
+ # scanned_image = day.scan(Day11:: EXAMPLE_INPUT)
47
88
# input_expanded = day.expand_cosmically(scanned_image)
48
- # input_expanded #=> day.scan(EXPANDED_INPUT)
89
+ # input_expanded #=> day.scan(Day11:: EXPANDED_INPUT)
49
90
def expand_cosmically ( scanned_image )
50
91
# dupe empty-space rows
51
92
expanded_rows = [ ]
@@ -65,51 +106,8 @@ def expand_cosmically(scanned_image)
65
106
end
66
107
67
108
# 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
70
110
3 . times { expanded_columns = expanded_columns . transpose }
71
111
expanded_columns
72
112
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
113
end
0 commit comments