-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
62 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 52 additions & 11 deletions
63
adventofcode/src/main/java/org/ck/adventofcode/year2024/Day11.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,75 @@ | ||
package org.ck.adventofcode.year2024; | ||
|
||
import java.util.Scanner; | ||
import java.util.*; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20241101, | ||
name = "Day 11: ?", | ||
name = "Day 11: Plutonian Pebbles", | ||
url = "https://adventofcode.com/2024/day/11", | ||
category = "2024", | ||
solved = false) | ||
category = "2024") | ||
@Solution( | ||
id = 20241102, | ||
name = "Day 11: ? - Part 2", | ||
name = "Day 11: Plutonian Pebbles - Part 2", | ||
url = "https://adventofcode.com/2024/day/11#part2", | ||
category = "2024", | ||
solved = false) | ||
category = "2024") | ||
public class Day11 extends AOCSolution { | ||
|
||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in); | ||
run(in, 25); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in); | ||
run(in, 75); | ||
} | ||
|
||
private void run(final Scanner in) { | ||
print("Whoopsie"); | ||
private void run(final Scanner in, final int steps) { | ||
final List<Long> numbers = | ||
Arrays.stream(in.nextLine().split(" ")).map(Long::parseLong).toList(); | ||
|
||
final Map<String, Long> cache = new HashMap<>(); | ||
long sum = 0; | ||
|
||
for (Long number : numbers) { | ||
sum += getValueAfterSteps(cache, number, steps); | ||
} | ||
|
||
print(sum); | ||
} | ||
|
||
private long getValueAfterSteps( | ||
final Map<String, Long> cache, final Long number, final int steps) { | ||
final String key = "%d-%d".formatted(number, steps); | ||
if (cache.containsKey(key)) { | ||
return cache.get(key); | ||
} | ||
|
||
if (steps == 0) { | ||
return 1; | ||
} | ||
|
||
long value = 0; | ||
|
||
if (number == 0) { | ||
value += getValueAfterSteps(cache, 1L, steps - 1); | ||
} else { | ||
final String baseTen = String.valueOf(number); | ||
|
||
if (baseTen.length() % 2 == 0) { | ||
value += | ||
getValueAfterSteps( | ||
cache, Long.valueOf(baseTen.substring(0, baseTen.length() / 2)), steps - 1) | ||
+ getValueAfterSteps( | ||
cache, Long.valueOf(baseTen.substring(baseTen.length() / 2)), steps - 1); | ||
} else { | ||
value += getValueAfterSteps(cache, number * 2024, steps - 1); | ||
} | ||
} | ||
|
||
cache.put(key, value); | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
222461 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 54 992917 5270417 2514 28561 0 990 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
55312 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
125 17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
264350935776416 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 54 992917 5270417 2514 28561 0 990 |
Empty file removed
0
adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.result.txt
Empty file.
Empty file.