From 9b3ff2be570ef32617a76d3552b64e380e794c40 Mon Sep 17 00:00:00 2001 From: MiguelMLorente Date: Fri, 1 Dec 2023 23:53:57 +0100 Subject: [PATCH] Day 1 implementation in Java --- java/pom.xml | 16 +++++++ java/src/main/java/day1/Main.java | 58 +++++++++++++++++++++++++ java/src/main/java/util/FileReader.java | 32 ++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 java/pom.xml create mode 100644 java/src/main/java/day1/Main.java create mode 100644 java/src/main/java/util/FileReader.java diff --git a/java/pom.xml b/java/pom.xml new file mode 100644 index 0000000..01a7a72 --- /dev/null +++ b/java/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.example + java + 1.0-SNAPSHOT + + + 8 + 8 + + + \ No newline at end of file diff --git a/java/src/main/java/day1/Main.java b/java/src/main/java/day1/Main.java new file mode 100644 index 0000000..9cf568d --- /dev/null +++ b/java/src/main/java/day1/Main.java @@ -0,0 +1,58 @@ +package day1; + +import util.FileReader; + +import java.util.List; +import java.util.stream.Collectors; + +public class Main { + // Part 1 one of the problem has this flag set to false, while part 2 has it set to true + private static final boolean IS_NUMBER_SPELLING_ENABLED = true; + + public static void main(String[] args) { + String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day1\\input.txt"; + FileReader reader = new FileReader(inputFilePath); + List inputData = reader.readContents(); + + Long result = inputData.stream() + .map(string -> IS_NUMBER_SPELLING_ENABLED ? replaceSpelledDigits(string) : string) + .map(string -> string.chars() + .mapToObj(encodedChar -> (char) encodedChar) + .collect(Collectors.toList())) + .map(charArray -> charArray.stream() + .filter(Character::isDigit) + .map(String::valueOf) + .map(Long::parseLong) + .collect(Collectors.toList())) + .map(longArray -> { + Long first = longArray.get(0); + Long last = longArray.get(longArray.size() - 1); + return first * 10 + last; + }) + .reduce(Long::sum) + .orElseThrow(RuntimeException::new); + + System.out.println("Result: " + result); + } + + private static String replaceSpelledDigits(String string) { + return string + .replaceAll("oneight", "18") + .replaceAll("threeight", "38") + .replaceAll("fiveight", "58") + .replaceAll("nineight", "98") + .replaceAll("twone", "21") + .replaceAll("eightwo", "82") + .replaceAll("eighthree", "83") + .replaceAll("one", "1") + .replaceAll("two", "2") + .replaceAll("three", "3") + .replaceAll("four", "4") + .replaceAll("five", "5") + .replaceAll("six", "6") + .replaceAll("seven", "7") + .replaceAll("eight", "8") + .replaceAll("nine", "9"); + } +} + diff --git a/java/src/main/java/util/FileReader.java b/java/src/main/java/util/FileReader.java new file mode 100644 index 0000000..56a1467 --- /dev/null +++ b/java/src/main/java/util/FileReader.java @@ -0,0 +1,32 @@ +package util; + +import java.io.File; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class FileReader { + private final String filePath; + + public FileReader(String filePath) { + this.filePath = filePath; + } + + public List readContents() { + File file = new File(filePath); + try (InputStream inputStream = Files.newInputStream(file.toPath())) { + Scanner scanner = new Scanner(inputStream); + scanner.useDelimiter("\n"); + List data = new ArrayList<>(); + while (scanner.hasNext()) { + data.add(scanner.next()); + } + return data; + } catch (Exception e) { + System.out.println("Error while parsing input data"); + throw new RuntimeException(e); + } + } +}