Skip to content

Commit bae4fd8

Browse files
authored
Merge pull request #6 from madimadica/master
Cleanup AdventDay processing and README
2 parents 8dd8315 + b6e52f1 commit bae4fd8

File tree

2 files changed

+106
-23
lines changed

2 files changed

+106
-23
lines changed

README.md

+104-22
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,147 @@ This is a community driven Java template and utility API for the annual [Advent
55

66
1. Clone this repository from GitHub as an IntelliJ repository (untested on other IDE's)
77
2. Add the `AOC_SESSION_COOKIE` environment variable to your gradle run config and set it equal to that of your session cookie after logging in to your AoC account.
8+
* Alternatively you can set a system environment variable and restart the IDE/terminal.
89
3. Execute the gradle application -> run task.
910

10-
# Creating and running a solution #
11+
# Creating and Running Solutions #
1112

13+
## Solution Packages
1214
Create a `package-info.java` in the package which contains your solutions for a given year.
13-
Make sure to annotate it with the `@AdventYear` annotation.
14-
Example:
15+
Make sure to annotate it with the `@AdventYear` annotation, so we can pull the correct inputs.
1516

17+
**Example**:
1618
```java
1719
@AdventYear(year=2024)
1820
package org.togetherjava.aoc.solutions;
21+
1922
import org.togetherjava.aoc.core.annotations.AdventYear;
2023
```
2124

22-
Create an implementation of the `PuzzleSolution` interface and annotate it with the `@AdventDay` annotation
25+
The package my contain any files or subpackages you wish, but as described next, the solutions need a specific structure.
2326

24-
```java
25-
import org.togetherjava.aoc.core.annotations.AdventDay;
26-
import org.togetherjava.aoc.core.puzzle.PuzzleInput;
27-
import org.togetherjava.aoc.core.puzzle.PuzzleSolution;
27+
---
28+
## Solution Classes
29+
Solution implementations must be under an `@AdventYear` package to be runnable.
30+
Additionally, they must implement the `PuzzleSolution` interface, which provides methods
31+
that give an input type directly.
32+
33+
A useful shortcut for this is `CTRL + I` in IntelliJ, which implements missing methods.
34+
35+
### Setting The Day
36+
37+
While `@AdventYear` explicitly defines the year, a solution can implicitly or explicitly
38+
define which day of the year.
39+
40+
### Explicit Day Value
41+
To explicitly set which day your solution corresponds to, use the `@AdventDay` annotation to override and ignore auto-detection.
2842

43+
**Example**:
44+
```java
2945
@AdventDay(day = 1)
46+
public class DayOne implements PuzzleSolution
47+
```
48+
49+
### Implicit Day Auto-Detection
50+
If no `@AdventDay` annotation is provided, a default auto-detection is used instead.
51+
This will look for the *first* number in the class name in the range `[1, 31]`, ignoring leading zeros.
52+
Therefore, the following examples would be parsed as:
53+
54+
| Class Name | Implicit Day |
55+
|----------------|--------------|
56+
| `Day1` | 1 |
57+
| `Day02` | 2 |
58+
| `Day003` | 3 |
59+
| `AocDay4` | 4 |
60+
| `AocDay05` | 5 |
61+
| `Aoc2024Day6` | 6 |
62+
| `Aoc2024Day07` | 7 |
63+
| `Day8Attempt2` | 8 |
64+
65+
---
66+
If neither implicit nor explicit days can be resolved, an error is thrown at runtime.
67+
68+
### Puzzle Solution
69+
The methods defined by `PuzzleSolution` return `Object`, which is because not all
70+
AOC answers are `int` or `long`. You may still return an `int` or `long` type, and it will be autoboxed into an object.
71+
72+
Here is an example implementation
73+
74+
```java
3075
public class Day01 implements PuzzleSolution {
3176

3277
@Override
3378
public Object part1(PuzzleInput input) {
34-
return 0L;
79+
return 123;
3580
}
3681

3782
@Override
3883
public Object part2(PuzzleInput input) {
39-
return 0L;
84+
return 456L;
4085
}
4186
}
4287
```
4388

44-
Then you can run your solutions with the static `AocRunner` class.
89+
## Running Solutions
90+
The `AocRunner` class provides static access to run your solution implementations.
91+
92+
There are 3 method names: `run`, `runPart1`, and `runPart2`, which each have the following overrides:
93+
94+
| Parameters | Description |
95+
|------------------------------------------|------------------------------|
96+
| `()` | Run today |
97+
| `(int year, int day)` | Run the given day |
98+
| `(PuzzleDate date)` | Run the given date |
99+
| `(Class<? extends PuzzleSolution> impl)` | Run the given implementation |
100+
101+
102+
Below are examples of different runner invocations:
45103

46104
```java
47-
AocRunner.run(); //this will detect and invoke the current day's problems
48-
AocRunner.run(2024, 1); //this will detect and invoke a day for a given year/day
49-
AocRunner.run(Day01.class); //Invoke a specific class
105+
AocRunner.run(); // Detect and run the current day's solution
106+
AocRunner.run(2024, 1); // Detect and run the AOC 2024 Day 1 solution
107+
AocRunner.run(Day01.class); // Run the solution implemented in Day01
108+
AocRunner.run(Day01BruteForce.class); // Run the solution implemented in Day01BruteForce
50109
```
51110

52-
# Getting your session cookie #
111+
### Multiple Solutions
112+
If more than one class implements a solution for a given date, all of those
113+
implementations are registered internally. When trying to run them without
114+
a specific class reference (`e.g. run(Day1BruteForce.class)`) then the specific
115+
implementation chosen is not well-defined, and is whichever is reflectively found first.
116+
117+
# Input Caching
118+
Input files are fetched from the AOC web API to get your input data. To support AOC,
119+
we automatically cache the input responses on your local computer, preventing redundant API
120+
calls every time you run an implementation.
121+
122+
## Local File Cache
123+
Cached input files are stored locally in a path relative to your OS platform. This is done
124+
with the `user.home` system property. Relative to that, `/.together-java/aoc/inputs/YYYY-DD-puzzle-input.txt`
125+
is where the file is stored.
126+
127+
* Windows: `%userprofile%/.together-java/aoc/inputs/`
128+
129+
# Session Cookie (`AOC_SESSION_COOKIE`)
130+
## Getting your session cookie
53131

54132
In Chrome, or other Chromium browsers such as Opera, OperaGX etc
55-
1. Hit ctrl + shift + J to open up developer tools
133+
1. Hit `ctrl + shift + J` to open up developer tools
56134
2. Navigate to the application tab
57135
3. Click cookies
58136
4. Copy the session cookie
59137

60138
![](/setup/4.png)
61139

62-
# Modifiying Gradle run configuration #
63-
1. Go to your Gradle tasks and go to application -> run and right click to modify the run configurations
64-
![](/setup/1.png)
65-
2. Click this box to add an environment variable
140+
## Modifiying Gradle run configuration / env
141+
1. Go to your Gradle tool window
142+
* `Tasks`
143+
* `application`
144+
* Right-click `run``Modify Run Configuration...`
145+
146+
![](/setup/1.png)
147+
2. Click this box to add an environment variable
66148
![](/setup/2.png)
67-
3. Name the environment variable `AOC_SESSION_COOKIE` and then paste the value of your session cookie from your browser in to the value box.
149+
3. Name the environment variable `AOC_SESSION_COOKIE` and then paste the value of your session cookie from your browser in to the value box.
68150
![](/setup/3.png)
69-
4. Hit okay and then execute the Gradle run task
151+
4. Hit okay and then execute the Gradle `run` task

src/main/java/org/togetherjava/aoc/internal/SolutionRegistry.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static void init() {
9898
if (solutionDay.isEmpty()) {
9999
throw new RuntimeException("""
100100
Unable to detect a valid day value from solution type '%s'. \
101-
Follow standard naming, or use @DayOverride() instead.\
101+
Follow standard naming, or use @AdventDay() instead.\
102102
""".formatted(solution.getCanonicalName())
103103
);
104104
}
@@ -113,6 +113,7 @@ private static Optional<Integer> extractDay(String className) {
113113
.results()
114114
.map(MatchResult::group)
115115
.map(StringUtils::trimLeadingZeros)
116+
.filter(x -> 1 <= x && x <= 31)
116117
.findFirst();
117118
}
118119

0 commit comments

Comments
 (0)