From 1c8af2a8aadfb3b9a6802109393aafee8581f77a Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 24 Oct 2021 16:59:54 +0200 Subject: [PATCH] day16 --- day16/day.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ inputs/16 | 1 + inputs/16_test | 1 + main.go | 3 ++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 day16/day.go create mode 100644 inputs/16 create mode 100644 inputs/16_test diff --git a/day16/day.go b/day16/day.go new file mode 100644 index 0000000..234bbd5 --- /dev/null +++ b/day16/day.go @@ -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]) +} diff --git a/inputs/16 b/inputs/16 new file mode 100644 index 0000000..9f912b4 --- /dev/null +++ b/inputs/16 @@ -0,0 +1 @@ +01111001100111011 \ No newline at end of file diff --git a/inputs/16_test b/inputs/16_test new file mode 100644 index 0000000..1746da6 --- /dev/null +++ b/inputs/16_test @@ -0,0 +1 @@ +10000 \ No newline at end of file diff --git a/main.go b/main.go index 0bc58cb..1158ff3 100644 --- a/main.go +++ b/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},