Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- File inputFile = new File("input-data/day11.txt");
- Scanner scanner = new Scanner(inputFile);
- String stoneLine = scanner.nextLine();
- List<Long> stones = new ArrayList<>(Arrays.stream(stoneLine.split(" "))
- .mapToLong(Long::parseLong).boxed().toList());
- Map<Long, List<Long>> transformationMapAll = new HashMap<>();
- Map<Long, Long> transformationCount = new HashMap<>();
- for (int i = 0; i < 25; i++) {
- transformationCount = new HashMap<>();
- for (Long stone : stones) {
- transform(stone, transformationMapAll, transformationCount);
- }
- stones = new ArrayList<>();
- for (Map.Entry<Long, List<Long>> tr : transformationMapAll.entrySet()) {
- if (transformationCount.containsKey(tr.getKey())) {
- for (int j = 0; j < transformationCount.get(tr.getKey()); j++) {
- stones.addAll(transformationMapAll.get(tr.getKey()));
- }
- }
- }
- }
- long sum = 0;
- for (Map.Entry<Long, Long> tr : transformationCount.entrySet()) {
- // System.out.println(tr);
- sum += tr.getValue() * transformationMapAll.get(tr.getKey()).size();
- }
- System.out.println(sum);
- }
- private static void transform(long stone, Map<Long, List<Long>> transformationMap,
- Map<Long, Long> transformationCount) {
- if (transformationMap.containsKey(stone)) {
- long old = transformationCount.getOrDefault(stone, 0L);
- transformationCount.put(stone, old + 1);
- } else {
- if (stone == 0) {
- transformationMap.put(stone, Collections.singletonList(1L));
- transformationCount.put(stone, 1L);
- } else if (((long) (Math.log10(stone) + 1)) % 2 == 0) {
- long length = (long) (Math.log10(stone) + 1);
- long left = (long) (stone / Math.pow(10, length / 2));
- long right = (long) (stone % Math.pow(10, length / 2));
- List<Long> list = new ArrayList<>();
- list.add(left);
- list.add(right);
- transformationMap.put(stone, list);
- transformationCount.put(stone, 1L);
- } else {
- transformationMap.put(stone, Collections.singletonList(stone * 2024));
- transformationCount.put(stone, 1L);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement