Advertisement
Guest User

Advent of Code 2023 Day 1

a guest
Dec 2nd, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | Source Code | 0 0
  1. package adventofcode2023day01;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Main {
  10.  
  11.   public static void main(String[] args) {
  12.     System.out.println("Part 1: " + new Part1().result(Main.class.getResourceAsStream("/input.txt")));
  13.     System.out.println("Part 2: " + new Part2().result(Main.class.getResourceAsStream("/input.txt")));
  14.   }
  15.  
  16.   static class Part1 extends AoC2023Day1Processor {
  17.  
  18.     @Override
  19.     protected int firstDigit(String line) {
  20.       for (int i = 0; i < line.length(); ++i) {
  21.         var character = line.charAt(i);
  22.         if (character >= '0' && character <= '9') {
  23.           return character - '0';
  24.         }
  25.       }
  26.       return 0;
  27.     }
  28.  
  29.     @Override
  30.     protected int lastDigit(String line) {
  31.       for (int i = line.length() - 1; i >= 0; --i) {
  32.         var character = line.charAt(i);
  33.         if (character >= '0' && character <= '9') {
  34.           return character - '0';
  35.         }
  36.       }
  37.       return 0;
  38.     }
  39.  
  40.   }
  41.  
  42.   static class Part2 extends AoC2023Day1Processor {
  43.  
  44.     private static final String DIGIT_PATTERN_STRING = "(zero|one|two|three|four|five|six|seven|eight|nine|\\d)";
  45.     private static final Pattern FIRST_DIGIT_PATTERN = Pattern.compile("^" + DIGIT_PATTERN_STRING + ".*");
  46.     private static final Pattern LAST_DIGIT_PATTERN = Pattern.compile(".*" + DIGIT_PATTERN_STRING + "$");
  47.  
  48.     @Override
  49.     protected int firstDigit(String line) {
  50.       for (int i = 0; i < line.length(); ++i) {
  51.         var matcher = FIRST_DIGIT_PATTERN.matcher(line.substring(i));
  52.         if (matcher.matches()) {
  53.           return toDigit(matcher.group(1));
  54.         }
  55.       }
  56.       return 0;
  57.     }
  58.  
  59.     @Override
  60.     protected int lastDigit(String line) {
  61.       for (int i = line.length() - 1; i >= 0; --i) {
  62.         var matcher = LAST_DIGIT_PATTERN.matcher(line.substring(0, i + 1));
  63.         if (matcher.matches()) {
  64.           return toDigit(matcher.group(1));
  65.         }
  66.       }
  67.       return 0;
  68.     }
  69.  
  70.     private static int toDigit(String digitString) {
  71.       return switch (digitString) {
  72.       case "zero", "0" -> 0;
  73.       case "one", "1" -> 1;
  74.       case "two", "2" -> 2;
  75.       case "three", "3" -> 3;
  76.       case "four", "4" -> 4;
  77.       case "five", "5" -> 5;
  78.       case "six", "6" -> 6;
  79.       case "seven", "7" -> 7;
  80.       case "eight", "8" -> 8;
  81.       case "nine", "9" -> 9;
  82.       default -> throw new IllegalArgumentException();
  83.       };
  84.     }
  85.  
  86.   }
  87.  
  88.   abstract static class AoC2023Day1Processor {
  89.  
  90.     int result(InputStream inputStream) {
  91.       try (var reader = new BufferedReader(new InputStreamReader(inputStream))) {
  92.         int result = 0;
  93.         for (var line = reader.readLine(); line != null; line = reader.readLine()) {
  94.           result += (10 * firstDigit(line) + lastDigit(line));
  95.         }
  96.         return result;
  97.       } catch (IOException e) {
  98.         throw new RuntimeException(e);
  99.       }
  100.     }
  101.  
  102.     protected abstract int firstDigit(String line);
  103.  
  104.     protected abstract int lastDigit(String line);
  105.  
  106.   }
  107.  
  108. }
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement