Piotr Czajkowski 2 years ago
parent
commit
f4bd1a4fda
1 changed files with 32 additions and 20 deletions
  1. 32 20
      14/code.go

+ 32 - 20
14/code.go

@@ -8,7 +8,7 @@ import (
 	"strings"
 )
 
-func readInput(file string) (map[string]int, map[string][]string) {
+func readInput(file string) (map[string]int, map[byte]int, map[string][]string) {
 	content, err := ioutil.ReadFile(file)
 	if err != nil {
 		log.Fatal(err)
@@ -17,6 +17,7 @@ func readInput(file string) (map[string]int, map[string][]string) {
 	lines := strings.Split(string(content), "\n")
 	template := make(map[string]int)
 	input := make(map[string][]string)
+	count := make(map[byte]int)
 	readingTemplate := true
 	for _, line := range lines {
 		if line == "" {
@@ -25,6 +26,10 @@ func readInput(file string) (map[string]int, map[string][]string) {
 		}
 
 		if readingTemplate {
+			for i := 0; i < len(line); i++ {
+				count[line[i]]++
+			}
+
 			for i := 0; i < len(line)-1; i++ {
 				template[string(line[i:i+2])] += 1
 			}
@@ -39,44 +44,51 @@ func readInput(file string) (map[string]int, map[string][]string) {
 		input[parts[0]] = []string{fmt.Sprintf("%c%s", parts[0][0], parts[1]), fmt.Sprintf("%s%c", parts[1], parts[0][1])}
 	}
 
-	return template, input
+	return template, count, input
 }
 
-func countElements(template map[string]int) (int, int) {
-	counts := make(map[byte]int)
-	for k, v := range template {
-		counts[k[0]] += v
-		counts[k[1]] += v
-	}
-
-	smallest := counts['N']
-	largest := counts['N']
-	for _, c := range counts {
-		if c < smallest {
-			smallest = c
+func countElements(count map[byte]int) (int, int) {
+	smallest := count['N']
+	largest := count['N']
+	for _, v := range count {
+		if v < smallest {
+			smallest = v
 		}
 
-		if c > largest {
-			largest = c
+		if v > largest {
+			largest = v
 		}
 	}
 
 	return smallest, largest
 }
 
-func part1(template map[string]int, input map[string][]string) (map[string]int, int) {
+func printCounts(counts map[byte]int) {
+	for k, v := range counts {
+		fmt.Printf("%c: %d;", k, v)
+	}
+	fmt.Println()
+}
+
+func part1(template map[string]int, count map[byte]int, input map[string][]string) (map[string]int, int) {
+	fmt.Println(template)
+	printCounts(count)
+
 	for i := 0; i < 10; i++ {
 		newTemplate := make(map[string]int)
 		for k, v1 := range template {
 			for _, v2 := range input[k] {
 				newTemplate[v2] += v1
 			}
+
+			count[input[k][0][1]] += v1
 		}
 
 		template = newTemplate
+		printCounts(count)
 	}
 
-	smallest, largest := countElements(template)
+	smallest, largest := countElements(count)
 	fmt.Println(smallest, largest)
 	return template, largest - smallest
 }
@@ -86,9 +98,9 @@ func main() {
 		log.Fatal("Please provide a file name as argument")
 	}
 
-	template, input := readInput(os.Args[1])
+	template, count, input := readInput(os.Args[1])
 	var diff int
-	_, diff = part1(template, input)
+	_, diff = part1(template, count, input)
 	fmt.Println("Part1:", diff)
 	//fmt.Println("Part1:", part2(template, input))
 }