day16part2 refactor. oof

This commit is contained in:
Alex Chao
2020-08-05 13:20:48 -04:00
parent 91909162df
commit 03939f3472
3 changed files with 33 additions and 19 deletions
+1 -1
View File
@@ -22,4 +22,4 @@ Day | Name | Type of Algo & Notes
13 | Care Package | Intcode again! It's basically every other day... <br> - part1: 2D array manipulation again <br> - part2: holy algo, some logic to basically play Bricks game. <br> - This is more of a design question for how you manage state
14 | Space Stoichiometry | __Weighted Graph and Breadth First Traversals__ <br> - Because not all of the products have a quantity of 1, it complicates the graph's data model. I ended up mapping the product/chemical name to a map of its stoichiometry where the product's number is positive & reactants were negative. <br> - part2: not just a simple division because of "extra byproducts" of generating each piece of fuel. I just let this brute force thing run for ~30 seconds...
15 | Oxygen System | YAY INTCODE 🙄 <br> - Combination of __2D grid searching algo__, __backtracking algo__ and the the Intcode... <br> - I've realized that I really need to stop using x and y for 2D grids and start using row and col because mathematically x is horizontal and y is vertical... My brain is all jumbled up <br> - Created a Robot struct/class that has a computer inside of it. It goes and searches around, collecting data on the floor types at various coordinates. That data is transformed into a 2D grid/array, and then finally fed into a backtracking, searching algorithm to determine the shortest path (turns out there's only one path to the O2 tank...) <br> - part2 is fairly straight forward 2D grid traversing and tagging a spread of oxygen to valid tiles/hallway spaces
16 | Flawed Frequency Transmission | - Some really weird, contrived (as if this whole thing isn't) phase calculator?..
16 | Flawed Frequency Transmission | - Some really weird, contrived (as if this whole thing isn't) phase calculator?.. <br> - part2 broke my brain. Optimally calculate subsets by __precalculating running sums__ (linear), then getting subsets by subtracting two of the precalculated running sums. i.e. sub[2:4] = sub[0:4] - sub[0:2] <br> - And also switching pattern recognition to make calculating a particular output O(nlogn)...
+12 -18
View File
@@ -31,6 +31,7 @@ func main() {
// run through 100 phases, overwriting digits
for i := 0; i < 100; i++ {
digits = getNextOutputNumber(digits)
// output a time to make sure this is running fast enough
fmt.Printf("output received at %v, %v to go\n", time.Now(), 100-i-1)
}
@@ -40,21 +41,19 @@ func main() {
firstEightDigits *= 10
firstEightDigits += digits[i+offsetIndex]
}
fmt.Printf("Offset 8 digits after 100 phases: %v\n", firstEightDigits)
// fmt.Println("Expect 84462026 for test")
fmt.Printf("\nOffset 8 digits after 100 phases: %v\n", firstEightDigits)
fmt.Println("Expect 84462026 for test, 36265589 for actual input")
}
// takes in digits and all patterns, generates next set of digits
func getNextOutputNumber(digits []int) []int {
// calculate the sum of partial subsets digits[0:i]
partials := make([]int, len(digits))
// ONE INDEX THE PARTIAL SUMS, so partials[0] = 0, partials[1] = digits[0]
partials := make([]int, len(digits)+1)
for i := range digits {
// add previous partial subset if i is not zero
if i > 0 {
partials[i] += partials[i-1]
}
partials[i+1] += partials[i]
// add digit on as well
partials[i] += digits[i]
partials[i+1] += digits[i]
}
output := make([]int, len(digits))
@@ -64,31 +63,26 @@ func getNextOutputNumber(digits []int) []int {
adding := true
for start := i; start < len(digits); start += chunkLength * 2 {
// calculate chunk sum
startOfChunkIndex := start - 1
if startOfChunkIndex < 0 {
startOfChunkIndex = 0
}
startOfChunkIndex := chunkLength - 1
endOfChunkIndex := start + chunkLength - 1
if endOfChunkIndex >= len(digits) {
endOfChunkIndex = len(digits) - 1
}
chunkSum := partials[endOfChunkIndex] - partials[startOfChunkIndex]
if chunkLength == 1 {
chunkSum = digits[start]
}
// increment or decrements output index
if adding {
// fmt.Printf("adding: %v\n", chunkSum)
output[i] += chunkSum
} else {
// fmt.Printf("subtracting: %v\n", chunkSum)
output[i] -= chunkSum
}
adding = !adding
}
}
// make all output digits positive
// make all output digits positive & single digits
// NOTE this is not equivalent to taking the mod of a negative number!
// This has to be done because of the problem's spec to just take the 1's digit
for i, v := range output {
if v < 0 {
output[i] *= -1
+20
View File
@@ -66,3 +66,23 @@ Here are the first eight digits of the final output list after 100 phases for so
19617804207202209144916044189917 becomes 73745418.
69317163492948606335995924319873 becomes 52432133.
After 100 phases of FFT, what are the first eight digits in the final output list?
Your puzzle answer was 27831665.
--- Part Two ---
Now that your FFT is working, you can decode the real signal.
The real signal is your puzzle input repeated 10000 times. Treat this new signal as a single input list. Patterns are still calculated as before, and 100 phases of FFT are still applied.
The first seven digits of your initial input signal also represent the message offset. The message offset is the location of the eight-digit message in the final output list. Specifically, the message offset indicates the number of digits to skip before reading the eight-digit message. For example, if the first seven digits of your initial input signal were 1234567, the eight-digit message would be the eight digits after skipping 1,234,567 digits of the final output list. Or, if the message offset were 7 and your final output list were 98765432109876543210, the eight-digit message would be 21098765. (Of course, your real message offset will be a seven-digit number, not a one-digit number like 7.)
Here is the eight-digit message in the final output list after 100 phases. The message offset given in each input has been highlighted. (Note that the inputs given below are repeated 10000 times to find the actual starting input lists.)
03036732577212944063491565474664 becomes 84462026.
02935109699940807407585447034323 becomes 78725270.
03081770884921959731165446850517 becomes 53553731.
After repeating your input signal 10000 times and running 100 phases of FFT, what is the eight-digit message embedded in the final output list?
Your puzzle answer was 36265589.
Both parts of this puzzle are complete! They provide two gold stars: **