Browse Source

Solved part1

Piotr Czajkowski 1 year ago
parent
commit
0ffe14b9cb
1 changed files with 35 additions and 3 deletions
  1. 35 3
      06/code.go

+ 35 - 3
06/code.go

@@ -7,13 +7,45 @@ import (
 	"os"
 )
 
-func readInput(path string) string {
+func readInput(path string) []byte {
 	data, err := ioutil.ReadFile(path)
 	if err != nil {
 		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() {
@@ -23,5 +55,5 @@ func main() {
 
 	filePath := os.Args[1]
 	text := readInput(filePath)
-	fmt.Println(text)
+	fmt.Println("Part1:", part1(text))
 }