52 lines
962 B
Go
52 lines
962 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
inp := h.StdinToString()
|
||
|
fmt.Println("# Part 1")
|
||
|
fmt.Printf("First Start of Packet @ %d\n", findFirstStartOfPacket(inp))
|
||
|
fmt.Println("")
|
||
|
fmt.Println("# Part 2")
|
||
|
fmt.Printf("First Start of Message @ %d\n", findFirstStartOfMessage(inp))
|
||
|
}
|
||
|
|
||
|
func findFirstStartOfPacket(inp string) int {
|
||
|
return findFirstWithXUnique(inp, 4)
|
||
|
}
|
||
|
|
||
|
func findFirstStartOfMessage(inp string) int {
|
||
|
return findFirstWithXUnique(inp, 14)
|
||
|
}
|
||
|
|
||
|
func findFirstWithXUnique(inp string, x int) int {
|
||
|
for i := range inp {
|
||
|
if i < x {
|
||
|
continue
|
||
|
}
|
||
|
if findUniqueCountBefore(inp, i) >= x {
|
||
|
return i + 1
|
||
|
}
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
func findUniqueCountBefore(inp string, pos int) int {
|
||
|
if len(inp) < pos {
|
||
|
return -1
|
||
|
}
|
||
|
counts := make(map[byte]int)
|
||
|
for i := pos; i >= 0; i-- {
|
||
|
if _, ok := counts[inp[i]]; !ok {
|
||
|
counts[inp[i]]++
|
||
|
} else {
|
||
|
return len(counts)
|
||
|
}
|
||
|
}
|
||
|
return pos
|
||
|
}
|