2023 Day 1 in Ruby
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
15
.idea/advent-of-code.iml
generated
Normal file
15
.idea/advent-of-code.iml
generated
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="RUBY_MODULE" version="4">
|
||||||
|
<component name="ModuleRunConfigurationManager">
|
||||||
|
<shared />
|
||||||
|
</component>
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
4
.idea/misc.xml
generated
Normal file
4
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="ruby-3.0.2-p107" project-jdk-type="RUBY_SDK" />
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/advent-of-code.iml" filepath="$PROJECT_DIR$/.idea/advent-of-code.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
0
2023/base/data
Normal file
0
2023/base/data
Normal file
5
2023/base/p1.rb
Normal file
5
2023/base/p1.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
res = 0
|
||||||
|
File.foreach("./2023/base/data") do |line|
|
||||||
|
|
||||||
|
end
|
||||||
|
puts res
|
||||||
5
2023/base/p2.rb
Normal file
5
2023/base/p2.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
res = 0
|
||||||
|
File.foreach("./2023/base/data") do |line|
|
||||||
|
|
||||||
|
end
|
||||||
|
puts res
|
||||||
1000
2023/day1/data
Normal file
1000
2023/day1/data
Normal file
File diff suppressed because it is too large
Load Diff
11
2023/day1/p1.rb
Normal file
11
2023/day1/p1.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
res = 0
|
||||||
|
File.foreach("./2023/day1/data") do |line|
|
||||||
|
first_int = -1
|
||||||
|
last_int = -1
|
||||||
|
line.each_byte do |b|
|
||||||
|
first_int = b - 48 if first_int == -1 and b > 47 and b < 58
|
||||||
|
last_int = b - 48 if b > 47 and b < 58
|
||||||
|
end
|
||||||
|
res += first_int * 10 + last_int
|
||||||
|
end
|
||||||
|
puts res
|
||||||
39
2023/day1/p2.rb
Normal file
39
2023/day1/p2.rb
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
res = 0
|
||||||
|
numbers = %w[one two three four five six seven eight nine]
|
||||||
|
|
||||||
|
File.foreach("./2023/day1/data") do |line|
|
||||||
|
first_digit_index = 10000
|
||||||
|
first_digit = 0
|
||||||
|
last_digit_index = -1
|
||||||
|
last_digit = 0
|
||||||
|
|
||||||
|
(1..9).each do |int|
|
||||||
|
next if (line.index int.to_s).nil?
|
||||||
|
if (line.index int.to_s) < first_digit_index
|
||||||
|
first_digit_index = line.index int.to_s
|
||||||
|
first_digit = int
|
||||||
|
end
|
||||||
|
|
||||||
|
if (line.rindex int.to_s) > last_digit_index
|
||||||
|
last_digit_index = line.rindex int.to_s
|
||||||
|
last_digit = int
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
numbers.each_with_index do |int, index|
|
||||||
|
next if (line.rindex int).nil?
|
||||||
|
if (line.index int) < first_digit_index
|
||||||
|
first_digit_index = line.index int
|
||||||
|
first_digit = index + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if (line.rindex int) > last_digit_index
|
||||||
|
last_digit_index = line.rindex int
|
||||||
|
last_digit = index + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
res += first_digit * 10 + last_digit
|
||||||
|
end
|
||||||
|
|
||||||
|
puts res
|
||||||
Reference in New Issue
Block a user