commit 961570ce442e0d98a9fe1e6a1966354d7096ea07
parent 303748ab9224157ee7e752a192a7a9ff96fa167b
Author: Martin Ashby <martin@ashbysoft.com>
Date: Tue, 31 Dec 2024 21:16:04 +0000
Day 10 pt1
Diffstat:
3 files changed, 103 insertions(+), 0 deletions(-)
diff --git a/day10.txt b/day10.txt
@@ -0,0 +1,42 @@
+901403454308767601987430010102103430787676
+812312567219458912376541023411212321096787
+765403498012307433489432196520303234585898
+012354321087216524570343787434454100674301
+985467891096987019601234378976569981001210
+876654102345106508710765234789678972343454
+798783287765278710789820105678710169858763
+687690196894369821890112344589801258769012
+501543285210152152721201453078987340150103
+432457894101043043630328762101456498201232
+870326543232032154549419234652343567398341
+961210012343129069608500165765432985437650
+054301456953238778217671876893211076126789
+123012387869345632347889956544509089005491
+128723498778734101256970987832678179012310
+039614595654187214378921078981083238768723
+340507680343096785561434563470198740059654
+651408971252125896450001412563267651145098
+762310564561034587321112303214107892230127
+891023453478987878543203474905896543451936
+678921962101216969458914985876708712767845
+567630879870305456367325676765219603856696
+654544567765412343210232110894334564943787
+783203498894301210321143029889123765830654
+890112985435213456787054334778010894321003
+901209876120102365098765245667654784321212
+892100125009871074109670123454743065210012
+763119834312568983232189812303892178756763
+654098701233457654983074505412789109349854
+120145610145012523474563676545630201201340
+034234543296543210565012985654321347652201
+543007665487012365034876501745610458943102
+632118978329110874128989432891234367812983
+789327659418723923567876541010765298103894
+879454341507654010430765430199804105412765
+988761030676589012321650321783219876545630
+896902321585438543456761230654323401454321
+787813490492127654349854345013210562367212
+076524582341013434234981016724505678498101
+123433671034565521187832109834694989654342
+589652180123877610096543258943783878701233
+676543091012988987101234567652102107212344
diff --git a/day10_sample.txt b/day10_sample.txt
@@ -0,0 +1,8 @@
+89010123
+78121874
+87430965
+96549874
+45678903
+32019012
+01329801
+10456732
diff --git a/src/main/java/day10_pt1.java b/src/main/java/day10_pt1.java
@@ -0,0 +1,53 @@
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.*;
+
+public class day10_pt1 {
+ record Point(int x, int y) {
+ Point add(Vec v) {
+ return new Point(this.x + v.x, this.y + v.y);
+ }
+ }
+ record Vec(int x, int y) {}
+ private static final Vec[] DIRS = new Vec[]{new Vec( 0, 1),
+ new Vec( 1, 0),
+ new Vec( 0,-1),
+ new Vec(-1, 0)};
+ public static void main(String[] args) throws IOException {
+ var grid = Files.readAllLines(Path.of(args[0]));
+ var trailheads = new ArrayList<Point>();
+ var height = grid.size();
+ var width = grid.get(0).length();
+ // pass 1 find trailheads
+ for (int y=0; y<height; y++) {
+ for (int x=0; x<width; x++) {
+ var val = gv(grid, new Point(x, y));
+ if (val == 0) {
+ trailheads.add(new Point(x, y));
+ }
+ }
+ }
+ var res = trailheads.stream().mapToInt(th -> tops(grid, th).size()).sum();
+ System.out.printf("Day 10, pt1: %d\n", res);
+ }
+
+ static Set<Point> tops(List<String> grid, Point p) {
+ var val = gv(grid, p);
+ if (val == 9) {
+ return Set.of(p);
+ } else {
+ return Arrays.stream(DIRS)
+ .filter(d -> gv(grid, p.add(d)) == val+1)
+ .flatMap(d -> tops(grid, p.add(d)).stream())
+ .collect(Collectors.toSet());
+ }
+ }
+
+ static int gv(List<String> grid, Point p) {
+ if (p.y < 0 || p.y >= grid.size()) return -1;
+ if (p.x < 0 || p.x >= grid.get(0).length()) return -1;
+ return grid.get(p.y).charAt(p.x) - '0';
+ }
+}