🌅
This commit is contained in:
58
solutions/day6/day6.go
Normal file
58
solutions/day6/day6.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package day6
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func allDifferent(a string) bool {
|
||||
length := len(a)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
for j := i + 1; j < length; j++ {
|
||||
if a[i] == a[j] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func Run(filename string, problemStr string) {
|
||||
problem, err := strconv.Atoi(problemStr)
|
||||
if err != nil || !(problem == 4 || problem == 14) {
|
||||
fmt.Println("Didn't understand problem number", os.Args[2], "should be 4 or 14")
|
||||
}
|
||||
|
||||
file, err := os.Open(filename)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
if len(line) > 0 {
|
||||
var index int
|
||||
offset := problem - 1
|
||||
|
||||
for index = offset; index < len(line); index++ {
|
||||
if allDifferent(line[index-offset : index+1]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("First marker after character", index+1)
|
||||
}
|
||||
}
|
||||
|
||||
file.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user