Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 269
Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

You are given an string representing the initial conditions of some dominoes. Each element can take one of three values:

`L`, meaning the domino has just been pushed to the left,
`R`, meaning the domino has just been pushed to the right, or
`.`, meaning the domino is standing still.
Determine the orientation of each tile when the dominoes stop falling. Note that if a domino receives a force from the left and right side simultaneously, it will remain upright.

For example, given the string `.L.R....L`, you should return `LL.RRRLLL`.

Given the string `..R...L.L`, you should return `..RR.LLLL`.

Pull request description:
Day269
Resolves #547 .

Patch:
diff --git a/README.md b/README.md
index 4c5f142..287c8ae 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/day269/problem.go b/day269/problem.go
new file mode 100644
index 0000000..bc74ffe
--- /dev/null
+++ b/day269/problem.go
@@ -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)
+}


Test patch:
diff --git a/day269/problem_test.go b/day269/problem_test.go
new file mode 100644
index 0000000..5bc445d
--- /dev/null
+++ b/day269/problem_test.go
@@ -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)
+		}
+	}
+}