day11_pt2.java (1537B)
1 import java.nio.file.Files; 2 import java.nio.file.Path; 3 import java.io.IOException; 4 import java.util.*; 5 import java.util.stream.*; 6 7 class day11_pt2 { 8 public static void main(String[] args) throws IOException { 9 // Order of stones is totally unimportant, store them in a map to avoid duplication... 10 var input = Files.readString(Path.of(args[0])).trim(); 11 var stones = new HashMap<String,Long>(); 12 for (var stone: input.split(" ")) { 13 var ct = stones.getOrDefault(stone, 0L); 14 stones.put(stone, ct+1); 15 } 16 var ns = new HashMap<String,Long>(); 17 for (var i=0;i<75;i++) { 18 for (var e: stones.entrySet()) { 19 var stone = e.getKey(); 20 if ("0".equals(stone)) { 21 var ct = ns.getOrDefault("1", 0L); 22 ns.put("1", ct + e.getValue()); 23 } else if (stone.length() % 2 == 0) { 24 var s1 = stone.substring(0, stone.length()/2); 25 var s2 = "%d".formatted(Long.parseLong(stone.substring(stone.length() / 2))); 26 var ct1 = ns.getOrDefault(s1, 0L); 27 ns.put(s1, ct1 + e.getValue()); 28 var ct2 = ns.getOrDefault(s2, 0L); 29 ns.put(s2, ct2 + e.getValue()); 30 } else { 31 var newKey = "%d".formatted(Long.parseLong(stone) * 2024); 32 var ct = ns.getOrDefault(newKey, 0L); 33 ns.put(newKey, ct + e.getValue()); 34 } 35 } 36 stones = ns; 37 ns = new HashMap<String,Long>(); 38 } 39 var res = stones.values().stream().mapToLong(i -> i).sum(); 40 System.out.printf("Day 11, pt2: %d\n", res); 41 } 42 }