day 03 :')

This commit is contained in:
Sugui 2023-12-03 14:55:02 +01:00
parent fc5804a157
commit 6e80331dd3
5 changed files with 312 additions and 0 deletions

140
resources/input03.txt Normal file
View File

@ -0,0 +1,140 @@
........................617.........123...........341.........................293..................38..19.753..................533..........
565.......................-..............951.....+..........354.....697.58....*.....941............*.....*.........+....529....&.....36.....
....1.....225...73...................472.......................-....*......920..999.......646..771.433......407..405.....*.......426*.......
.....*....*........./227..-113........@...825/.....348...881......603...........%....793...=............235*..............472.........82.941
..360..432..997....................................*.....=............62...702......*..............................273..................*...
...........&.......833.489.......@.........176...895............503.......$.......493...............929...............*.302....492.526......
....................*.....+....85.......................601............................................*386......*...96...........*....*613.
.....650.360+...#..589..............................221*..............927...........941..404..+669..............823.................360.....
.527...........919.................799.....................&............*...............-..............485............-..754................
....&....*187........./149............*.....................653........84......120...............-.....=....581...574.76....*287..968.......
.......26..................497........254........#..682..........$.236..........................111............*...=.............*..........
....79.......394.......112.-...762.............847....*........450.*...$...495&........$.49.............#.....213...................171.....
.....$.......*..........$....*....*136....594......134....&............991..........292.................374.......123......676.........$....
...........957...&..740......631..........*.............403......186.........................................260...*..308...................
................582..*...............463.......524....-.....883...*..463..........389....71......+594..........*...45..+....................
.815....520..........747......@......*...588.....-....14...*.....632....*.946........../...............$982...927.....................822...
...........*..............271.889.631......................889........951.........363.774...............................%975...546....*.....
........709.......561.....-.................+885...................................................121..............794.......&....450......
............469.............478.44...799..........273..218.....361..........763.88............977.....*.969..............528................
...30.=........%......746.....*.*.........................*401.*...............*.........27......*.......*.................@.....#....572...
...*..749..............+...573...286..539........110...........101................241...........132.217..736...................714..........
.........................................*976...*....897.136.......@............./..........338........*......149.....894..874..............
...267*537.................850..864.262..........178..*..+......721.......314..............$........834.........%..........@................
...............966.479.............*.....448.........98....25#......................524.................728.871....../.............258......
....109...........*.....570....775......*...............82.............933.107*......*...415..109............*....795...*953....#...........
......%..................=.....*....408.277.677...143...*................*.....792..501..*....*........354.629........81.......899..695.....
................*166............98.%.........-....*...677.......+.........................955..26.....#....................103......+...140.
.............286..................................638........158...........4.&....................57..............-...........*......../....
...968...............112.@139.....102..............................860.546...922.847.....888*390..........88.....796.........507............
...*...........550..*................*493.................54*299......*................................$.................348........%.......
672...........+....142......................................................548..343....................843........&.....*.......773.....3..
.........791.........................................................&..#........*.........383..716.........599...415.155...................
.565......%..............49........................712$............159...854......662...-..*....=.....*.......*............173..973.........
......./.................*....624......905....590......................................271..445....958.42....531......293..*............866.
....645..877.............304...$...761*......*.................718*495...............................................*.....587.....815+.....
.........*......958*319......................40............678......................701*422....37......357............170...................
..........493...................438.224...............&.....*.............814..................*...832*........%863................448......
......-.............727.....412.*....*...........@...514..563...............*.....853*194.986.475.....................................@.....
......274.....810....*..874.../.688.40...271..646...........................66..............*........351..........*122.97&.435..............
.236...............755....*./...............%..............112...................492.......798........*...946..717........./...........453..
...*...........509.....766..169....&.........................*......................................719......=.........998.......*....*.....
309....896......*................773...........+..155.797..#.........164......296........................................-.......312.452....
..........*..216.............670..............90..*.........705.......=.......*...893.214.....655.......439*158..345.$.......51.............
....#....746..................@.....874..*.........46............456...........26.*...*..........*...............#....491...*...421*795.....
....596...............578&..&.........@..190.256................*......614........769.47........7.......$..+405.............536.............
.............868.973........613.+.......................516........431*...........................613..759......+.....546.......441...&.....
.....134*82.*.....*..483=.......940.987....................*...................13....-..............*..........184.............*....789.....
...............697........347-.................23.#........27.568/..............*....312.&373....889.................%........682.......673.
.........930..........141..............997.....%..528.=829..........254.................................55.......690.765....#...........$...
...655....*.............*....636..........*....................36..$..................503.....*417.....*.....942*...........67..92$.........
.....*....397...999......441...*....568...686............................................=.376............-.............683.................
713...79..........@..........423....$............935...................899...303$....=..........579.......720.508.......%....=..............
............416.......22.....................$..*............624.........*...........922.......*................&...........63......977.....
...........+..........*..........*27.......943...35...........-..........83..210...........497.854...28.......&.....*344....................
662..892..............146............................886.........503........$..............*...............253...678..........441...........
......*...814.......*.......%....../203...35......68*....#.......*.....579...............35.....650...../......+......716............901....
506..152.*........777......551..............*898........225....728........*224...................*.....425......917..............500...*....
...*......984...........................861.........&22............30...+........353........77.565............#............405...@....873...
...99...........109....490&..445...322../............................-..135.......*.........+........859...454................*....&........
.................*............*.....@.........927.@738......925..797..............503..618.............=.....................562.643........
......546.358.281....247/...831..........977....*.........../....*............553.....*.....994.343......................880...........-949.
..721...@..........................305........590....410..........94.296......*......19....*..............202......88+......-...............
....*........590..................%....542..........+..................*......582...........229..................&......-........273..585...
....623.......@.............771........=.........+.........737&.......683...............*.......*.......613...769........849......*..@......
........722.....571..725.....*.................758..........................209...584...162..826.246......*..................212.487...774..
....352.........*...../......269.669.....................527..537...........*.......+....................649......368....251*...............
165....+.708.675..79.....408...........612.....&.........+......%..244@....399........912...364-..............17.....................547....
...*.....*........=.....*.................*...551......+.................................*.........726.257...*..............623..839.*......
.478.....768..........935.......194..813..890..........290..911...261..........698..450...340.........*.....361..280...............*........
..................820.......548*...../............................*...941$.......#....*..........................*....406...........621.....
....@.672.............177...............868..716..965.............246..............444...%164.....................44.@......931.............
..540...#.74.............*.....875...90*......*.....*..962................................................107@..............................
...........%.791.......95.........*..........166.$.....#......615*470.......333/....73........5..449...........@.........................896
....227......*...582%......17#....................703.....@...........................*.733...*...../........216.495........383*289.........
...#.........255.................%.......&...434.......270.....712..................628..+...893....................-.280...................
...................171..750.......312....733......%............./......*887...@.............................................................
327..+295..700@....+......=.....................158.................357.......930....................645..........399....149...-......716...
.....................966.......378........................96............576.+................58.....+.............*.........*.114.....-.....
....46$....................776*.....522......................$.........*....70.....534.......*..990........892...556.....131................
.............98....-.587........697*..../729..@...352....688..987....647.......974.........706....*..........%..............................
...47...498.....121.../.......................218..*........*................-...%..647*.......808................&...../...647.............
..../.......................880...996.............351.$...276.......937*636.4...........75...................502..43.156....................
.......141..140......-..976*..........................853.....=402.........................487........648+../..............*124.............
..........+...*...868.........*....158.....559.......................128.....476........@.-...................@..986....729.................
..............663..............114.*...363*........909*961...........*........*.......897....$.............678..$...............725.........
..886.329..................324.....722........287..................$.81....166.............555.929..................414.............292/....
.....*.....$....947..767...-....................*.......527..111.293.............726...........*...553....../665.............950............
171........937......*........751.................993...../....*...........915......=......651.519.....*..........426...21/..*........907....
...*560...............................217...................347...429.......*.........50...........293.......160*.............*698..*.......
..........49...................55.....*....130......305@..........=....459..141.380*...................920........501......432......301.....
211*......*...................*......29.......*...........586.............*.........866...............*..........*..........................
....766.102.901.....*...........696.....@.....179..969......+.............513............840#......#...639.....102..........................
..............@..366.710.$545....+...654...........*............133.............209...............675................41........122..........
.............................................430*...75..........*........889..............761.702..........*379...............*.............
...958...905..........*718....*932......585............650...689.....177.....840.........*.......*......869.............107...170...526.912.
...........$.......268.....812.........../...941..........*......893*........*.........971..614..452.........207.......*...............*....
.293.............................286.........*.........432............*83..499..682..........@................=.....257..........520.....977
.....767...711..$683......*......*..........419.625#...........788.549............+.712........411..946.........@...........................
.......*....=...........58.991..412.42.222......................*.......79..978.....*............%.*........*58..389......616.........686...
....131............67.......................995............926.561......*....*....406.273...........490..611...................634$.........
..........908.320........................................................725.533.......*...624.....................198*246.209.........#....
............*...@....594..298....743...601......123......@......@606...$..............439.$.....#....../175...386.............*......490....
...16....371........*.....-........*...........=..........202..........373.....6.749............28................*....675.....529..........
...#.............662...............422.............&.......................462....*.........139...........175@.....376.+....................
.......................+..@325...........18.....543....................../....%.699.....611*..........240...................................
...............297...754....................................15......790..785........474.................+.........-.......719.....962.......
198*...........@..........252...................413...*800......@................./.*...............951...@899...748....#.....486*..........
....295....334....900.......*..................*....37.........306..............263.737.......722......-................651.............679.
...........*.....=.......150.......535.531....609..................287.....@.................*....519..............92............*8....*....
904......139.........995.......@..*.......*.*.....933...229........../...45.......361.206.....37.*..........730......&...226..585......553..
........................*...276.......$.435..582..@.....*........185...............#....*.559.....471..........*...........*......974.......
......285..........493.61...........81..................844.......*.......424........330..*.....................342..994.222.......*........
..492*.....456*326..*....................728....705.............973.........*............313.554*......596............*.........288..958....
.....................247..........*508.....+.....*.....#...................789.....................861*................936...........#......
.589....368......956.......#...469......................757..361....144*..................................=......*..........................
........-.......$....80.822........560...........172....................212........=552..*.....#....%..858...462.739.....343........22......
..........=........................*.......170..*..............................413.......368.264.622...........-............*........*......
...601..505.234.&371...........123.............739.812......429.................%...788....................................277..967...170...
....*.......*........547...........%.....849........*......*.......532...309@.........*..................536....581...352.........-.........
.....840.....636.996*.......399.....455..............35.723..934..*............842...875....90...........*..............*...992......@......
.........817...........#462....*............$788................*..615.469........*...........*..261.....774.........830......*...624.......
.........*..................607...............................117.................532...............$....................950..676...........
..749*12..558..62...+199........492......870.....719........................548.........353......................486.....-........698*61....
..................................-..514*....25@...............681..*975..........*437....*....99..............@.*.........+................
....363...296.....350.............................457.....421-....*.......*234.980.........339.#.............660.443...$.645.132.....930....
........$.....*....*........617.........712.......*..............122...237............491........../452..............445.......#.342...@....
........903.422....854.643...*......942...*.......334................................*.......855*..........507....................@.........
.326...................*....426.838.*.....948...................868...635....635.....401.........810.........#........887...40.......743....
............879......889..#.........252.............496....*...........#..........55........370..................39..$.....*....331..*......
..506......&...............487..............794....*.....55.................262.....*..465..*.............309......=.......460...*..431.....
.....#............................*......41.........911........./.495.....................*.241..........*....509.....314.......329.........
...............627....15.706...277.276........................68.............*.........................491.......*........#898......%956....
...........830....*12..*....*.................219.112......................952.....637*........-..539..........999.316.2..........%.........
....../.......*.......132..577.595......426..*......*.3....#380.......681+...............460..829.*..................=.....223.....615......
......726...811...........................+..91..980..*........................$..........*.......639..................193.%............403.
...................358*............633................526....266.........666...534.....662........................+.....$.........758...*...
.......=.......137.....313.........=.............998......&....*..........*.....................559..313..825=.....353....405.........296...
....447...........#...........342....%.....%........*..938......238.....327..............*152......@...*...................%..472.153.......
.............152#............*......792...334......741........................570*....335..............137..........338..........*......+...
952.........................................................793......583..........623............11........730............50.116.........446

