Day 1 implementation in Java
parent
9df57c9733
commit
9b3ff2be57
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?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>org.example</groupId>
|
||||||
|
<artifactId>java</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -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<String> 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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<String> readContents() {
|
||||||
|
File file = new File(filePath);
|
||||||
|
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
|
||||||
|
Scanner scanner = new Scanner(inputStream);
|
||||||
|
scanner.useDelimiter("\n");
|
||||||
|
List<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue