This commit is contained in:
Sugui 2023-12-01 17:49:55 +01:00
commit 3a7e2e235b
6 changed files with 1202 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode/
target/

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# aoc23
Advent of Code 2023

74
pom.xml Normal file
View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sugui</groupId>
<artifactId>aoc23</artifactId>
<version>1.0-SNAPSHOT</version>
<name>aoc23</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

1000
resources/input01.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,82 @@
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.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.IntPredicate;
public class Day01 {
public static Path inputPath = Paths.get("resources/input01.txt");
public static Map<String, List<String>> spelledDigitsMap = Map.of(
"1", List.of("1", "one"),
"2", List.of("2", "two"),
"3", List.of("3", "three"),
"4", List.of("4", "four"),
"5", List.of("5", "five"),
"6", List.of("6", "six"),
"7", List.of("7", "seven"),
"8", List.of("8", "eight"),
"9", List.of("9", "nine"));
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);
}
public static int getFirstPuzzleResult(String input) throws IOException {
IntPredicate isDigit = c -> Character.isDigit(c);
var bufferedReader = new BufferedReader(new StringReader(input));
var result = bufferedReader.lines()
.map(line -> {
var digits = line
.chars()
.filter(isDigit)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
var number = digits.isEmpty() ? 0
: Integer.parseInt(
String.format(
"%c%c",
digits.charAt(0),
digits.charAt(digits.length() - 1)));
return number;
})
.reduce(0, (a, b) -> a + b);
return result;
}
public static int getSecondPuzzleResult(String input) throws IOException {
var bufferedReader = new BufferedReader(new StringReader(input));
var result = bufferedReader.lines()
.map(line -> {
Map<Integer, String> positions = new HashMap<>();
spelledDigitsMap.forEach((digit, spelledDigits) -> {
spelledDigits.forEach(spelled -> {
int firstIndex = line.indexOf(spelled);
int lastIndex = line.lastIndexOf(spelled);
if (firstIndex != -1) {
positions.put(firstIndex, digit);
positions.put(lastIndex, digit);
}
});
});
int maxPosition = Collections.max(positions.keySet());
int minPosition = Collections.min(positions.keySet());
int number = Integer.parseInt(positions.get(minPosition) + positions.get(maxPosition));
return number;
}).reduce(0, (a, b) -> a + b);
return result;
}
}

View File

@ -0,0 +1,42 @@
package sugui;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class Day01Test {
public static String firstBasicInput = """
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
""";
public static String secondBasicInput = """
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
""";
public static int firstBasicResult = 142;
public static int secondBasicResult = 281;
@Test
public void firstBasicCase() throws IOException {
int actualResult = Day01.getFirstPuzzleResult(firstBasicInput);
assertEquals(firstBasicResult, actualResult);
}
@Test
public void secondBasicCase() throws IOException {
int actualResult = Day01.getSecondPuzzleResult(secondBasicInput);
assertEquals(secondBasicResult, actualResult);
}
}