Created
December 1, 2015 13:02
-
-
Save martinlindhe/f939d45bbc5307c0a454 to your computer and use it in GitHub Desktop.
advent of code - day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoc | |
func CalcFloor(s string) int { | |
res := 0 | |
for i := 0; i < len(s); i++ { | |
if s[i] == '(' { | |
res++ | |
} else if s[i] == ')' { | |
res-- | |
} | |
} | |
return res | |
} | |
func CalcCellarStep(s string) int { | |
res := 0 | |
for i := 0; i < len(s); i++ { | |
if s[i] == '(' { | |
res++ | |
} else if s[i] == ')' { | |
res-- | |
} | |
if res < 0 { | |
return i + 1 | |
} | |
} | |
return 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoc | |
import ( | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestCalcFloor(t *testing.T) { | |
assert.Equal(t, 0, CalcFloor("(())")) | |
assert.Equal(t, 0, CalcFloor("()()")) | |
assert.Equal(t, 3, CalcFloor("(((")) | |
assert.Equal(t, 3, CalcFloor("(()(()(")) | |
assert.Equal(t, 3, CalcFloor("))(((((")) | |
assert.Equal(t, -1, CalcFloor("())")) | |
assert.Equal(t, -1, CalcFloor("))(")) | |
assert.Equal(t, -3, CalcFloor(")))")) | |
assert.Equal(t, -3, CalcFloor(")())())")) | |
} | |
func TestCalcCellarStep(t *testing.T) { | |
assert.Equal(t, 1, CalcCellarStep(")")) | |
assert.Equal(t, 5, CalcCellarStep("()())")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment