Created
December 14, 2021 10:06
-
-
Save almendar/9054f3d7e8ce10b11fb9e5ece2911f04 to your computer and use it in GitHub Desktop.
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" | |
"io/ioutil" | |
"log" | |
"math" | |
"strings" | |
) | |
func readLine(input string) ([]string, map[string]string) { | |
bytes, err := ioutil.ReadFile(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
lines := strings.Split(string(bytes), "\n") | |
chain := make([]string, 0, len(lines[0])) | |
for _, v := range lines[0] { | |
chain = append(chain, string(v)) | |
} | |
transformations := make(map[string]string) | |
for _, v := range lines[2:] { | |
tmp := strings.Split(v, " -> ") | |
transformations[tmp[0]] = tmp[1] | |
} | |
return chain, transformations | |
} | |
func task1step(chain []string, transformations map[string]string) []string { | |
nextChain := make([]string, 0, 2*len(chain)) | |
nextChain = append(nextChain, chain[0]) | |
for i := 0; i < len(chain)-1; i++ { | |
key := chain[i] + chain[i+1] | |
nextChain = append(nextChain, transformations[key], chain[i+1]) | |
} | |
return nextChain | |
} | |
func printChain(chain []string) { | |
for _, v := range chain { | |
fmt.Print(v) | |
} | |
fmt.Println() | |
} | |
func task1(input string) { | |
chain, transformations := readLine(input) | |
// printChain(chain) | |
var nextChain []string = chain | |
steps := 10 | |
for i := 1; i <= steps; i++ { | |
nextChain = task1step(nextChain, transformations) | |
// printChain(nextChain) | |
} | |
min := math.MaxInt | |
minS := "" | |
max := -1 | |
maxS := "" | |
counts := make(map[string]int) | |
for _, v := range nextChain { | |
counts[v]++ | |
} | |
for k := range counts { | |
if counts[k] < min { | |
min = counts[k] | |
minS = k | |
} | |
if counts[k] > max { | |
max = counts[k] | |
maxS = k | |
} | |
} | |
fmt.Printf("Day 14 task 1 input: %v\n", input) | |
fmt.Printf("min: %v\n", min) | |
fmt.Printf("max: %v\n", max) | |
fmt.Printf("(max-min): %v\n", (max - min)) | |
fmt.Printf("minS: %v maxS: %v\n", minS, maxS) | |
} | |
func task2(input string) { | |
chain, transformations := readLine(input) | |
adjacentTracker := make(map[string]int) | |
counts := make(map[string]int) | |
counts[chain[0]]++ | |
for i := 0; i < len(chain)-1; i++ { | |
key := chain[i] + chain[i+1] | |
adjacentTracker[key] += 1 | |
counts[chain[i+1]]++ | |
} | |
min := math.MaxInt | |
minS := "" | |
max := -1 | |
maxS := "" | |
steps := 40 | |
for i := 1; i <= steps; i++ { | |
nextTracker := make(map[string]int) | |
for k, val := range adjacentTracker { | |
newItem := transformations[k] | |
left := string(k[0]) + newItem | |
right := newItem + string(k[1]) | |
counts[newItem] += val | |
nextTracker[left] += val | |
nextTracker[right] += val | |
} | |
adjacentTracker = nextTracker | |
} | |
for k := range counts { | |
if counts[k] < min { | |
min = counts[k] | |
minS = k | |
} | |
if counts[k] > max { | |
max = counts[k] | |
maxS = k | |
} | |
} | |
fmt.Printf("Day 14 task 2 input: %v\n", input) | |
fmt.Printf("min: %v\n", min) | |
fmt.Printf("max: %v\n", max) | |
fmt.Printf("(max-min): %v\n", (max - min)) | |
fmt.Printf("minS: %v maxS: %v\n", minS, maxS) | |
} | |
func main() { | |
task1("example.txt") | |
task1("input.txt") | |
task2("example.txt") | |
task2("input.txt") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment