Skip to content
Snippets Groups Projects
Commit a5746272 authored by LUDMANN Pierre's avatar LUDMANN Pierre
Browse files

dirty Day2 but clean Maven setup

parents
No related branches found
No related tags found
No related merge requests found
### Advent of Code ###
src/main/resources/day*.input
### Maven ###
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.idea/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
Advent of Code 2023
===================
This year will be in Java.
Requirements
------------
- Maven
- Java SDK (21 here but it shouldn't matter)
Build
-----
mvn clean install
Usage
-----
mvn exec:java -Day=1
mvn exec:java -Day=10
mvn exec:java
When no day is given, it assumes today.
Contributing
------------
Write classes `Day1`, ..., `Day10`, ... that extend the abstract class `Day`.
Most of your work should go to implement the methods `String part1()` and `String part2()`.
The superclass `Day` provides a constructor to fetch the inputs `day1.input1`, ..., `day10.input`, ...
from `src/(main|test)/resources`.
In development, you might want to chain the Maven goals:
mvn clean install exec:java
pom.xml 0 → 100644
<?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>CieDuVaseDesNoces</groupId>
<artifactId>advent-of-code-2023-java</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>${project.groupId}.Main</exec.mainClass>
<exec.args>${ay}</exec.args>
<ay>${maven.build.timestamp}</ay>
<maven.build.timestamp.format>d</maven.build.timestamp.format>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package CieDuVaseDesNoces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;
import static java.lang.Thread.currentThread;
abstract class Day {
List<String> input;
Day(String inputName) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Objects.requireNonNull(currentThread().getContextClassLoader()
.getResourceAsStream(inputName))))) {
input = reader.lines().toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
abstract String part1();
abstract String part2();
// public static void main(String[] args) {
//final Day today = new Day("1");
//System.out.println(part1());
//System.out.println(part2());
}
package CieDuVaseDesNoces;
import java.util.HashMap;
import static java.lang.Integer.max;
import static java.lang.Integer.parseInt;
public class Day2 extends Day {
static HashMap<String, Integer> limit = new HashMap<>(3);
Day2(String inputName) {
super(inputName);
limit.put("red", 12);
limit.put("green", 13);
limit.put("blue", 14);
}
String part1() {
int ans = 0;
for (String game : input) {
String[] tmp = game.split(": ", 2);
int gameId = parseInt(tmp[0].split(" ", 2)[1]);
boolean possible = true;
for (String set : tmp[1].split("; ")) {
for (String cubes : set.split(", ")) {
String[] colorQty = cubes.split(" ", 2);
if (parseInt(colorQty[0]) > limit.get(colorQty[1])) {
possible = false;
break;
}
}
if (!possible) break;
}
if (possible) ans += gameId;
}
return String.valueOf(ans);
}
String part2() {
int ans = 0;
for (String game : input) {
String[] tmp = game.split(": ", 2);
HashMap<String, Integer> needed = new HashMap<>(3);
needed.put("blue", 0);
needed.put("red", 0);
needed.put("green", 0);
for (String set : tmp[1].split("; ")) {
for (String cubes : set.split(", ")) {
String[] colorQty = cubes.split(" ", 2);
needed.put(colorQty[1], max(parseInt(colorQty[0]),
needed.get(colorQty[1])));
}
}
int power = 1;
for (int col : needed.values()) {
power *= col;
}
if (power == 0) System.err.println(power);
ans += power;
}
return String.valueOf(ans);
}
}
package CieDuVaseDesNoces;
import java.lang.reflect.InvocationTargetException;
import static java.lang.Integer.parseInt;
public class Main {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException,
IllegalAccessException {
int day = parseInt(args[0]);
if (day < 1 || day > 25) {
throw new IllegalArgumentException(
"Day should be between 1 and 25");
}
Day today =
(Day) Class.forName(Main.class.getPackageName() + ".Day" + day)
.getDeclaredConstructor(String.class)
.newInstance("day" + day + ".input");
System.out.println(today.part1());
System.out.println(today.part2());
}
}
package CieDuVaseDesNoces;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Day2Test {
Day2 today = new Day2("day2.input");
@Test
void part1_test() {
assertEquals("8", today.part1());
}
@Test
void part2_test() {
assertEquals("2286", today.part2());
}
}
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment