Advertisement
icdef

Untitled

Dec 11th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | Help | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) throws FileNotFoundException {
  4.         File inputFile = new File("input-data/day11.txt");
  5.         Scanner scanner = new Scanner(inputFile);
  6.         String stoneLine = scanner.nextLine();
  7.         List<Long> stones = new ArrayList<>(Arrays.stream(stoneLine.split(" "))
  8.                 .mapToLong(Long::parseLong).boxed().toList());
  9.  
  10.         Map<Long, List<Long>> transformationMapAll = new HashMap<>();
  11.         Map<Long, Long> transformationCount = new HashMap<>();
  12.  
  13.         for (int i = 0; i < 25; i++) {
  14.             transformationCount = new HashMap<>();
  15.             for (Long stone : stones) {
  16.                 transform(stone, transformationMapAll, transformationCount);
  17.             }
  18.             stones = new ArrayList<>();
  19.             for (Map.Entry<Long, List<Long>> tr : transformationMapAll.entrySet()) {
  20.                 if (transformationCount.containsKey(tr.getKey())) {
  21.                     for (int j = 0; j < transformationCount.get(tr.getKey()); j++) {
  22.                         stones.addAll(transformationMapAll.get(tr.getKey()));
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.  
  28.         long sum = 0;
  29.         for (Map.Entry<Long, Long> tr : transformationCount.entrySet()) {
  30. //            System.out.println(tr);
  31.             sum += tr.getValue() * transformationMapAll.get(tr.getKey()).size();
  32.         }
  33.         System.out.println(sum);
  34.        
  35.     }
  36.  
  37.     private static void transform(long stone, Map<Long, List<Long>> transformationMap,
  38.                                   Map<Long, Long> transformationCount) {
  39.  
  40.         if (transformationMap.containsKey(stone)) {
  41.             long old = transformationCount.getOrDefault(stone, 0L);
  42.             transformationCount.put(stone, old + 1);
  43.         } else {
  44.             if (stone == 0) {
  45.                 transformationMap.put(stone, Collections.singletonList(1L));
  46.                 transformationCount.put(stone, 1L);
  47.             } else if (((long) (Math.log10(stone) + 1)) % 2 == 0) {
  48.                 long length = (long) (Math.log10(stone) + 1);
  49.                 long left = (long) (stone / Math.pow(10, length / 2));
  50.                 long right = (long) (stone % Math.pow(10, length / 2));
  51.                 List<Long> list = new ArrayList<>();
  52.                 list.add(left);
  53.                 list.add(right);
  54.                 transformationMap.put(stone, list);
  55.                 transformationCount.put(stone, 1L);
  56.             } else {
  57.                 transformationMap.put(stone, Collections.singletonList(stone * 2024));
  58.                 transformationCount.put(stone, 1L);
  59.             }
  60.         }
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement