Browse Source

Solved part1

Piotr Czajkowski 3 months ago
parent
commit
ad130b25ce
2 changed files with 47 additions and 1 deletions
  1. 46 1
      11/code.go
  2. 1 0
      11/input

+ 46 - 1
11/code.go

@@ -29,11 +29,56 @@ func readInput(file string) []int {
 	return stones
 }
 
+func splitStone(stoneString string) (int, int) {
+	half := len(stoneString) / 2
+
+	first, err1 := strconv.Atoi(stoneString[:half])
+	if err1 != nil {
+		log.Fatalf("Can't convert %s: %s", stoneString[:half], err1)
+	}
+
+	second, err2 := strconv.Atoi(stoneString[half:])
+	if err1 != nil {
+		log.Fatalf("Can't convert %s: %s", stoneString[half:], err2)
+	}
+
+	return first, second
+}
+
+func processStones(stones []int) []int {
+	var newStones []int
+	for _, stone := range stones {
+		if stone == 0 {
+			newStones = append(newStones, 1)
+			continue
+		}
+
+		stoneString := fmt.Sprintf("%d", stone)
+		if len(stoneString)%2 == 0 {
+			first, second := splitStone(stoneString)
+			newStones = append(newStones, first)
+			newStones = append(newStones, second)
+		} else {
+			newStones = append(newStones, stone*2024)
+		}
+	}
+
+	return newStones
+}
+
+func part1(stones []int) int {
+	for i := 0; i < 25; i++ {
+		stones = processStones(stones)
+	}
+
+	return len(stones)
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
 	}
 
 	stones := readInput(os.Args[1])
-	fmt.Println(stones)
+	fmt.Println("Part1:", part1(stones))
 }

+ 1 - 0
11/input

@@ -0,0 +1 @@
+1750884 193 866395 7 1158 31 35216 0