From e698c6b5c3ab69fa43c77d6bd71127549868cd2f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 4 Dec 2023 12:16:24 -0500 Subject: [PATCH] Added java solution, updated readme. --- README.md | 3 ++- day_1/solution.java | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 day_1/solution.java diff --git a/README.md b/README.md index 5783564..6a0f23e 100644 --- a/README.md +++ b/README.md @@ -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). -- 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 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. diff --git a/day_1/solution.java b/day_1/solution.java new file mode 100644 index 0000000..b8b8327 --- /dev/null +++ b/day_1/solution.java @@ -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(); + } +} \ No newline at end of file