commit 594417f456226d37c3038c028292af6c5c162288
parent 1caf5cea2cc5120fd45f78b483398777c8227c2e
Author: Martin Ashby <martin@ashbysoft.com>
Date: Tue, 31 Dec 2024 22:22:32 +0000
Day 11 pt1 and 2
Diffstat:
4 files changed, 77 insertions(+), 0 deletions(-)
diff --git a/day11.txt b/day11.txt
@@ -0,0 +1 @@
+5910927 0 1 47 261223 94788 545 7771
diff --git a/day11_sample.txt b/day11_sample.txt
@@ -0,0 +1 @@
+125 17
diff --git a/src/main/java/day11_pt1.java b/src/main/java/day11_pt1.java
@@ -0,0 +1,33 @@
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.*;
+
+class day11_pt1 {
+ public static void main(String[] args) throws IOException {
+ var input = Files.readString(Path.of(args[0])).trim();
+ var stones = new ArrayList<>(Arrays.asList(input.split(" ")));
+ System.out.println(stones);
+ for (var i=0;i<25;i++) {
+ for (var j=0; j<stones.size(); j++) {
+ var stone = stones.get(j);
+ if ("0".equals(stone)) {
+ stones.set(j, "1");
+ } else if (stone.length() % 2 == 0) {
+ var s1 = stone.substring(0, stone.length()/2);
+ var s2 = "%d".formatted(Long.parseLong(stone.substring(stone.length() / 2)));
+ //System.out.printf("s1 [%s] s2 [%s]\n", s1, s2);
+ stones.set(j, s2);
+ stones.add(j, s1);
+ j++; // skip the newly inserted stone
+ } else {
+ stones.set(j, "%d".formatted(Long.parseLong(stone) * 2024));
+ }
+ }
+ //System.out.println(stones);
+ }
+ var res = stones.size();
+ System.out.printf("Day 11, pt1: %d\n", res);
+ }
+}
diff --git a/src/main/java/day11_pt2.java b/src/main/java/day11_pt2.java
@@ -0,0 +1,42 @@
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.*;
+
+class day11_pt2 {
+ public static void main(String[] args) throws IOException {
+ // Order of stones is totally unimportant, store them in a map to avoid duplication...
+ var input = Files.readString(Path.of(args[0])).trim();
+ var stones = new HashMap<String,Long>();
+ for (var stone: input.split(" ")) {
+ var ct = stones.getOrDefault(stone, 0L);
+ stones.put(stone, ct+1);
+ }
+ var ns = new HashMap<String,Long>();
+ for (var i=0;i<75;i++) {
+ for (var e: stones.entrySet()) {
+ var stone = e.getKey();
+ if ("0".equals(stone)) {
+ var ct = ns.getOrDefault("1", 0L);
+ ns.put("1", ct + e.getValue());
+ } else if (stone.length() % 2 == 0) {
+ var s1 = stone.substring(0, stone.length()/2);
+ var s2 = "%d".formatted(Long.parseLong(stone.substring(stone.length() / 2)));
+ var ct1 = ns.getOrDefault(s1, 0L);
+ ns.put(s1, ct1 + e.getValue());
+ var ct2 = ns.getOrDefault(s2, 0L);
+ ns.put(s2, ct2 + e.getValue());
+ } else {
+ var newKey = "%d".formatted(Long.parseLong(stone) * 2024);
+ var ct = ns.getOrDefault(newKey, 0L);
+ ns.put(newKey, ct + e.getValue());
+ }
+ }
+ stones = ns;
+ ns = new HashMap<String,Long>();
+ }
+ var res = stones.values().stream().mapToLong(i -> i).sum();
+ System.out.printf("Day 11, pt2: %d\n", res);
+ }
+}