aoc_utils 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/aoc_utils.rb +68 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56df3b1a821167f6333889fe6e630c4c6dbc7eaeb07087c58a4d085d30f6e9d2
|
4
|
+
data.tar.gz: 833039ef582e37e2da240f602a55162afc42b341cf3dfb1ba079166dbd2fb677
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fce022c24980b65d710a1c1be66b93074b281b463899b24e8ade14b41e1386dca91c3508f1f31dcb0d8e173cff235c9c243e211f76ff8eeeec7ceedec1fafd9
|
7
|
+
data.tar.gz: 7a5ee3f1297e1d4498b9e53c439f57d4f94bcc63c21bbc71882dc813a155b597ebbaf91f4f3b29bca198613c0653720677af1ffe821312ab4cbf55045a5cd29c
|
data/lib/aoc_utils.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Desc: Utility functions for Advent of Code problems
|
2
|
+
module AocUtils
|
3
|
+
# extracts all integers from the specified file
|
4
|
+
# @param filename [String] the name of the file to read from
|
5
|
+
# @param other_characters [Array<String>] will save the first non integer character in the line for further usage
|
6
|
+
# @return [Array<Array<Integer>>] an array of all the lines read from the file, with each line being an array of the integers read from that line
|
7
|
+
def read_ints(filename, other_characters = [])
|
8
|
+
ints = []
|
9
|
+
File.open(filename).each_line do |line|
|
10
|
+
ints << line.scan(/-?\d+/).map(&:to_i)
|
11
|
+
other_characters << line[/[^0-9\s]/]
|
12
|
+
end
|
13
|
+
ints
|
14
|
+
end
|
15
|
+
|
16
|
+
# extracts all strings from the specified file
|
17
|
+
# @param filename [String] the name of the file to read from
|
18
|
+
# @return [Array<String>] an array of all the lines read from the file as strings with no leading or trailing whitespace
|
19
|
+
def read_strings(filename)
|
20
|
+
strings = []
|
21
|
+
File.open(filename).each_line do |line|
|
22
|
+
strings << line.strip
|
23
|
+
end
|
24
|
+
strings
|
25
|
+
end
|
26
|
+
|
27
|
+
# extracts all characters from the specified file
|
28
|
+
# @param filename [String] the name of the file to read from
|
29
|
+
# @return [Array<Array<String>>] an array of all the lines read from the file, with each line being an array of the characters read from that line with no leading or trailing whitespace
|
30
|
+
def read_chars(filename)
|
31
|
+
chars = []
|
32
|
+
File.open(filename).each_line do |line|
|
33
|
+
chars << line.strip.chars
|
34
|
+
end
|
35
|
+
chars
|
36
|
+
end
|
37
|
+
|
38
|
+
# extracts all lines from the specified file and splits them into two parts based on an empty line
|
39
|
+
# @param filename [String] the name of the file to read from
|
40
|
+
# @param datatype1 [String] the datatype of the first part of the file, either "Integer", "String", or "Char"
|
41
|
+
# @param datatype2 [String] the datatype of the second part of the file, either "Integer", "String", or "Char"
|
42
|
+
# @return [Array<Array>] an array containing two arrays, the first being the first part of the file and the second being the second part of the file
|
43
|
+
def read_two_parts(filename, datatype1, datatype2)
|
44
|
+
lines = File.open(filename).readlines.map(&:strip)
|
45
|
+
index = lines.index("")
|
46
|
+
part1 = lines[0...index]
|
47
|
+
part2 = lines[(index + 1)..-1]
|
48
|
+
case datatype1
|
49
|
+
when "Integer"
|
50
|
+
part1 = part1.map { |line| line.scan(/-?\d+/).map(&:to_i) }
|
51
|
+
when "String"
|
52
|
+
part1 = part1.map(&:strip)
|
53
|
+
when "Char"
|
54
|
+
part1 = part1.map(&:strip.chars)
|
55
|
+
else
|
56
|
+
raise "Invalid datatype"
|
57
|
+
end
|
58
|
+
case datatype2
|
59
|
+
when "Integer"
|
60
|
+
part2 = part2.map { |line| line.scan(/-?\d+/).map(&:to_i) }
|
61
|
+
when "String"
|
62
|
+
part2 = part2.map(&:strip)
|
63
|
+
when "Char"
|
64
|
+
part2 = part2.map(&:chars)
|
65
|
+
end
|
66
|
+
[part1, part2]
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aoc_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lennard Clicqué
|
@@ -15,7 +15,8 @@ email: l.clicque@gmail.com
|
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
|
-
files:
|
18
|
+
files:
|
19
|
+
- lib/aoc_utils.rb
|
19
20
|
homepage: https://rubygems.org/gems/aoc_utils
|
20
21
|
licenses: []
|
21
22
|
metadata: {}
|
@@ -27,7 +28,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
27
28
|
requirements:
|
28
29
|
- - ">="
|
29
30
|
- !ruby/object:Gem::Version
|
30
|
-
version:
|
31
|
+
version: 2.7.0
|
31
32
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
32
33
|
requirements:
|
33
34
|
- - ">="
|