Skip to content

Commit

Permalink
Merge 76c9e07 into f6d47f1
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed May 19, 2019
2 parents f6d47f1 + 76c9e07 commit 407c92e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,4 @@ problems from
* [Day 260](https://github.com/vaskoz/dailycodingproblem-go/issues/533)
* [Day 266](https://github.com/vaskoz/dailycodingproblem-go/issues/542)
* [Day 268](https://github.com/vaskoz/dailycodingproblem-go/issues/544)
* [Day 269](https://github.com/vaskoz/dailycodingproblem-go/issues/547)
33 changes: 33 additions & 0 deletions day269/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day269

// SimulateDominoes runs a simulation of dominoes falling
// based on an initial state. It returns the end state.
func SimulateDominoes(initial string) string {
start := make([]rune, len([]rune(initial)))
copy(start, []rune(initial))
result := make([]rune, len(start))
changed := true
for changed {
changed = false
for i, domino := range start {
if domino == '.' {
switch {
case i != 0 && i != len(start)-1 && start[i-1] == 'R' && start[i+1] == 'L':
result[i] = start[i]
case i != 0 && start[i-1] == 'R':
result[i] = 'R'
changed = true
case i != len(start)-1 && start[i+1] == 'L':
result[i] = 'L'
changed = true
default:
result[i] = start[i]
}
} else {
result[i] = start[i]
}
}
copy(start, result)
}
return string(result)
}
28 changes: 28 additions & 0 deletions day269/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package day269

import "testing"

// nolint
var testcases = []struct {
start, end string
}{
{".L.R....L", "LL.RRRLLL"},
{"..R...L.L", "..RR.LLLL"},
}

func TestSimulateDominoes(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := SimulateDominoes(tc.start); result != tc.end {
t.Errorf("Given %s, expected %s, got %s", tc.start, tc.end, result)
}
}
}

func BenchmarkSimulateDominoes(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
SimulateDominoes(tc.start)
}
}
}

0 comments on commit 407c92e

Please sign in to comment.