aoc2016/day16/day.go
Stefan Harmuth 1c8af2a8aa day16
2021-10-24 16:59:54 +02:00

59 lines
966 B
Go

package day16
import (
"tools"
)
func expandState(state string) string {
b := []rune(state)
for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 {
x := b[j]
if b[i] == 48 {
b[j] = 49
} else {
b[j] = 48
}
if x == 48 {
b[i] = 49
} else {
b[i] = 48
}
}
return state + "0" + string(b)
}
func getCheckSum(state string) string {
checksum := []byte(state)
for len(checksum)%2 == 0 {
var newsum []byte
bytes := checksum
for i := 0; i < len(bytes); i += 2 {
newsum = append(newsum, 49-(bytes[i]+bytes[i+1])%2)
}
checksum = newsum
}
return string(checksum)
}
func Part1(puzzle tools.AoCPuzzle) interface{} {
state := puzzle.GetInputArray()[0]
for len(state) < 272 {
state = expandState(state)
}
return getCheckSum(state[:272])
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
state := puzzle.GetInputArray()[0]
for len(state) < 35651584 {
state = expandState(state)
}
return getCheckSum(state[:35651584])
}