From c24cc156f3759e69b8f00ab7257ddd055bfdd7f0 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 18 Dec 2024 20:14:13 -0500 Subject: [PATCH] Complete day 10 --- .../adventOfCode24/ProblemSelector.java | 7 +- .../adventOfCode24/days/Problem19.java | 177 ++++++++++++++++++ .../adventOfCode24/days/Problem20.java | 176 +++++++++++++++++ src/main/resources/days/Problem19.txt | 43 +++++ 4 files changed, 402 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mattrixwv/adventOfCode24/days/Problem19.java create mode 100644 src/main/java/com/mattrixwv/adventOfCode24/days/Problem20.java create mode 100644 src/main/resources/days/Problem19.txt diff --git a/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java b/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java index bcffff2..44221d6 100644 --- a/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java +++ b/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java @@ -17,7 +17,9 @@ import com.mattrixwv.adventOfCode24.days.Problem15; import com.mattrixwv.adventOfCode24.days.Problem16; import com.mattrixwv.adventOfCode24.days.Problem17; import com.mattrixwv.adventOfCode24.days.Problem18; +import com.mattrixwv.adventOfCode24.days.Problem19; import com.mattrixwv.adventOfCode24.days.Problem2; +import com.mattrixwv.adventOfCode24.days.Problem20; import com.mattrixwv.adventOfCode24.days.Problem3; import com.mattrixwv.adventOfCode24.days.Problem4; import com.mattrixwv.adventOfCode24.days.Problem5; @@ -32,7 +34,8 @@ public class ProblemSelector{ //Holds the valid problem numbers protected static final List PROBLEM_NUMBERS = List.of( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18 + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20 ); @@ -63,6 +66,8 @@ public class ProblemSelector{ case 16 : day = new Problem16(); break; case 17 : day = new Problem17(); break; case 18 : day = new Problem18(); break; + case 19 : day = new Problem19(); break; + case 20 : day = new Problem20(); break; default: throw new InvalidParameterException(); } return day; diff --git a/src/main/java/com/mattrixwv/adventOfCode24/days/Problem19.java b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem19.java new file mode 100644 index 0000000..5a3dd4a --- /dev/null +++ b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem19.java @@ -0,0 +1,177 @@ +package com.mattrixwv.adventOfCode24.days; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Scanner; +import java.util.Set; + +import com.mattrixwv.Stopwatch; +import com.mattrixwv.Triple; + +public class Problem19 extends Problem{ + private static final String inputFileName = "days/Problem19.txt"; + private List> map; + private List> trailHeadScores; + + + public Problem19(){ + super(); + description = "Find the sum of the trail head scores."; + result = "Unresolved"; + } + + + public String runSolution(){ + Stopwatch timer = new Stopwatch(); + timer.start(); + + + //Read the file + map = new ArrayList<>(); + try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){ + scanner.useDelimiter("\n"); + while(scanner.hasNext()){ + String mapLine = scanner.next(); + if(!mapLine.isBlank()){ + List mapArray = new ArrayList<>(); + for(char ch : mapLine.toCharArray()){ + mapArray.add(Integer.parseInt(Character.toString(ch))); + } + map.add(mapArray); + } + } + } + + + //Calculate the trail head scores + trailHeadScores = new ArrayList<>(); + for(int rowCnt = 0;rowCnt < map.size();++rowCnt){ + for(int colCnt = 0;colCnt < map.get(rowCnt).size();++colCnt){ + if(map.get(rowCnt).get(colCnt) == 0){ + Set> foundEndpoints = new HashSet<>(); + foundEndpoints.addAll(checkAllDirections(new Triple(rowCnt, colCnt, 0))); + trailHeadScores.add(new Triple(rowCnt, colCnt, foundEndpoints.size())); + } + } + } + + //Sum the trail head scores + int sum = 0; + for(Triple trailHeadScore : trailHeadScores){ + sum += trailHeadScore.getC(); + } + + + + //Save the results + timer.stop(); + result = "The sum of the trail head scores is " + sum + ".\nIt took " + timer.toString() + " to run this algorithm."; + return result; + } + + + private Set> checkAllDirections(Triple loc){ + Set> foundEndpoints = new HashSet<>(); + foundEndpoints.addAll(moveUp(loc)); + foundEndpoints.addAll(moveRight(loc)); + foundEndpoints.addAll(moveDown(loc)); + foundEndpoints.addAll(moveLeft(loc)); + return foundEndpoints; + } + + private Set> moveUp(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() <= 0) || (loc.getA() >= map.size()) || (loc.getB() < 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new HashSet<>(); + } + + Set> finishSet = new HashSet<>(); + //Check if we need to continue looking + if(map.get(loc.getA() - 1).get(loc.getB()) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA() - 1, loc.getB(), loc.getC() + 1); + if(newLoc.getC() == 9){ + finishSet.add(newLoc); + } + else{ + finishSet.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty set + //Return any values we found + return finishSet; + } + + private Set> moveRight(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= map.size()) || (loc.getB() < 0) || (loc.getB() >= (map.get(loc.getA()).size() - 1))){ + return new HashSet<>(); + } + + Set> finishSet = new HashSet<>(); + //Check if we need to continue looking + if(map.get(loc.getA()).get(loc.getB() + 1) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA(), loc.getB() + 1, loc.getC() + 1); + if(newLoc.getC() == 9){ + finishSet.add(newLoc); + } + else{ + finishSet.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty set + //Return any values we found + return finishSet; + } + + private Set> moveDown(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= (map.size() - 1)) || (loc.getB() < 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new HashSet<>(); + } + + Set> finishSet = new HashSet<>(); + //Check if we need to continue looking + if(map.get(loc.getA() + 1).get(loc.getB()) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA() + 1, loc.getB(), loc.getC() + 1); + if(newLoc.getC() == 9){ + finishSet.add(newLoc); + } + else{ + finishSet.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty set + //Return any values we found + return finishSet; + } + + private Set> moveLeft(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= map.size()) || (loc.getB() <= 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new HashSet<>(); + } + + Set> finishSet = new HashSet<>(); + //Check if we need to continue looking + if(map.get(loc.getA()).get(loc.getB() - 1) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA(), loc.getB() - 1, loc.getC() + 1); + if(newLoc.getC() == 9){ + finishSet.add(newLoc); + } + else{ + finishSet.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty set + //Return any values we found + return finishSet; + } +} + + +/* +Find the sum of the trail head scores. +The sum of the trail head scores is 510. +It took 16.725 milliseconds to run this algorithm. +*/ diff --git a/src/main/java/com/mattrixwv/adventOfCode24/days/Problem20.java b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem20.java new file mode 100644 index 0000000..4f12092 --- /dev/null +++ b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem20.java @@ -0,0 +1,176 @@ +package com.mattrixwv.adventOfCode24.days; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.mattrixwv.Stopwatch; +import com.mattrixwv.Triple; + + +public class Problem20 extends Problem{ + private static final String inputFileName = "days/Problem19.txt"; + private List> map; + private List> trailHeadScores; + + + public Problem20(){ + super(); + description = "Find the sum of the trail head scores."; + result = "Unresolved"; + } + + + public String runSolution(){ + Stopwatch timer = new Stopwatch(); + timer.start(); + + + //Read the file + map = new ArrayList<>(); + try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){ + scanner.useDelimiter("\n"); + while(scanner.hasNext()){ + String mapLine = scanner.next(); + if(!mapLine.isBlank()){ + List mapArray = new ArrayList<>(); + for(char ch : mapLine.toCharArray()){ + mapArray.add(Integer.parseInt(Character.toString(ch))); + } + map.add(mapArray); + } + } + } + + + //Calculate the trail head scores + trailHeadScores = new ArrayList<>(); + for(int rowCnt = 0;rowCnt < map.size();++rowCnt){ + for(int colCnt = 0;colCnt < map.get(rowCnt).size();++colCnt){ + if(map.get(rowCnt).get(colCnt) == 0){ + List> foundEndpoints = new ArrayList<>(); + foundEndpoints.addAll(checkAllDirections(new Triple(rowCnt, colCnt, 0))); + trailHeadScores.add(new Triple(rowCnt, colCnt, foundEndpoints.size())); + } + } + } + + //Sum the trail head scores + int sum = 0; + for(Triple trailHeadScore : trailHeadScores){ + sum += trailHeadScore.getC(); + } + + + //Save the results + timer.stop(); + result = "The sum of the trail head scores is " + sum + ".\nIt took " + timer.toString() + " to run this algorithm."; + return result; + } + + + private List> checkAllDirections(Triple loc){ + List> foundEndpoints = new ArrayList<>(); + foundEndpoints.addAll(moveUp(loc)); + foundEndpoints.addAll(moveRight(loc)); + foundEndpoints.addAll(moveDown(loc)); + foundEndpoints.addAll(moveLeft(loc)); + return foundEndpoints; + } + + private List> moveUp(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() <= 0) || (loc.getA() >= map.size()) || (loc.getB() < 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new ArrayList<>(); + } + + List> finishList = new ArrayList<>(); + //Check if we need to continue looking + if(map.get(loc.getA() - 1).get(loc.getB()) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA() - 1, loc.getB(), loc.getC() + 1); + if(newLoc.getC() == 9){ + finishList.add(newLoc); + } + else{ + finishList.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty list + //Return any values we found + return finishList; + } + + private List> moveRight(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= map.size()) || (loc.getB() < 0) || (loc.getB() >= (map.get(loc.getA()).size() - 1))){ + return new ArrayList<>(); + } + + List> finishList = new ArrayList<>(); + //Check if we need to continue looking + if(map.get(loc.getA()).get(loc.getB() + 1) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA(), loc.getB() + 1, loc.getC() + 1); + if(newLoc.getC() == 9){ + finishList.add(newLoc); + } + else{ + finishList.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty list + //Return any values we found + return finishList; + } + + private List> moveDown(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= (map.size() - 1)) || (loc.getB() < 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new ArrayList<>(); + } + + List> finishList = new ArrayList<>(); + //Check if we need to continue looking + if(map.get(loc.getA() + 1).get(loc.getB()) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA() + 1, loc.getB(), loc.getC() + 1); + if(newLoc.getC() == 9){ + finishList.add(newLoc); + } + else{ + finishList.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty list + //Return any values we found + return finishList; + } + + private List> moveLeft(Triple loc){ + //Check the current and next elements are in bounds + if((loc.getA() < 0) || (loc.getA() >= map.size()) || (loc.getB() <= 0) || (loc.getB() >= map.get(loc.getA()).size())){ + return new ArrayList<>(); + } + + List> finishList = new ArrayList<>(); + //Check if we need to continue looking + if(map.get(loc.getA()).get(loc.getB() - 1) == (loc.getC() + 1)){ + Triple newLoc = new Triple(loc.getA(), loc.getB() - 1, loc.getC() + 1); + if(newLoc.getC() == 9){ + finishList.add(newLoc); + } + else{ + finishList.addAll(checkAllDirections(newLoc)); + } + } + //If the next value is not correct, then return an empty list + //Return any values we found + return finishList; + } +} + + +/* +Find the sum of the trail head scores. +The sum of the trail head scores is 1058. +It took 8.705 milliseconds to run this algorithm. +*/ diff --git a/src/main/resources/days/Problem19.txt b/src/main/resources/days/Problem19.txt new file mode 100644 index 0000000..4c0594a --- /dev/null +++ b/src/main/resources/days/Problem19.txt @@ -0,0 +1,43 @@ +3212345678121056780310106543218765210109876 +7301106769012349891210237858909650987212345 +8943219878734217894354345997434501896398730 +7654306765025606765969876786521432345485621 +1233456834110715610870945431010676541014678 +0321067923230894320101234894321289239823549 +7450478810301129831230128765045658178101630 +8964329234532034761945219652136789017652721 +7875012187645695610876106543221054787243890 +4976701094556786921065987233234565692104323 +3289898763216547836976856100149874563495414 +4122305603407436545885445765454703454386901 +5001414512518921056794039870363212325677842 +6980523347676545121603128981278101210543234 +7879601258989236730512789974329892105601100 +0968708769076107845676697865012783416782321 +1651219940145045912389546521323676545699450 +2340367831231236705489030430654541236598769 +4565456328900987876018121046543010167894378 +3678995417811432980127630198672187098765298 +2980987606321501676534548767981096523030167 +1011876545450650101456759854102345614123454 +9872345034968763212321876543201298705323456 +8960101123879454789410989812120398676311237 +1051211012567345673508989401032367985200398 +2340349873498212232679876501001456810123478 +1232456780341000141089845432132345210194569 +0541542191232567657897894012201106761087654 +5670233088943498766501703025672239872398743 +4589104567652567898432612134984346765489232 +5498010988961043765430543210789655159896101 +4321023870870152896321432323658705014765012 +2349834561431161067816521874503216723454101 +1056767652521078154907890963212345894323221 +0148988943652189143878903454503454513411230 +3297803456789321012363212367696543201500549 +4586912369878934101454201008987012102676678 +5675001078767765210323123412108991343989654 +6784100981059874349014056503234780256978703 +7693201452342103458005347854345650107869012 +8543542367033012567176218961259654398054327 +9232632198124988943287307870568765212167898 +0101789087235677656896478969876890103456787