Created
December 15, 2021 03:18
-
-
Save gullyn/fe121e576f3d824676b00cf1759e9425 to your computer and use it in GitHub Desktop.
aoc day 14 part 2
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
package main | |
import ( | |
"fmt" | |
"math" | |
"os" | |
"strings" | |
) | |
func hash(id string) uint64 { | |
return (uint64(id[0])-65)*26 + uint64(id[1]) - 65 | |
} | |
func parseInput(data string, values []uint64) { | |
for i := 1; i < len(data); i++ { | |
values[hash(string(data[i-1])+string(data[i]))]++ | |
} | |
} | |
func iterate(values []uint64, conversions map[uint64]uint64) []uint64 { | |
new_values := make([]uint64, 26*26) | |
for i, n := range values { | |
if _, ok := conversions[uint64(i)]; !ok || n == 0 { | |
new_values[i] += n | |
continue | |
} | |
new_values[uint64(i/26*26)+conversions[uint64(i)]] += n | |
new_values[conversions[uint64(i)]*26+uint64(i%26)] += n | |
} | |
return new_values | |
} | |
func main() { | |
dat, _ := os.ReadFile("day14.txt") | |
split := strings.Split(string(dat), "\n") | |
values := make([]uint64, 26*26) | |
conversions := make(map[uint64]uint64, 0) | |
parseInput(split[0], values) | |
for i := 2; i < len(split); i++ { | |
conversions[hash(strings.Split(split[i], " -> ")[0])] = hash("A" + strings.Split(split[i], " -> ")[1]) | |
} | |
for i := 0; i < 40; i++ { | |
values = iterate(values, conversions) | |
} | |
appearances := make([]uint64, 26) | |
for i, n := range values { | |
appearances[i/26] += n | |
appearances[i%26] += n | |
} | |
appearances[hash("A"+string(string(dat)[0]))]++ | |
max := uint64(0) | |
min := uint64(18446744073709551615) | |
for i, n := range appearances { | |
if n == 0 { | |
continue | |
} | |
appearances[i] = uint64(math.Ceil(float64(n) / 2)) | |
if appearances[i] > max { | |
max = appearances[i] | |
} | |
if appearances[i] < min { | |
min = appearances[i] | |
} | |
} | |
fmt.Println(max - min) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment