-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package day16 | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nlowe/aoc2019/util" | ||
|
||
"github.com/nlowe/aoc2019/challenge" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var A = &cobra.Command{ | ||
Use: "16a", | ||
Short: "Day 16, Problem A", | ||
Run: func(_ *cobra.Command, _ []string) { | ||
fmt.Printf("Answer: %s\n", a(challenge.FromFile())) | ||
}, | ||
} | ||
|
||
const iterations = 100 | ||
|
||
var pattern = [4]int{0, 1, 0, -1} | ||
|
||
func a(challenge *challenge.Input) string { | ||
in := <-challenge.Lines() | ||
|
||
data := make([]int, len(in)) | ||
for i, r := range in { | ||
data[i] = util.MustAtoI(string(r)) | ||
} | ||
|
||
for i := 0; i < iterations; i++ { | ||
data = fft(data) | ||
} | ||
|
||
result := strings.Builder{} | ||
for i := 0; i < 8; i++ { | ||
result.WriteString(strconv.Itoa(data[i])) | ||
} | ||
|
||
return result.String() | ||
} | ||
|
||
func fft(in []int) []int { | ||
result := make([]int, len(in)) | ||
|
||
for i := 0; i < len(in); i++ { | ||
sum := 0 | ||
for j := i; j < len(in); j++ { | ||
sum += in[j] * mixin(j, i) | ||
} | ||
|
||
result[i] = int(math.Abs(float64(sum))) % 10 | ||
} | ||
|
||
return result | ||
} | ||
|
||
func mixin(offset, iteration int) int { | ||
return pattern[((1+offset)/(1+iteration))%len(pattern)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package day16 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nlowe/aoc2019/challenge" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestA(t *testing.T) { | ||
tests := []struct { | ||
in string | ||
out string | ||
}{ | ||
{in: "80871224585914546619083218645595", out: "24176176"}, | ||
{in: "19617804207202209144916044189917", out: "73745418"}, | ||
{in: "69317163492948606335995924319873", out: "52432133"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.in, func(t *testing.T) { | ||
require.Equal(t, tt.out, a(challenge.FromLiteral(tt.in))) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package day16 | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nlowe/aoc2019/challenge" | ||
"github.com/nlowe/aoc2019/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var B = &cobra.Command{ | ||
Use: "16b", | ||
Short: "Day 16, Problem B", | ||
Run: func(_ *cobra.Command, _ []string) { | ||
fmt.Printf("Answer: %s\n", b(challenge.FromFile())) | ||
}, | ||
} | ||
|
||
func b(challenge *challenge.Input) string { | ||
in := strings.Repeat(<-challenge.Lines(), 10000) | ||
|
||
data := make([]int, len(in)) | ||
for i, r := range in { | ||
data[i] = util.MustAtoI(string(r)) | ||
} | ||
|
||
offset := util.MustAtoI(in[:7]) | ||
data = data[offset:] | ||
for i := 0; i < iterations; i++ { | ||
data = shortcut(data) | ||
} | ||
|
||
result := strings.Builder{} | ||
for i := 0; i < 8; i++ { | ||
result.WriteString(strconv.Itoa(data[i])) | ||
} | ||
|
||
return result.String() | ||
} | ||
|
||
// TODO: Here be vodo. I need to read up on why this works. | ||
// this was mostly pieced together from posts on the | ||
// subreddit. | ||
func shortcut(in []int) []int { | ||
cumulativeSum := 0 | ||
for _, v := range in { | ||
cumulativeSum += v | ||
} | ||
|
||
var result []int | ||
for i := range in { | ||
result = append(result, ((cumulativeSum%10)+10)%10) | ||
cumulativeSum -= in[i] | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package day16 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nlowe/aoc2019/challenge" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestB(t *testing.T) { | ||
tests := []struct { | ||
in string | ||
out string | ||
}{ | ||
{in: "03036732577212944063491565474664", out: "84462026"}, | ||
{in: "02935109699940807407585447034323", out: "78725270"}, | ||
{in: "03081770884921959731165446850517", out: "53553731"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.in, func(t *testing.T) { | ||
require.Equal(t, tt.out, b(challenge.FromLiteral(tt.in))) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
59776034095811644545367793179989602140948714406234694972894485066523525742503986771912019032922788494900655855458086979764617375580802558963587025784918882219610831940992399201782385674223284411499237619800193879768668210162176394607502218602633153772062973149533650562554942574593878073238232563649673858167635378695190356159796342204759393156294658366279922734213385144895116649768185966866202413314939692174223210484933678866478944104978890019728562001417746656699281992028356004888860103805472866615243544781377748654471750560830099048747570925902575765054898899512303917159138097375338444610809891667094051108359134017128028174230720398965960712 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters