Added java solution, updated readme.

This commit is contained in:
Andrew Lalis 2023-12-04 12:16:24 -05:00
parent f2c47597ff
commit e698c6b5c3
2 changed files with 66 additions and 1 deletions

View File

@ -2,7 +2,8 @@
This repository has my solutions for this year's Advent of Code challenges. Each day's challenge will be placed into a directory named `day_N`, where `N` is the day of the challenge. Each day will have a solution program named `solution.*` (with the filetype depending on which programming language I used). This repository has my solutions for this year's Advent of Code challenges. Each day's challenge will be placed into a directory named `day_N`, where `N` is the day of the challenge. Each day will have a solution program named `solution.*` (with the filetype depending on which programming language I used).
- For D programs, you can simply run them with `rdmd solution.d`. - For D programs, you can run them with `rdmd solution.d`.
- For Lua programs, you can run them with `lua solution.lua`. - For Lua programs, you can run them with `lua solution.lua`.
- For Java programs, you can run them with `java solution.java`.
Note that I try and solve the problems as fast as possible, but then I go back and clean up my solution, so what you see isn't really my ugly first attempt. Note that I try and solve the problems as fast as possible, but then I go back and clean up my solution, so what you see isn't really my ugly first attempt.

64
day_1/solution.java Normal file
View File

@ -0,0 +1,64 @@
import java.nio.file.Files;
import java.nio.file.Path;
class solution {
private interface DigitFinder {
int getDigit(String s, int idx);
}
private static int parseDigits(DigitFinder df, String s) {
int a = -1, b = -1;
for (int i = 0; i < s.length(); i++) {
int an = df.getDigit(s, i);
int bn = df.getDigit(s, s.length() - i - 1);
if (a == -1 && an != -1) a = an;
if (b == -1 && bn != -1) b = bn;
if (a != -1 && b != -1) break;
}
return a * 10 + b;
}
private static class Part1DigitFinder implements DigitFinder {
public int getDigit(String s, int idx) {
if (Character.isDigit(s.charAt(idx))) {
return s.charAt(idx) - '0';
}
return -1;
}
}
private static void part1() throws Exception {
var df = new Part1DigitFinder();
int sum = Files.readAllLines(Path.of("part_1_input.txt")).stream()
.mapToInt(line -> parseDigits(df, line))
.sum();
System.out.println(sum);
}
private static class Part2DigitFinder implements DigitFinder {
public int getDigit(String s, int idx) {
if (Character.isDigit(s.charAt(idx))) return s.charAt(idx) - '0';
String[] words = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (s.length() - idx >= word.length() && s.substring(idx, idx + word.length()).equals(word)) {
return i + 1;
}
}
return -1;
}
}
private static void part2() throws Exception {
var df = new Part2DigitFinder();
int sum = Files.readAllLines(Path.of("part_2_input.txt")).stream()
.mapToInt(line -> parseDigits(df, line))
.sum();
System.out.println(sum);
}
public static void main(String[] args) throws Exception {
part1();
part2();
}
}