From 5e4c23281a0f81093dfc2053dd253e71dda6f6a3 Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Sun, 3 Dec 2023 19:59:25 +0100 Subject: [PATCH] Initialize Project and add Solution for day 1 part 1 --- .gitignore | 19 ++++++++++++ README.md | 10 +++++++ input/.gitkeep | 0 pom.xml | 18 +++++++++++ src/main/java/de/dhbw/horb/App.java | 46 +++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 input/.gitkeep create mode 100644 pom.xml create mode 100644 src/main/java/de/dhbw/horb/App.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ce8b0e --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath +# Intellij Idea +.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..3d18eb3 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Advent of Code in Java ☕ + +## Usage +1. fork this Repository +2. Install dependencies + - [Maven](https://maven.apache.org/install.html) + - [Java 17](https://www.oracle.com/java/technologies/downloads/#java17) +3. Create file `day1.txt` with puzzle input in `input` folder +4. Run the project 🚀 with `mvn package && java -cp target/AdventOfCode-1.0-SNAPSHOT.jar de.dhbw.horb.App` + - or open and run in your favorite IDE diff --git a/input/.gitkeep b/input/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3f28305 --- /dev/null +++ b/pom.xml @@ -0,0 +1,18 @@ + + + + 4.0.0 + + de.dhbw.horb + AdventOfCode + 1.0-SNAPSHOT + + AdventOfCode + + + UTF-8 + 17 + 17 + + diff --git a/src/main/java/de/dhbw/horb/App.java b/src/main/java/de/dhbw/horb/App.java new file mode 100644 index 0000000..b77b4af --- /dev/null +++ b/src/main/java/de/dhbw/horb/App.java @@ -0,0 +1,46 @@ +package de.dhbw.horb; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + * Advent of Code with sample Solution for Day 1 (Part 1) + */ +public class App +{ + public static void main( String[] args ) + { + String input = readInput("day1.txt"); + int total = 0; + for(String line : input.split("\n")) { + char last = 0; + char first = 0; + //Get last number + for(int i = 0; i < line.length(); i++){ + char c = line.charAt(i); + if(Character.isDigit(c)){ + last = c; + } + } + //Get first + for(int i = line.length() - 1; i >= 0; i--){ + char c = line.charAt(i); + if(Character.isDigit(c)){ + first = c; + } + } + //Parse and add: + total += Integer.parseInt(Character.toString(first) + Character.toString(last)); + } + System.out.println(total); + } + + public static String readInput(String filename) { + try{ + return Files.readString(Paths.get(System.getProperty("user.dir"), "input", filename)); + } catch (IOException e){ + throw new RuntimeException(e); + } + } +}