aoc_rb_helpers 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +39 -0
- data/aoc_rb_helpers.gemspec +1 -0
- data/lib/aoc_rb_helpers/dot_matrix.rb +151 -129
- data/lib/aoc_rb_helpers/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9049a2637e249afd1d2670f1b4ca3616aa21ef94ae10efb271178693ff16f63
|
4
|
+
data.tar.gz: 485a04d7594ef25fbec3fb2ca19060c8f5699a30fb0845ec9c985a899aae5be4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6343e8180d5ab30baecad403c7765b4a666562a713154541c70ee6a602ecaa3253c08a25f89db76d36e4e86371901564b34eb7ba779fbbaa2e011cb543edf10a
|
7
|
+
data.tar.gz: dade32999ee76bbda52f4615cad719ab2dc4ab2209dccf88b6756b0f63d125d7dfe40b76d61e85245c599b472b29e426c5dd110b91c5c3eb46bd8ffd1ab96f84
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
## [Unreleased]
|
8
8
|
- No unreleased changes!
|
9
9
|
|
10
|
+
## [0.0.3]
|
11
|
+
### Added
|
12
|
+
- Characters `I` and `T` now supported by DotMatrix
|
13
|
+
- Updated README with links to documentation
|
14
|
+
- Added documentation link to gemspec
|
15
|
+
|
10
16
|
## [0.0.2]
|
11
17
|
### Added
|
12
18
|
- DotMatrix class for decoding printed puzzle output
|
data/README.md
CHANGED
@@ -35,6 +35,29 @@ pd some_object
|
|
35
35
|
|
36
36
|
You can read more on how you can use `pd` in their [README](https://github.com/AndyObtiva/puts_debuggerer/blob/master/README.md).
|
37
37
|
|
38
|
+
## Provided Helper Classes
|
39
|
+
|
40
|
+
All documentation is available here - https://rubydoc.info/github/pacso/aoc_rb_helpers.
|
41
|
+
|
42
|
+
The provided helper classes are as follows:
|
43
|
+
|
44
|
+
### [AocInput](https://rubydoc.info/github/pacso/aoc_rb_helpers/AocInput)
|
45
|
+
Provides input manipulation helper methods. Methods are chainable, and directly modify the parsed view of the input data within the `@data` instance variable.
|
46
|
+
|
47
|
+
### [DotMatrix](https://rubydoc.info/github/pacso/aoc_rb_helpers/DotMatrix)
|
48
|
+
Parses and decodes ASCII art text from puzzles. Can output to STDOUT or return the result to your code.
|
49
|
+
|
50
|
+
Will turn an input like:
|
51
|
+
```ruby
|
52
|
+
XX XXXX X XXXX X XX X XXXXX XX XXX
|
53
|
+
X X X X X X X X X XX X X X
|
54
|
+
X XXX X XXX X X X X X XXX X X
|
55
|
+
X X X X X X X X X X XX
|
56
|
+
X X X X X X X X X X X X X
|
57
|
+
XX X XXXX XXXX XXXX XX X X XX XXX
|
58
|
+
```
|
59
|
+
Into the string `CFLELOYFCS`.
|
60
|
+
|
38
61
|
## Examples
|
39
62
|
|
40
63
|
Below are some examples of how you can use the features of this gem.
|
@@ -71,3 +94,19 @@ module Year2024
|
|
71
94
|
end
|
72
95
|
end
|
73
96
|
```
|
97
|
+
|
98
|
+
### Decoding printed text
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
text_input = <<~EOF
|
102
|
+
X X XXXX X X XX
|
103
|
+
X X X X X X X
|
104
|
+
XXXX XXX X X X X
|
105
|
+
X X X X X X X
|
106
|
+
X X X X X X X
|
107
|
+
X X XXXX XXXX XXXX XX
|
108
|
+
EOF
|
109
|
+
|
110
|
+
input_array = text_input.lines(chomp: true).map(&:chars)
|
111
|
+
DotMatrix.decode(input_array) # returns the string "HELLO"
|
112
|
+
```
|
data/aoc_rb_helpers.gemspec
CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new("~> 3.1", ">= 3.1.0")
|
14
14
|
|
15
15
|
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["documentation_uri"] = "https://rubydoc.info/github/pacso/aoc_rb_helpers"
|
16
17
|
spec.metadata["changelog_uri"] = "https://github.com/pacso/aoc_rb_helpers/blob/main/CHANGELOG"
|
17
18
|
|
18
19
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
@@ -1,127 +1,151 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# Parses and decodes ASCII art text from puzzles. Can output to STDOUT or return the result to your code.
|
3
4
|
class DotMatrix
|
4
|
-
CHAR_WIDTH = 5
|
5
5
|
DICTIONARY = {
|
6
6
|
a: [
|
7
|
-
" XX
|
8
|
-
"X X
|
9
|
-
"X X
|
10
|
-
"XXXX
|
11
|
-
"X X
|
12
|
-
"X X
|
7
|
+
" XX ",
|
8
|
+
"X X",
|
9
|
+
"X X",
|
10
|
+
"XXXX",
|
11
|
+
"X X",
|
12
|
+
"X X"
|
13
13
|
],
|
14
14
|
b: [
|
15
|
-
"XXX
|
16
|
-
"X X
|
17
|
-
"XXX
|
18
|
-
"X X
|
19
|
-
"X X
|
20
|
-
"XXX
|
15
|
+
"XXX ",
|
16
|
+
"X X",
|
17
|
+
"XXX ",
|
18
|
+
"X X",
|
19
|
+
"X X",
|
20
|
+
"XXX "
|
21
21
|
],
|
22
22
|
c: [
|
23
|
-
" XX
|
24
|
-
"X X
|
25
|
-
"X
|
26
|
-
"X
|
27
|
-
"X X
|
28
|
-
" XX
|
23
|
+
" XX ",
|
24
|
+
"X X",
|
25
|
+
"X ",
|
26
|
+
"X ",
|
27
|
+
"X X",
|
28
|
+
" XX "
|
29
|
+
],
|
30
|
+
d: [
|
31
|
+
"XXX ",
|
32
|
+
"X X",
|
33
|
+
"X X",
|
34
|
+
"X X",
|
35
|
+
"X X",
|
36
|
+
"XXX "
|
29
37
|
],
|
30
38
|
e: [
|
31
|
-
"XXXX
|
32
|
-
"X
|
33
|
-
"XXX
|
34
|
-
"X
|
35
|
-
"X
|
36
|
-
"XXXX
|
39
|
+
"XXXX",
|
40
|
+
"X ",
|
41
|
+
"XXX ",
|
42
|
+
"X ",
|
43
|
+
"X ",
|
44
|
+
"XXXX"
|
37
45
|
],
|
38
46
|
f: [
|
39
|
-
"XXXX
|
40
|
-
"X
|
41
|
-
"XXX
|
42
|
-
"X
|
43
|
-
"X
|
44
|
-
"X
|
47
|
+
"XXXX",
|
48
|
+
"X ",
|
49
|
+
"XXX ",
|
50
|
+
"X ",
|
51
|
+
"X ",
|
52
|
+
"X "
|
45
53
|
],
|
46
54
|
g: [
|
47
|
-
" XX
|
48
|
-
"X X
|
49
|
-
"X
|
50
|
-
"X XX
|
51
|
-
"X X
|
52
|
-
" XXX
|
55
|
+
" XX ",
|
56
|
+
"X X",
|
57
|
+
"X ",
|
58
|
+
"X XX",
|
59
|
+
"X X",
|
60
|
+
" XXX"
|
53
61
|
],
|
54
62
|
h: [
|
55
|
-
"X X
|
56
|
-
"X X
|
57
|
-
"XXXX
|
58
|
-
"X X
|
59
|
-
"X X
|
60
|
-
"X X
|
63
|
+
"X X",
|
64
|
+
"X X",
|
65
|
+
"XXXX",
|
66
|
+
"X X",
|
67
|
+
"X X",
|
68
|
+
"X X",
|
69
|
+
],
|
70
|
+
i: [
|
71
|
+
"XXX",
|
72
|
+
" X ",
|
73
|
+
" X ",
|
74
|
+
" X ",
|
75
|
+
" X ",
|
76
|
+
"XXX"
|
61
77
|
],
|
62
78
|
j: [
|
63
|
-
" XX
|
64
|
-
" X
|
65
|
-
" X
|
66
|
-
" X
|
67
|
-
"X X
|
68
|
-
" XX
|
79
|
+
" XX",
|
80
|
+
" X",
|
81
|
+
" X",
|
82
|
+
" X",
|
83
|
+
"X X",
|
84
|
+
" XX ",
|
69
85
|
],
|
70
86
|
k: [
|
71
|
-
"X X
|
72
|
-
"X X
|
73
|
-
"XX
|
74
|
-
"X X
|
75
|
-
"X X
|
76
|
-
"X X
|
87
|
+
"X X",
|
88
|
+
"X X ",
|
89
|
+
"XX ",
|
90
|
+
"X X ",
|
91
|
+
"X X ",
|
92
|
+
"X X",
|
77
93
|
],
|
78
94
|
l: [
|
79
|
-
"X
|
80
|
-
"X
|
81
|
-
"X
|
82
|
-
"X
|
83
|
-
"X
|
84
|
-
"XXXX
|
95
|
+
"X ",
|
96
|
+
"X ",
|
97
|
+
"X ",
|
98
|
+
"X ",
|
99
|
+
"X ",
|
100
|
+
"XXXX",
|
85
101
|
],
|
86
102
|
o: [
|
87
|
-
" XX
|
88
|
-
"X X
|
89
|
-
"X X
|
90
|
-
"X X
|
91
|
-
"X X
|
92
|
-
" XX
|
103
|
+
" XX ",
|
104
|
+
"X X",
|
105
|
+
"X X",
|
106
|
+
"X X",
|
107
|
+
"X X",
|
108
|
+
" XX "
|
93
109
|
],
|
94
110
|
p: [
|
95
|
-
"XXX
|
96
|
-
"X X
|
97
|
-
"X X
|
98
|
-
"XXX
|
99
|
-
"X
|
100
|
-
"X
|
111
|
+
"XXX ",
|
112
|
+
"X X",
|
113
|
+
"X X",
|
114
|
+
"XXX ",
|
115
|
+
"X ",
|
116
|
+
"X ",
|
101
117
|
],
|
102
118
|
r: [
|
103
|
-
"XXX
|
104
|
-
"X X
|
105
|
-
"X X
|
106
|
-
"XXX
|
107
|
-
"X X
|
108
|
-
"X X
|
119
|
+
"XXX ",
|
120
|
+
"X X",
|
121
|
+
"X X",
|
122
|
+
"XXX ",
|
123
|
+
"X X ",
|
124
|
+
"X X",
|
109
125
|
],
|
110
126
|
s: [
|
111
|
-
" XXX
|
112
|
-
"X
|
113
|
-
"X
|
114
|
-
" XX
|
115
|
-
" X
|
116
|
-
"XXX
|
127
|
+
" XXX",
|
128
|
+
"X ",
|
129
|
+
"X ",
|
130
|
+
" XX ",
|
131
|
+
" X",
|
132
|
+
"XXX ",
|
133
|
+
],
|
134
|
+
t: [
|
135
|
+
"XXX",
|
136
|
+
" X ",
|
137
|
+
" X ",
|
138
|
+
" X ",
|
139
|
+
" X ",
|
140
|
+
" X "
|
117
141
|
],
|
118
142
|
u: [
|
119
|
-
"X X
|
120
|
-
"X X
|
121
|
-
"X X
|
122
|
-
"X X
|
123
|
-
"X X
|
124
|
-
" XX
|
143
|
+
"X X",
|
144
|
+
"X X",
|
145
|
+
"X X",
|
146
|
+
"X X",
|
147
|
+
"X X",
|
148
|
+
" XX ",
|
125
149
|
],
|
126
150
|
y: [
|
127
151
|
"X X",
|
@@ -132,12 +156,12 @@ class DotMatrix
|
|
132
156
|
" X "
|
133
157
|
],
|
134
158
|
z: [
|
135
|
-
"XXXX
|
136
|
-
" X
|
137
|
-
" X
|
138
|
-
" X
|
139
|
-
"X
|
140
|
-
"XXXX
|
159
|
+
"XXXX",
|
160
|
+
" X",
|
161
|
+
" X ",
|
162
|
+
" X ",
|
163
|
+
"X ",
|
164
|
+
"XXXX",
|
141
165
|
]
|
142
166
|
}
|
143
167
|
|
@@ -157,6 +181,8 @@ class DotMatrix
|
|
157
181
|
@input = input
|
158
182
|
@on = on
|
159
183
|
@off = off
|
184
|
+
|
185
|
+
convert_characters
|
160
186
|
end
|
161
187
|
|
162
188
|
# Prints the input array to STDOUT and returns self.
|
@@ -178,25 +204,18 @@ class DotMatrix
|
|
178
204
|
# Returns the decoded input.
|
179
205
|
# @return [String] the decoded input
|
180
206
|
def to_s
|
181
|
-
|
207
|
+
cursor = 0
|
208
|
+
decoded = ""
|
209
|
+
while cursor < max_cursor
|
182
210
|
begin
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
unless @on == "X" && @off == " "
|
188
|
-
new_map = []
|
189
|
-
char_map.each do |row|
|
190
|
-
row_chars = []
|
191
|
-
row.chars.each do |cell|
|
192
|
-
row_chars.push(cell == @on ? "X" : " ")
|
193
|
-
end
|
194
|
-
new_map.push(row_chars.join)
|
195
|
-
end
|
196
|
-
char_map = new_map
|
211
|
+
cursor += 1 while @input.all? { |row| row[cursor] == ' ' }
|
212
|
+
cursor_end = cursor_range(cursor)
|
213
|
+
char_map = @input.map do |row|
|
214
|
+
row[cursor...cursor_end].join
|
197
215
|
end
|
216
|
+
cursor = cursor_end
|
198
217
|
|
199
|
-
DICTIONARY.find { |_, map| map == char_map }[0].to_s.upcase
|
218
|
+
decoded += DICTIONARY.find { |_, map| map == char_map }[0].to_s.upcase
|
200
219
|
rescue NoMethodError
|
201
220
|
puts "ERROR: Missing character in dictionary:\n\n"
|
202
221
|
print(char_map)
|
@@ -204,31 +223,34 @@ class DotMatrix
|
|
204
223
|
print
|
205
224
|
break
|
206
225
|
end
|
207
|
-
end
|
208
|
-
end
|
226
|
+
end
|
209
227
|
|
210
|
-
|
211
|
-
def char_offset(index)
|
212
|
-
index * CHAR_WIDTH
|
228
|
+
decoded
|
213
229
|
end
|
214
230
|
|
215
|
-
|
216
|
-
row[first_position..last_position]
|
217
|
-
end
|
231
|
+
private
|
218
232
|
|
219
|
-
def
|
220
|
-
@
|
233
|
+
def convert_characters
|
234
|
+
unless @on == "X" && @off == " "
|
235
|
+
@input = @input.map do |row|
|
236
|
+
row.map do |cell|
|
237
|
+
cell == @on ? "X" : " "
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
221
241
|
end
|
222
242
|
|
223
|
-
def
|
224
|
-
|
243
|
+
def cursor_range(cursor)
|
244
|
+
c = cursor
|
245
|
+
c += 1 while @input.any? { |row| row[c] != ' ' } && c - cursor < max_cursor_range
|
246
|
+
c
|
225
247
|
end
|
226
248
|
|
227
|
-
def
|
228
|
-
@input.
|
249
|
+
def max_cursor
|
250
|
+
@input.first.length - 4
|
229
251
|
end
|
230
252
|
|
231
|
-
def
|
232
|
-
|
253
|
+
def max_cursor_range
|
254
|
+
DICTIONARY.values.map { |v| v.first.length }.max
|
233
255
|
end
|
234
256
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aoc_rb_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Pascoe
|
@@ -77,6 +77,7 @@ licenses:
|
|
77
77
|
- MIT
|
78
78
|
metadata:
|
79
79
|
homepage_uri: https://github.com/pacso/aoc_rb_helpers
|
80
|
+
documentation_uri: https://rubydoc.info/github/pacso/aoc_rb_helpers
|
80
81
|
changelog_uri: https://github.com/pacso/aoc_rb_helpers/blob/main/CHANGELOG
|
81
82
|
post_install_message:
|
82
83
|
rdoc_options: []
|