2019 Day 16 Complete

This commit is contained in:
Brian Buller 2019-12-16 09:25:02 -06:00
parent a3cee63862
commit 11019fa77e
3 changed files with 90 additions and 0 deletions

1
2019/day16/input Normal file
View File

@ -0,0 +1 @@
59708072843556858145230522180223745694544745622336045476506437914986923372260274801316091345126141549522285839402701823884690004497674132615520871839943084040979940198142892825326110513041581064388583488930891380942485307732666485384523705852790683809812073738758055115293090635233887206040961042759996972844810891420692117353333665907710709020698487019805669782598004799421226356372885464480818196786256472944761036204897548977647880837284232444863230958576095091824226426501119748518640709592225529707891969295441026284304137606735506294604060549102824977720776272463738349154440565501914642111802044575388635071779775767726626682303495430936326809

88
2019/day16/main.go Normal file
View File

@ -0,0 +1,88 @@
package main
import (
"fmt"
"math"
"strings"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := helpers.StdinToString()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(inp string) {
var intinp []int
for _, v := range inp {
intinp = append(intinp, int(byte(v)-'0'))
}
for i := 0; i < 100; i++ {
intinp = runFFT(intinp)
}
fmt.Println("# Part 1:")
fmt.Println(intinp[:8])
}
// Well, brute force isn't going to work
// Fortunately, the next instance of each digit is actually just the
// sum of all digits after it (%10)
func part2(inp string) {
fullInput := strings.Repeat(inp, 10000)
messageOffset := helpers.Atoi(inp[:7])
var intinp []int
for _, v := range fullInput[messageOffset:] {
intinp = append(intinp, int(byte(v)-'0'))
}
for iter := 0; iter < 100; iter++ {
sum := 0
for i := len(intinp) - 1; i >= 0; i-- {
sum += intinp[i]
intinp[i] = sum % 10
}
}
fmt.Println("# Part 2:")
for _, c := range intinp[:8] {
fmt.Print(helpers.Itoa(c))
}
fmt.Println()
}
func runFFT(inp []int) []int {
var result []int
for k := range inp {
pattern := getPattern(len(inp), k+1)
var wrk int
for k, iv := range inp {
wrk = wrk + (iv * pattern[k])
}
result = append(result, helpers.AbsInt(wrk%10))
}
return result
}
func getPattern(length, iter int) []int {
var ret []int
pattern := []int{0, 1, 0, -1}
var idx int
for len(ret) < length+1 {
wrkIter := iter
for ; wrkIter > 0; wrkIter-- {
ret = append(ret, pattern[idx])
}
idx = (idx + 1) % len(pattern)
}
return ret[1 : length+1]
}
func intSliceToInt(inp []int) int {
var ret int
for k := len(inp) - 1; k >= 0; k-- {
ret = ret + (inp[k] * int(math.Pow10(len(inp)-k-1)))
}
return ret
}

1
2019/day16/testinput1 Normal file
View File

@ -0,0 +1 @@
12345678