File tree 2 files changed +184
-0
lines changed
2 files changed +184
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'numo/narray'
4
+
5
+ class Map
6
+ attr_reader :antennae , :map , :antinodes
7
+
8
+ def initialize ( input )
9
+ @height = input . size
10
+ @width = input [ 0 ] . size
11
+ @antennae = Hash . new [ ]
12
+ @map = Numo ::UInt8 . zeros ( @height , @width )
13
+ @antinodes = Numo ::UInt8 . zeros ( @height , @width )
14
+ @height . times do |y |
15
+ @width . times do |x |
16
+ case input [ y ] [ x ]
17
+ when '.'
18
+ @map [ y , x ] = 0
19
+ else
20
+ @map [ y , x ] = input [ y ] [ x ] . codepoints [ 0 ]
21
+ @antennae [ input [ y ] [ x ] . codepoints [ 0 ] ] += [ [ y , x ] ]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def find_antinodes ( a , b )
28
+ possibles = [
29
+ [
30
+ a [ 0 ] + ( b [ 0 ] - a [ 0 ] ) * 2 ,
31
+ a [ 1 ] + ( b [ 1 ] - a [ 1 ] ) * 2
32
+ ] ,
33
+ [
34
+ b [ 0 ] + ( a [ 0 ] - b [ 0 ] ) * 2 ,
35
+ b [ 1 ] + ( a [ 1 ] - b [ 1 ] ) * 2
36
+ ]
37
+ ]
38
+
39
+ possibles . reject { |p | p [ 0 ] < 0 || p [ 0 ] >= @height || p [ 1 ] < 0 || p [ 1 ] >= @width }
40
+ end
41
+
42
+ def fill!
43
+ antennae . each_value do |ants |
44
+ ants . combination ( 2 ) do |pair |
45
+ find_antinodes ( pair [ 0 ] , pair [ 1 ] ) . each do |a |
46
+ @antinodes [ a [ 0 ] , a [ 1 ] ] += 1
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def score
53
+ count = 0
54
+ @height . times do |y |
55
+ @width . times do |x |
56
+ count += 1 if @antinodes [ y , x ] . positive?
57
+ end
58
+ end
59
+ count
60
+ end
61
+
62
+ def inspect
63
+ to_s
64
+ end
65
+
66
+ def to_s
67
+ s = "<#{ self . class } :\n "
68
+ @height . times do |y |
69
+ @width . times do |x |
70
+ if @map [ y , x ] != 0
71
+ s << @map [ y , x ]
72
+ elsif @antinodes [ y , x ] != 0
73
+ s += '#'
74
+ else
75
+ s += '.'
76
+ end
77
+ end
78
+ s += "\n "
79
+ end
80
+ s += ">"
81
+ s
82
+ end
83
+ end
84
+
85
+ map = Map . new File . read ( '08.input' ) . lines . map ( &:strip )
86
+ map . fill!
87
+ puts map . score
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'numo/narray'
4
+
5
+ class Map
6
+ attr_reader :antennae , :map , :antinodes
7
+
8
+ def initialize ( input )
9
+ @height = input . size
10
+ @width = input [ 0 ] . size
11
+ @antennae = Hash . new [ ]
12
+ @map = Numo ::UInt8 . zeros ( @height , @width )
13
+ @antinodes = Numo ::UInt8 . zeros ( @height , @width )
14
+ @height . times do |y |
15
+ @width . times do |x |
16
+ case input [ y ] [ x ]
17
+ when '.'
18
+ @map [ y , x ] = 0
19
+ else
20
+ @map [ y , x ] = input [ y ] [ x ] . codepoints [ 0 ]
21
+ @antennae [ input [ y ] [ x ] . codepoints [ 0 ] ] += [ [ y , x ] ]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def find_antinodes ( a , b )
28
+ dy = b [ 0 ] - a [ 0 ]
29
+ dx = b [ 1 ] - a [ 1 ]
30
+
31
+ possibles = [ a , b ]
32
+
33
+ y = a [ 0 ] + dy * 2
34
+ x = a [ 1 ] + dx * 2
35
+ while y < @height && x < @width && y >= 0 && x >= 0
36
+ possibles << [ y , x ]
37
+ y += dy
38
+ x += dx
39
+ end
40
+
41
+ y = a [ 0 ] - dy
42
+ x = a [ 1 ] - dx
43
+ while y < @height && x < @width && y >= 0 && x >= 0
44
+ possibles << [ y , x ]
45
+ y -= dy
46
+ x -= dx
47
+ end
48
+
49
+ possibles
50
+ end
51
+
52
+ def fill!
53
+ antennae . each_value do |ants |
54
+ ants . combination ( 2 ) do |pair |
55
+ find_antinodes ( pair [ 0 ] , pair [ 1 ] ) . each do |a |
56
+ @antinodes [ a [ 0 ] , a [ 1 ] ] += 1
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def score
63
+ count = 0
64
+ @height . times do |y |
65
+ @width . times do |x |
66
+ count += 1 if @antinodes [ y , x ] . positive?
67
+ end
68
+ end
69
+ count
70
+ end
71
+
72
+ def inspect
73
+ to_s
74
+ end
75
+
76
+ def to_s
77
+ s = "<#{ self . class } :\n "
78
+ @height . times do |y |
79
+ @width . times do |x |
80
+ if @map [ y , x ] != 0
81
+ s << @map [ y , x ]
82
+ elsif @antinodes [ y , x ] != 0
83
+ s += '#'
84
+ else
85
+ s += '.'
86
+ end
87
+ end
88
+ s += "\n "
89
+ end
90
+ s += ">"
91
+ s
92
+ end
93
+ end
94
+
95
+ map = Map . new File . read ( '08.input' ) . lines . map ( &:strip )
96
+ map . fill!
97
+ puts map . score
You can’t perform that action at this time.
0 commit comments