View File

@ -0,0 +1,106 @@
package sugui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import sugui.util.IntegerRef;
import sugui.util.Pair;
public class Day03 {
public static Path inputPath = Paths.get("resources/input03.txt");
private static Map<Pair<Integer, Integer>, IntegerRef> numbers = new HashMap<>();
private static Map<Pair<Integer, Integer>, String> symbols = new HashMap<>();
public static void main(String[] args) throws IOException {
var input = Files.readString(inputPath);
var firstResult = getFirstPuzzleResult(input);
var secondResult = getSecondPuzzleResult(input);
System.out.println("First result: " + firstResult);
System.out.println("Second result: " + secondResult);
}
private static void loadNumbersAndSymbols(String input) {
var bufferedReader = new BufferedReader(new StringReader(input));
var lines = bufferedReader.lines().collect(Collectors.toList());
int lineNumber = 0;
for (String line : lines) {
Pattern numberPattern = Pattern.compile("(\\d+)");
Pattern symbolPattern = Pattern.compile("[^\\d|\\.]");
Matcher numberMatcher = numberPattern.matcher(line);
Matcher symbolMatcher = symbolPattern.matcher(line);
while (numberMatcher.find()) {
IntegerRef numberRef = new IntegerRef(Integer.parseInt(numberMatcher.group()));
for (int col = numberMatcher.start(); col < numberMatcher.end(); col++) {
numbers.put(new Pair<>(col, lineNumber), numberRef);
}
}
while (symbolMatcher.find()) {
String symbol = symbolMatcher.group();
symbols.put(new Pair<>(symbolMatcher.start(), lineNumber), symbol);
}
lineNumber++;
}
}
public static String getFirstPuzzleResult(String input) {
loadNumbersAndSymbols(input);
int sumOfPartNumbers = 0;
Set<IntegerRef> visitedNumbers = new HashSet<>();
for (var symbolPositions : symbols.keySet()) {
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy);
if (numbers.containsKey(position)) {
IntegerRef numberRef = numbers.get(position);
if (!visitedNumbers.contains(numberRef)) {
visitedNumbers.add(numberRef);
sumOfPartNumbers += numberRef.value();
}
}
}
}
}
return Integer.toString(sumOfPartNumbers);
}
public static String getSecondPuzzleResult(String input) {
loadNumbersAndSymbols(input);
int sumOfGearRatios = 0;
for (var symbolPositions : symbols.keySet()) {
if ("*".equals(symbols.get(symbolPositions))) {
Set<IntegerRef> visitedNumbers = new HashSet<>();
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
var position = new Pair<>(symbolPositions._1() + dx, symbolPositions._2() + dy);
if (numbers.containsKey(position)) {
IntegerRef numberRef = numbers.get(position);
if (!visitedNumbers.contains(numberRef)) {
visitedNumbers.add(numberRef);
}
}
}
}
if (visitedNumbers.size() == 2) {
int gearRatio = visitedNumbers.stream().map(iRef -> iRef.value()).reduce(1, (a, b) -> a * b);
sumOfGearRatios += gearRatio;
}
}
}
return Integer.toString(sumOfGearRatios);
}
}

View File

@ -0,0 +1,13 @@
package sugui.util;
public class IntegerRef {
private int value;
public IntegerRef(int value) {
this.value = value;
}
public int value() {
return value;
}
}

View File

@ -0,0 +1,12 @@
package sugui.util;
public record Pair<T1, T2>(T1 _1, T2 _2) {
@Override
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair<?, ?> pair = (Pair<?, ?>) o;
return _1.equals(pair._1) && _2.equals(pair._2);
}
return false;
}
}

View File

@ -0,0 +1,41 @@
package sugui;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class Day03Test {
public static String firstBasicInput = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
""";
public static String secondBasicInput = firstBasicInput;
public static String firstBasicResult = "4361";
public static String secondBasicResult = "467835";
@Test
public void firstBasicCase() throws IOException {
String actualResult = Day03.getFirstPuzzleResult(firstBasicInput);
assertEquals(firstBasicResult, actualResult);
}
@Test
public void secondBasicCase() throws IOException {
String actualResult = Day03.getSecondPuzzleResult(secondBasicInput);
assertEquals(secondBasicResult, actualResult);
}
}