Skip to content

Commit

Permalink
Add part two and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorris180 committed Dec 7, 2023
1 parent b51e329 commit 1cbda2b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
11 changes: 7 additions & 4 deletions inputs/day01.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
46 changes: 30 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.jonathanmorris.aoc</groupId>
<artifactId>aoc</artifactId>
<version>1.0-SNAPSHOT</version>

<name>aoc</name>
<url>https://github.com/jonathanmorris180/aoc</url>

<properties>
<service.mainClass>com.jonathanmorris.Main</service.mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spotless.version>2.41.1</spotless.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand All @@ -42,21 +37,40 @@
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<!--formatter-->
<dependency>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!--formatter-->
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.23.0</version>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<directories>
<directory>${project.build.sourceDirectory}</directory>
<directory>${project.build.directory}/generated-sources</directory>
</directories>
<java>
<!-- These are the defaults, you can override if you want -->
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<googleJavaFormat>
<version>1.18.0</version>
<style>GOOGLE</style>
</googleJavaFormat>
<importOrder/>
<!-- standard import order -->
<removeUnusedImports/>
<!-- self-explanatory -->
<formatAnnotations/>
<!-- fixes formatting of type annotations, see below -->
</java>
</configuration>
</plugin>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jonathanmorris/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.List;

public interface Challenge {
public void execute(List<String> lines);
public void execute(List<String> lines);
}
39 changes: 18 additions & 21 deletions src/main/java/com/jonathanmorris/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Hello world!
*
*/
/** Hello world! */
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final String FILE_NAME_PLACEHOLDER = "inputs/dayXX.txt";
private static final String CLASS_NAME_PLACEHOLDER = "com.jonathanmorris.challenges.DayXXChallenge";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final String FILE_NAME_PLACEHOLDER = "inputs/dayXX.txt";
private static final String CLASS_NAME_PLACEHOLDER =
"com.jonathanmorris.challenges.DayXXChallenge";

public static void main(final String... args) {
logger.info("Running with args: {}", (Object) args);
String className = CLASS_NAME_PLACEHOLDER.replace("XX", args[0]);
public static void main(final String... args) {
logger.info("Running with args: {}", (Object) args);
String className = CLASS_NAME_PLACEHOLDER.replace("XX", args[0]);

try {
String fileName = FILE_NAME_PLACEHOLDER.replace("XX", args[0]);
logger.info("Reading file: {}", className);
List<String> lines = Files.readAllLines(Paths.get(fileName));
Object challenge = Class.forName(className).getConstructor().newInstance();
logger.info("Executing challenge: {}", className);
((Challenge) challenge).execute(lines);
} catch (Exception e) {
e.printStackTrace();
}
try {
String fileName = FILE_NAME_PLACEHOLDER.replace("XX", args[0]);
logger.info("Reading file: {}", className);
List<String> lines = Files.readAllLines(Paths.get(fileName));
Object challenge = Class.forName(className).getConstructor().newInstance();
logger.info("Executing challenge: {}", className);
((Challenge) challenge).execute(lines);
} catch (Exception e) {
e.printStackTrace();
}
}
}
84 changes: 53 additions & 31 deletions src/main/java/com/jonathanmorris/challenges/Day01Challenge.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,69 @@
package com.jonathanmorris.challenges;

import com.jonathanmorris.Challenge;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jonathanmorris.Challenge;

public class Day01Challenge implements Challenge {

private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Logger logger = LoggerFactory.getLogger(Day01Challenge.class);
private static final Map<String, Integer> WORDS_TO_NUMBER =
Map.of(
"one", 1, "two", 2, "three", 3, "four", 4, "five", 5, "six", 6, "seven", 7, "eight", 8,
"nine", 9);
private static final String WORDS_TO_NUMBER_REGEX = String.join("|", WORDS_TO_NUMBER.keySet());

@Override
public void execute(final List<String> lines) {
List<Integer> numbers = new ArrayList<>();
numbers.toString();
String firstNumber = "";
String lastNumber = "";
for (String line : lines) {
logger.info("Current line is: {}", line);
List<String> chars = Arrays.asList(line.trim().split(""));
for (String currentChar : chars) {
if (isNumber(currentChar)) {
firstNumber = currentChar;
break;
}
}
for (int i = chars.size() - 1; i >= 0; i--) {
String currentChar = chars.get(i);
if (isNumber(currentChar)) {
lastNumber = currentChar;
break;
}
}
numbers.add(Integer.parseInt(String.format("%s%s", firstNumber, lastNumber)));
@Override
public void execute(final List<String> lines) {
List<Integer> numbers = new ArrayList<>();
numbers.toString();
String firstNumber = "";
String lastNumber = "";
logger.info("wordsToNumberRegex is: {}", WORDS_TO_NUMBER_REGEX);
for (String line : lines) {
line = this.getStringWithNumbers(line);
logger.info("Line is: {}", line);
List<String> chars = Arrays.asList(line.trim().split(""));
for (String currentChar : chars) {
if (this.isNumber(currentChar)) {
firstNumber = currentChar;
break;
}
int result = numbers.stream().reduce(0, Integer::sum);
logger.info("Result is: {}", result);
}
for (int i = chars.size() - 1; i >= 0; i--) {
String currentChar = chars.get(i);
if (this.isNumber(currentChar)) {
lastNumber = currentChar;
break;
}
}
numbers.add(Integer.parseInt(String.format("%s%s", firstNumber, lastNumber)));
}
int result = numbers.stream().reduce(0, Integer::sum);
logger.info("Result is: {}", result);
}

private Boolean isNumber(final String currentChar) {
return currentChar.matches("[0-9]");
}

private String getStringWithNumbers(final String line) {
Pattern pattern = Pattern.compile(WORDS_TO_NUMBER_REGEX);
Matcher matcher = pattern.matcher(line);

private Boolean isNumber(final String currentChar) {
return currentChar.matches("[0-9]");
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String match = matcher.group();
matcher.appendReplacement(buffer, String.valueOf(WORDS_TO_NUMBER.get(match)));
}
matcher.appendTail(buffer);
logger.info("Buffer is: {}", buffer.toString());
return buffer.toString();
}
}
20 changes: 7 additions & 13 deletions src/test/java/com/jonathanmorris/aoc/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

import org.junit.Test;

/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
/** Unit test for simple App. */
public class AppTest {
/** Rigorous Test :-) */
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}

0 comments on commit 1cbda2b

Please sign in to comment.