-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
garbage_open = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😉 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better spacing in this file 👍 |
Large diffs are not rendered by default.
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>}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{<a>,<a>,<a>,<a>} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<!!>},{<!!>},{<!!>},{<!!>}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<a!>},{<a!>},{<a!>},{<ab>}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{},{},{{}}}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<ab>},{<ab>},{<ab>},{<ab>}} |
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 |
There was a problem hiding this comment.
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.