day16
This commit is contained in:
parent
55c6d5fa3b
commit
1c8af2a8aa
58
day16/day.go
Normal file
58
day16/day.go
Normal file
@ -0,0 +1,58 @@
|
||||
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])
|
||||
}
|
||||
1
inputs/16_test
Normal file
1
inputs/16_test
Normal file
@ -0,0 +1 @@
|
||||
10000
|
||||
3
main.go
3
main.go
@ -15,6 +15,7 @@ import (
|
||||
"aoc2016/day13"
|
||||
"aoc2016/day14"
|
||||
"aoc2016/day15"
|
||||
"aoc2016/day16"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -47,7 +48,7 @@ func initDayFunctions() {
|
||||
13: {1: day13.Part1, 2: day13.Part2},
|
||||
14: {1: day14.Part1, 2: day14.Part2},
|
||||
15: {1: day15.Part1, 2: day15.Part2},
|
||||
// 16: {1: day16.Part1, 2: day16.Part2},
|
||||
16: {1: day16.Part1, 2: day16.Part2},
|
||||
// 17: {1: day17.Part1, 2: day17.Part2},
|
||||
// 18: {1: day18.Part1, 2: day18.Part2},
|
||||
// 19: {1: day19.Part1, 2: day19.Part2},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user