-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day362 | ||
package day402 | ||
|
||
import ( | ||
"fmt" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day362 | ||
package day402 | ||
|
||
import ( | ||
"reflect" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day45 | ||
package day403 | ||
|
||
import ( | ||
"math/rand" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day45 | ||
package day403 | ||
|
||
import "testing" | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day21 | ||
package day404 | ||
|
||
import "sort" | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day21 | ||
package day404 | ||
|
||
import "testing" | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day93 | ||
package day405 | ||
|
||
// BinaryTree is a binary tree: left & right children. | ||
type BinaryTree struct { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package day93 | ||
package day405 | ||
|
||
import "testing" | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package day406 | ||
|
||
// CountParensEqualTrue returns the number of possible | ||
// paren patterns that evaluate to True. | ||
// Runs in O(N^3) time and O(N^2) extra space. | ||
func CountParensEqualTrue(input []rune) int { | ||
var sym, ops []rune | ||
|
||
for i := range input { | ||
if i%2 == 0 { | ||
sym = append(sym, input[i]) | ||
} else { | ||
ops = append(ops, input[i]) | ||
} | ||
} | ||
|
||
f := make([][]int, len(sym)) | ||
t := make([][]int, len(sym)) | ||
|
||
for i := range f { | ||
f[i] = make([]int, len(sym)) | ||
t[i] = make([]int, len(sym)) | ||
|
||
if sym[i] == 'F' { | ||
f[i][i] = 1 | ||
} | ||
|
||
if sym[i] == 'T' { | ||
t[i][i] = 1 | ||
} | ||
} | ||
|
||
for gap := 1; gap < len(sym); gap++ { | ||
for i, j := 0, gap; j < len(sym); i, j = i+1, j+1 { | ||
t[i][j] = 0 | ||
f[i][j] = 0 | ||
|
||
for g := 0; g < gap; g++ { | ||
k := i + g | ||
tik := t[i][k] + f[i][k] | ||
tkj := t[k+1][j] + f[k+1][j] | ||
|
||
if ops[k] == '&' { | ||
t[i][j] += t[i][k] * t[k+1][j] | ||
f[i][j] += tik*tkj - t[i][k]*t[k+1][j] | ||
} | ||
|
||
if ops[k] == '|' { | ||
f[i][j] += f[i][k] * f[k+1][j] | ||
t[i][j] += tik*tkj - f[i][k]*f[k+1][j] | ||
} | ||
|
||
if ops[k] == '^' { | ||
t[i][j] += f[i][k]*t[k+1][j] + t[i][k]*f[k+1][j] | ||
f[i][j] += t[i][k]*t[k+1][j] + f[i][k]*f[k+1][j] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return t[0][len(t[0])-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package day406 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input []rune | ||
expected int | ||
}{ | ||
{[]rune{'F', '|', 'T', '&', 'T'}, 2}, | ||
{[]rune{'T', '|', 'T', '&', 'F', '^', 'T'}, 4}, | ||
} | ||
|
||
func TestCountParensEqualTrue(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if result := CountParensEqualTrue(tc.input); result != tc.expected { | ||
t.Errorf("Expected %v, got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkCountParensEqualTrue(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
CountParensEqualTrue(tc.input) | ||
} | ||
} | ||
} |