59 lines
968 B
Go
59 lines
968 B
Go
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()
|
|
}
|