Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added day 9 solved in ruby #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions duxy/Day9/eje_one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class EjerOne

def solution(name)
alllines = ""
score = 0
File.open(name, "r") do |f|
f.each_line do |line|
alllines = alllines + line
end
end
numofgroups = 0
groups_open = 0
novalid = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see you are using novalid as a boolean. Using int flags can be quite tough to read.

garbage_open = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

alllines = alllines.split("")
for character in alllines
if novalid == 0
if garbage_open <= 0
if character == "{"
groups_open+= 1
end
if character == "}"
score = score + 1*groups_open
groups_open-= 1
numofgroups+= 1
end
if character == "<"
garbage_open+= 1
end
end
if character == "!"
novalid = 1
end
if character == ">"
garbage_open-= 1
end
else
novalid = 0
end
end
return score
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, big file here. I would recommend you to separate and reuse code. Follow the DRY principle 😉

60 changes: 60 additions & 0 deletions duxy/Day9/eje_two.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class EjerTwo

def solution(name)
alllines = ""

score = 0

File.open(name, "r") do |f|
f.each_line do |line|
alllines = alllines + line
end
end

numofgroups = 0
groups_open = 0
novalid = 0
garbage_open = 0

alllines = alllines.split("")

for character in alllines

if novalid == 0
if garbage_open <= 0
if character == "{"
groups_open+= 1
end

if character == "}"
groups_open-= 1
numofgroups+= 1
end

if character == "<"
garbage_open+= 1
end
else
if character != "!"
score+=1
end
end

if character == "!"
novalid = 1
end

if character == ">"
garbage_open-= 1
score-=1
end

else
novalid = 0
end

end
return score
end

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better spacing in this file 👍

1 change: 1 addition & 0 deletions duxy/Day9/input.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions duxy/Day9/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}{{{}}}{{},{}}{{{},{},{{}}}}{<a>,<a>,<a>,<a>}{{<ab>},{<ab>},{<ab>},{<ab>}}{{<!!>},{<!!>},{<!!>},{<!!>}}{{<a!>},{<a!>},{<a!>},{<ab>}}
1 change: 1 addition & 0 deletions duxy/Day9/input3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{<a>,<a>,<a>,<a>}
1 change: 1 addition & 0 deletions duxy/Day9/input4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<!!>},{<!!>},{<!!>},{<!!>}}
1 change: 1 addition & 0 deletions duxy/Day9/input5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<a!>},{<a!>},{<a!>},{<ab>}}
1 change: 1 addition & 0 deletions duxy/Day9/input6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{},{},{{}}}}
1 change: 1 addition & 0 deletions duxy/Day9/input7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<ab>},{<ab>},{<ab>},{<ab>}}
28 changes: 28 additions & 0 deletions duxy/Day9/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative "eje_one"
require_relative "eje_two"

require "test/unit"

class TestSimpleNumber < Test::Unit::TestCase

def test_simple_ej1
assert_equal(1, EjerOne.new().solution("input3.txt"))
assert_equal(9, EjerOne.new().solution("input4.txt"))
assert_equal(3, EjerOne.new().solution("input5.txt"))
assert_equal(16, EjerOne.new().solution("input6.txt"))
assert_equal(9, EjerOne.new().solution("input7.txt"))
assert_equal(50, EjerOne.new().solution("input2.txt"))
assert_equal(23588, EjerOne.new().solution("input.txt"))
end

def test_simple_ej2
assert_equal(4, EjerTwo.new().solution("input3.txt"))
assert_equal(0, EjerTwo.new().solution("input4.txt"))
assert_equal(17, EjerTwo.new().solution("input5.txt"))
assert_equal(0, EjerTwo.new().solution("input6.txt"))
assert_equal(8, EjerTwo.new().solution("input7.txt"))
assert_equal(29, EjerTwo.new().solution("input2.txt"))
assert_equal(10045, EjerTwo.new().solution("input.txt"))
end

end