|
@@ -7,13 +7,45 @@ import (
|
|
"os"
|
|
"os"
|
|
)
|
|
)
|
|
|
|
|
|
-func readInput(path string) string {
|
|
|
|
|
|
+func readInput(path string) []byte {
|
|
data, err := ioutil.ReadFile(path)
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- return string(data)
|
|
|
|
|
|
+ return data
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func check(duplicates map[byte]int) bool {
|
|
|
|
+ for _, value := range duplicates {
|
|
|
|
+ if value > 1 {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func part1(text []byte) int {
|
|
|
|
+ count := 0
|
|
|
|
+ toDelete := 0
|
|
|
|
+ duplicates := make(map[byte]int)
|
|
|
|
+
|
|
|
|
+ for i := range text {
|
|
|
|
+ count++
|
|
|
|
+
|
|
|
|
+ duplicates[text[i]]++
|
|
|
|
+ if count-toDelete > 3 {
|
|
|
|
+ if check(duplicates) {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ duplicates[text[toDelete]]--
|
|
|
|
+ toDelete++
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return count
|
|
}
|
|
}
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
@@ -23,5 +55,5 @@ func main() {
|
|
|
|
|
|
filePath := os.Args[1]
|
|
filePath := os.Args[1]
|
|
text := readInput(filePath)
|
|
text := readInput(filePath)
|
|
- fmt.Println(text)
|
|
|
|
|
|
+ fmt.Println("Part1:", part1(text))
|
|
}
|
|
}
|