Skip to content

Commit 9df6b20

Browse files
committed
aoc 2024 day 10
1 parent 24accac commit 9df6b20

File tree

23 files changed

+234
-18
lines changed

23 files changed

+234
-18
lines changed

adventofcode/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@
14221422
[20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java
14231423
[20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java
14241424

1425-
# 2024 (18/49)
1425+
# 2024 (20/49)
14261426

14271427
| # | Name | Solution | Test |
14281428
|---------:|---------------------------------------------------|:------------------------------------:|:---------------------------------:|
@@ -1444,8 +1444,8 @@
14441444
| 20240802 | [Day 8: Resonant Collinearity - Part 2][20240802] | ✅[💾][20240802solution] | ✅[💾][20240802tests] |
14451445
| 20240901 | [Day 9: Disk Fragmenter][20240901] | ✅[💾][20240901solution] | ✅[💾][20240901tests] |
14461446
| 20240902 | [Day 9: Disk Fragmenter - Part 2][20240902] | ✅[💾][20240902solution] | ✅[💾][20240902tests] |
1447-
| 20241001 | [Day 10: ?][20241001] | [💾][20241001solution] | [💾][20241001tests] |
1448-
| 20241002 | [Day 10: ? - Part 2][20241002] | [💾][20241002solution] | [💾][20241002tests] |
1447+
| 20241001 | [Day 10: Hoof It][20241001] | ✅[💾][20241001solution] | ✅[💾][20241001tests] |
1448+
| 20241002 | [Day 10: Hoof It - Part 2][20241002] | ✅[💾][20241002solution] | ✅[💾][20241002tests] |
14491449
| 20241101 | [Day 11: ?][20241101] | [💾][20241101solution] | [💾][20241101tests] |
14501450
| 20241102 | [Day 11: ? - Part 2][20241102] | [💾][20241102solution] | [💾][20241102tests] |
14511451
| 20241201 | [Day 12: ?][20241201] | [💾][20241201solution] | [💾][20241201tests] |
Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,77 @@
11
package org.ck.adventofcode.year2024;
22

3-
import java.util.Scanner;
3+
import java.util.*;
4+
import java.util.function.Supplier;
45
import org.ck.adventofcode.util.AOCSolution;
56
import org.ck.codechallengelib.annotation.Solution;
67

78
@Solution(
89
id = 20241001,
9-
name = "Day 10: ?",
10+
name = "Day 10: Hoof It",
1011
url = "https://adventofcode.com/2024/day/10",
11-
category = "2024",
12-
solved = false)
12+
category = "2024")
1313
@Solution(
1414
id = 20241002,
15-
name = "Day 10: ? - Part 2",
15+
name = "Day 10: Hoof It - Part 2",
1616
url = "https://adventofcode.com/2024/day/10#part2",
17-
category = "2024",
18-
solved = false)
17+
category = "2024")
1918
public class Day10 extends AOCSolution {
2019

2120
@Override
2221
protected void runPartOne(final Scanner in) {
23-
run(in);
22+
run(in, HashSet::new);
2423
}
2524

2625
@Override
2726
protected void runPartTwo(final Scanner in) {
28-
run(in);
27+
run(in, ArrayList::new);
2928
}
3029

31-
private void run(final Scanner in) {
32-
print("Whoopsie");
30+
private void run(final Scanner in, final Supplier<Collection<Position>> collectionInitializer) {
31+
final List<String> grid = new ArrayList<>();
32+
33+
while (in.hasNextLine()) {
34+
grid.add(in.nextLine());
35+
}
36+
37+
long trailHeadCound = 0;
38+
39+
for (int y = 0; y < grid.size(); ++y) {
40+
for (int x = 0; x < grid.get(y).length(); ++x) {
41+
if (grid.get(y).charAt(x) == '0') {
42+
trailHeadCound += findPaths(grid, x, y, collectionInitializer).size();
43+
}
44+
}
45+
}
46+
47+
print(trailHeadCound);
3348
}
49+
50+
private Collection<Position> findPaths(
51+
final List<String> grid,
52+
final int x,
53+
final int y,
54+
final Supplier<Collection<Position>> collectionInitializer) {
55+
if (grid.get(y).charAt(x) == '9') {
56+
return Set.of(new Position(x, y));
57+
}
58+
59+
final Collection<Position> reachable = collectionInitializer.get();
60+
if (x > 0 && grid.get(y).charAt(x - 1) == grid.get(y).charAt(x) + 1) {
61+
reachable.addAll(findPaths(grid, x - 1, y, collectionInitializer));
62+
}
63+
if (x < grid.get(y).length() - 1 && grid.get(y).charAt(x + 1) == grid.get(y).charAt(x) + 1) {
64+
reachable.addAll(findPaths(grid, x + 1, y, collectionInitializer));
65+
}
66+
if (y > 0 && grid.get(y - 1).charAt(x) == grid.get(y).charAt(x) + 1) {
67+
reachable.addAll(findPaths(grid, x, y - 1, collectionInitializer));
68+
}
69+
if (y < grid.size() - 1 && grid.get(y + 1).charAt(x) == grid.get(y).charAt(x) + 1) {
70+
reachable.addAll(findPaths(grid, x, y + 1, collectionInitializer));
71+
}
72+
73+
return reachable;
74+
}
75+
76+
record Position(int x, int y) {}
3477
}

adventofcode/src/test/java/org/ck/adventofcode/year2024/Day10Test.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package org.ck.adventofcode.year2024;
22

33
import org.ck.adventofcode.util.BaseAOCTest;
4-
import org.junit.jupiter.api.Disabled;
54
import org.junit.jupiter.params.ParameterizedTest;
65
import org.junit.jupiter.params.provider.ValueSource;
76

8-
@Disabled
97
class Day10Test extends BaseAOCTest {
108
@ParameterizedTest
11-
@ValueSource(strings = {"01", "01a"})
9+
@ValueSource(strings = {"01", "01a", "01b", "01c", "01d"})
1210
void testOne(String name) throws Exception {
1311
runTest(new Day10()::partOne, "day10/%s".formatted(name));
1412
}
1513

1614
@ParameterizedTest
17-
@ValueSource(strings = {"02", "02a"})
15+
@ValueSource(strings = {"02", "02a", "02b", "02c", "02d"})
1816
void testTwo(String name) throws Exception {
1917
runTest(new Day10()::partTwo, "day10/%s".formatted(name));
2018
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
659
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
210783456778967870123014510101265430321567841067432103
2+
389892167865430965232323623432278921430438932398963412
3+
456787018917821234301056798542103450543228940187876503
4+
144567878706980031212349887653012760690117651256901414
5+
033178989215432120103498978954321821789807652349832365
6+
122017676321056763234567827667430930989778943210765474
7+
221022345432349804589432014578123045678654332122110389
8+
478431001234989812676541323489054101569743221043011268
9+
569532102145676543234540983234569232478890104454345457
10+
459643243054321789107632872110878745323210223467276306
11+
348758954567810654308901962028964656910343210568189210
12+
232367985678910563213456451038943247871356901879011232
13+
141031076589623410012847302347652198961267812921010941
14+
056122125486543223678998218956981067650345765832349850
15+
347034234397890104556780127965671230347876854767456767
16+
298965340987013278943012876871060141236989945678945698
17+
100879891056324562102103945432054650105410834100034501
18+
321410702345456983478754434567123789870320123211129632
19+
492323612101237874569569123458870123561016784342568543
20+
585494543087654723433478010789969834432125692103477655
21+
676788984599123014529821021056954765893236543238989856
22+
569876678678043565810730112347823876734567832397650765
23+
108765549432154378925643209454210985621696941987041654
24+
012014230556069210234758998763401034100787100456132545
25+
321023121687678776107867812012310123212898912343203456
26+
434564087794502985289978903401498921089877654132112347
27+
543478096893211234676129984512347832210168743043032298
28+
692109145232120140165038777601056544301259012158941187
29+
783013234141034051234345668932347895614348747867650016
30+
014560179054543761018903456787890198754109636958934567
31+
323678788765656890127812345196781256543234545567825898
32+
014989699656387787436541014012376387012373236430012327
33+
123176540345494598545632123963465498106789108921183210
34+
034012331256923687656543010876501065410632107847894789
35+
545143422107810765467852196549432678321541096556905654
36+
696654212212701899321946587038944569432532187443211565
37+
787963401321432108910707432127653654549651056300120478
38+
297872317450545657659818956012102165678740145212321329
39+
187701298764696780348723347654245074569632238305433210
40+
056781084543787691230654218923336789430101109876543678
41+
145692985652310541021783204510421123321243278898694589
42+
232343898721023432012895145654530014562354569167783098
43+
621956782891236781763676034789691125671234321016012187
44+
560845021760545890854985421056787323980765012345323456
45+
456732130450674348921234678547845418345896721056891012
46+
308212345321189267630104549430989509216765892234732127
47+
219800456542010106549843230121678678107874763105645298
48+
056701067434329010178760101456563212078983654267836787
49+
147832198125018723269053212387654103147672101756921898
50+
232943219096527654450144528498987054234598767843410189
51+
981856901787403434543239609801256964374567056932501278
52+
870987897898912523692108718712345878985432145001652567
53+
561010762345809610787230125621236323476301232156743410
54+
654323451056718721676343234210987410565432343239874323
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
...0...
2+
...1...
3+
...2...
4+
6543456
5+
7.....7
6+
8.....8
7+
9.....9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
..90..9
2+
...1.98
3+
...2..7
4+
6543456
5+
765.987
6+
876....
7+
987....
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

0 commit comments

Comments
 (0)