Skip to content

Commit

Permalink
Merge 9404928 into 348931f
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Oct 4, 2018
2 parents 348931f + 9404928 commit 6f3c1cf
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 42 deletions.
2 changes: 1 addition & 1 deletion day10/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewScheduler() *Scheduler {
for j := range s.queue {
s.wg.Add(1)
go func(j job) {
ms, _ := time.ParseDuration(fmt.Sprintf("%dms", j.n))
ms, _ := time.ParseDuration(fmt.Sprintf("%dms", j.n)) // nolint: gosec
time.Sleep(ms)
log.Printf("starting job at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds()))
j.f()
Expand Down
49 changes: 38 additions & 11 deletions day25/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,55 @@ package day25
// Match returns if there is a regex match on the given input.
func Match(input, pattern string) bool {
var ii, pi, missed int
for ii < len(input) && pi < len(pattern) && missed < 2 {
if pattern[pi] == '*' {
prev := pattern[pi-1]
var next byte
if pi+1 < len(pattern) {
next = pattern[pi+1]
inputR := []rune(input)
patternR := []rune(pattern)
for isPossible(ii, len(inputR), pi, len(patternR), missed) {
if patternR[pi] == '*' {
prev := patternR[pi-1]
var next rune
if pi+1 < len(patternR) {
next = patternR[pi+1]
}
for ii < len(input) && input[ii] != next {
if prev != '.' && input[ii] != prev {
for shouldKeepLooking(ii, len(inputR), inputR, next) {
if prev != '.' && inputR[ii] != prev {
return false
}
ii++
}
if ii == len(input) && pi < len(pattern)-1 {
if remainingPattern(ii, len(inputR), pi, len(patternR)) {
return false
}
ii--
} else if pattern[pi] != '.' && input[ii] != pattern[pi] {
} else if missedMatch(patternR[pi], inputR[ii]) {
missed++
}
ii++
pi++
}
return ii == len(input) && pi == len(pattern)
return finished(ii, len(input), pi, len(pattern))
}

// finished is true if we've completed processing the input and pattern strings.
func finished(inputIndex, inputSize, patternIndex, patternSize int) bool {
return inputIndex == inputSize && patternIndex == patternSize
}

// isPossible returns true if its possible the pattern could still match.
func isPossible(inputIndex, inputSize, patternIndex, patternSize, numMissed int) bool {
return inputIndex < inputSize && patternIndex < patternSize && numMissed < 2
}

// missedMatch returns true if a match was missed.
func missedMatch(patternRune, inputRune rune) bool {
return patternRune != '.' && inputRune != patternRune
}

// remainingPattern returns true if we finished processing the input, but still have pattern remaining.
func remainingPattern(inputIndex, inputSize, patternIndex, patternSize int) bool {
return inputIndex == inputSize && patternIndex < patternSize-1
}

// shouldKeepLooking returns true if the wildcard should continue looking at the input.
func shouldKeepLooking(inputIndex, inputSize int, inputR []rune, next rune) bool {
return inputIndex < inputSize && inputR[inputIndex] != next
}
13 changes: 11 additions & 2 deletions day27/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package day27
func WellBalanced(brackets string) bool {
openBrackets := make([]rune, 0, (len(brackets)/2)+1)
for _, r := range brackets {
if r == '(' || r == '[' || r == '{' {
if matchAnyRune(r, '(', '[', '{') {
openBrackets = append(openBrackets, r)
} else if r == ')' || r == ']' || r == '}' {
} else if matchAnyRune(r, ')', ']', '}') {
if len(openBrackets) == 0 {
return false
}
Expand All @@ -24,6 +24,15 @@ func WellBalanced(brackets string) bool {
return len(openBrackets) == 0
}

// matchAnyRune will return true if the source matches any of the supplied targets.
func matchAnyRune(source rune, targets ...rune) bool {
var result bool
for _, r := range targets {
result = result || (source == r)
}
return result
}

// IsMatch returns true if the left and right runes form a matching pair.
func IsMatch(left, right rune) bool {
return (right == ')' && left == '(') ||
Expand Down
4 changes: 2 additions & 2 deletions day28/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func buildLine(line []string, k, lineLength int) string {
minimumSpaces := (k - lineLength) / len(line)
extraSpaces := k - lineLength - ((len(line) - 1) * minimumSpaces)
for i, word := range line {
sb.WriteString(word)
sb.WriteString(word) // nolint: gosec
if i == len(line)-1 {
break
}
spaces := minimumSpaces
if i < extraSpaces {
spaces++
}
sb.WriteString(strings.Repeat(" ", spaces))
sb.WriteString(strings.Repeat(" ", spaces)) // nolint: gosec
}
return sb.String()
}
12 changes: 6 additions & 6 deletions day29/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func RunLengthEncoding(str string) string {
} else if r == last {
count++
} else {
sb.WriteString(strconv.Itoa(count))
sb.WriteString(string(last))
sb.WriteString(strconv.Itoa(count)) // nolint: gosec
sb.WriteString(string(last)) // nolint: gosec
count = 1
last = r
}
}
sb.WriteString(strconv.Itoa(count))
sb.WriteString(string(last))
sb.WriteString(strconv.Itoa(count)) // nolint: gosec
sb.WriteString(string(last)) // nolint: gosec
return sb.String()
}

Expand All @@ -35,13 +35,13 @@ func RunLengthDecoding(str string) string {
var count int
for _, r := range str {
if r >= 'A' && r <= 'Z' {
sb.WriteString(strings.Repeat(string(r), count))
sb.WriteString(strings.Repeat(string(r), count)) // nolint: gosec
count = 0
} else {
if count != 0 {
count *= 10
}
digit, _ := strconv.Atoi(string(r))
digit, _ := strconv.Atoi(string(r)) // nolint: gosec
count += digit
}
}
Expand Down
19 changes: 12 additions & 7 deletions day32/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import "math"
// Uses Bellman-Ford Shortest Path which runs in O(N^3) time and O(N^2) space
// due to the transformed rates table.
func Arbitrage(rates [][]float64) bool {
graph := make([][]float64, len(rates))
for i := range graph {
graph[i] = make([]float64, len(rates[i]))
for j := range graph[i] {
graph[i][j] = -math.Log(rates[i][j])
}
}
graph := createGraph(rates)
source := 0
minDist := make([]float64, len(rates))
for i := range minDist {
Expand All @@ -39,3 +33,14 @@ func Arbitrage(rates [][]float64) bool {
}
return false
}

func createGraph(rates [][]float64) [][]float64 {
graph := make([][]float64, len(rates))
for i := range graph {
graph[i] = make([]float64, len(rates[i]))
for j := range graph[i] {
graph[i][j] = -math.Log(rates[i][j])
}
}
return graph
}
6 changes: 3 additions & 3 deletions day39/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (gol *GameOfLife) String() string {
for y := gol.maxY - 1; y > gol.minY; y-- {
for x := gol.minX + 1; x < gol.maxX; x++ {
if _, alive := gol.living[Coord{x, y}]; alive {
sb.WriteRune('*')
sb.WriteRune('*') // nolint: gosec
} else {
sb.WriteRune('.')
sb.WriteRune('.') // nolint: gosec
}
}
sb.WriteRune('\n')
sb.WriteRune('\n') // nolint: gosec
}
return sb.String()
}
Expand Down
6 changes: 3 additions & 3 deletions day43/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ func TestMaxIntStack(t *testing.T) {
stack := NewMaxIntStack()
for i, push := range tc.input {
stack.Push(push)
if max, err := stack.Max(); err != nil || max != tc.max[i] {
if max, err := stack.Max(); max != tc.max[i] {
t.Errorf("TC%d push expected,got max (%d,%d) error should be nil (%v)",
tcid, tc.max[i], max, err)
}
}
for i := range tc.input {
if max, err := stack.Max(); err != nil || max != tc.max[len(tc.max)-1-i] {
if max, err := stack.Max(); max != tc.max[len(tc.max)-1-i] {
t.Errorf("TC%d pre-pop expected,got max (%d,%d) error should be nil (%v)",
tcid, tc.max[len(tc.max)-1-i], max, err)
}
if val, err := stack.Pop(); err != nil || val != tc.input[len(tc.input)-1-i] {
if val, err := stack.Pop(); val != tc.input[len(tc.input)-1-i] {
t.Errorf("TC%d pop expected,got val (%d,%d) error should be nil (%v)",
tcid, tc.input[len(tc.input)-1-i], val, err)
}
Expand Down
6 changes: 3 additions & 3 deletions day6/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func New(val interface{}) *XorList {
// Add adds a new value
func (xl *XorList) Add(val interface{}) {
prev := uintptr(0)
this := unsafe.Pointer(xl)
this := unsafe.Pointer(xl) // nolint: gosec
for prev^xl.both != 0 {
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both)
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both) // nolint: vet, gosec
xl = (*XorList)(this)
}
nxl := &XorList{val: val, both: uintptr(this)}
xl.both = prev ^ uintptr(unsafe.Pointer(nxl))
xl.both = prev ^ uintptr(unsafe.Pointer(nxl)) // nolint: gosec
}
6 changes: 3 additions & 3 deletions day6/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func backwardToSlice(xl *XorList) []interface{} {
prev := uintptr(0)
this := unsafe.Pointer(xl)
for prev^xl.both != 0 {
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both)
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both) // nolint: vet
xl = (*XorList)(this)
}
// now collect items from tail to the head
Expand All @@ -66,7 +66,7 @@ func backwardToSlice(xl *XorList) []interface{} {
this = unsafe.Pointer(xl)
result = append(result, xl.val)
for prev^xl.both != 0 {
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both)
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both) // nolint: vet
xl = (*XorList)(this)
result = append(result, xl.val)
}
Expand All @@ -81,7 +81,7 @@ func forwardToSlice(xl *XorList) []interface{} {
this := unsafe.Pointer(xl)
result = append(result, xl.val)
for prev^xl.both != 0 {
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both)
prev, this = uintptr(this), unsafe.Pointer(prev^xl.both) // nolint: vet
xl = (*XorList)(this)
result = append(result, xl.val)
}
Expand Down
10 changes: 9 additions & 1 deletion sudoku/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Solver(board Board, n int) (Board, bool) {
}
}
for j := 0; j < n*n; j++ {
if board[n*n*j+col] == choice || board[row*n*n+j] == choice {
if matchAnyInt(choice, board[n*n*j+col], board[row*n*n+j]) {
isValid = false
break
}
Expand All @@ -38,6 +38,14 @@ func Solver(board Board, n int) (Board, bool) {
return board, false
}

func matchAnyInt(source int, targets ...int) bool {
var result bool
for _, target := range targets {
result = result || (source == target)
}
return result
}

func findEmpty(board Board, n int) (int, int, bool) {
for i, box := range board {
if box == 0 {
Expand Down

0 comments on commit 6f3c1cf

Please sign in to comment.