Skip to content

Commit f57fedd

Browse files
committed
Implemented better input manager instead of bash
1 parent 9c986cc commit f57fedd

File tree

10 files changed

+135
-91
lines changed

10 files changed

+135
-91
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ clean:
2020
uberjar: clean
2121
./mvnw install
2222

23-
.PHONY: download
24-
download: uberjar mk-puzzle-dir
25-
java -jar ./target/solver-1.0.0.jar $$(java -jar ./target/solver-1.0.0.jar check -d) > src/main/resources/$$(java -jar ./target/solver-1.0.0.jar check -p)
23+
.PHONY: fix
24+
fix: uberjar mk-puzzle-dir
25+
java -jar ./target/solver-1.0.0.jar check -a -f
2626

27-
.PHONY: download
27+
.PHONY: redownload
2828
redownload: uberjar mk-puzzle-dir
29-
java -jar ./target/solver-1.0.0.jar $$(java -jar ./target/solver-1.0.0.jar check -d -c) > src/main/resources/$$(java -jar ./target/solver-1.0.0.jar check -c -p)
29+
java -jar ./target/solver-1.0.0.jar check -a -c -f
3030

3131
.PHONY: mk-puzzle-dir
3232
mkdir src/main/resources/puzzle

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You have downloaded task input to: src/resources/puzzle/2024_01_1.txt
3232

3333
#### Alternative
3434

35+
Create empty .env file.
3536
Create src/resources/puzzle/2024_01_1.txt
3637
Copy and paste your first puzzle input in that file.
3738

@@ -81,7 +82,9 @@ It will download puzzle input to ./src/resources/puzzle/{YYYY}\_{dd}\_{id}.txt
8182

8283
## Can I download manually?
8384

84-
Sure. Create a file ./src/resources/puzzle/{YYYY}\_{dd}\_{n}.txt and copy/paste your input there.
85+
Sure.
86+
Create empty .env file.
87+
Create a file ./src/resources/puzzle/{YYYY}\_{dd}\_{n}.txt and copy/paste your input there.
8588

8689
## How to make and run jar
8790

src/main/java/dev/aoc/starter/internal/MainConfiguration.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
package dev.aoc.starter.internal;
33

44
import dev.aoc.starter.internal.aocapi.PuzzleLoader;
5-
import dev.aoc.starter.internal.command.DownloadCommand;
65
import dev.aoc.starter.internal.command.CheckCommand;
6+
import dev.aoc.starter.internal.command.DownloadCommand;
77
import dev.aoc.starter.internal.command.RootCommand;
88
import dev.aoc.starter.internal.command.SolveCommand;
99
import dev.aoc.starter.internal.puzzleinputmanager.MissingInputPuzzleProvider;
10+
import dev.aoc.starter.internal.puzzleinputmanager.PuzzleInputManager;
1011
import dev.aoc.starter.internal.solutionrunner.PuzzleInputReader;
1112
import dev.aoc.starter.internal.solutionrunner.SolutionContainer;
1213
import dev.aoc.starter.internal.solutionrunner.Solver;
@@ -50,15 +51,19 @@ public SolveCommand solveCommandCommand(Solver solver) {
5051
}
5152

5253
@Bean
53-
public DownloadCommand downloadCommand(PuzzleLoader puzzleLoader) {
54-
return new DownloadCommand(puzzleLoader);
54+
public DownloadCommand downloadCommand(
55+
PuzzleLoader puzzleLoader,
56+
PuzzleInputManager puzzleInputManager
57+
) {
58+
return new DownloadCommand(puzzleLoader, puzzleInputManager);
5559
}
5660

5761
@Bean
5862
public CheckCommand missingCommand(
59-
MissingInputPuzzleProvider missingInputPuzzleProvider
63+
MissingInputPuzzleProvider missingInputPuzzleProvider,
64+
PuzzleInputManager puzzleInputManager
6065
) {
61-
return new CheckCommand(missingInputPuzzleProvider);
66+
return new CheckCommand(missingInputPuzzleProvider, puzzleInputManager);
6267
}
6368

6469
@Bean
@@ -86,4 +91,9 @@ public MissingInputPuzzleProvider missingInputPuzzleProvider(
8691
public CliRunner cliRunner(IFactory cliFactory, RootCommand command) {
8792
return new CliRunner(cliFactory, command, 0);
8893
}
94+
95+
@Bean
96+
public PuzzleInputManager puzzleInputManager(PuzzleLoader loader) {
97+
return new PuzzleInputManager(loader);
98+
}
8999
}

src/main/java/dev/aoc/starter/internal/aocapi/PuzzleSaver.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/dev/aoc/starter/internal/command/CheckCommand.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.google.common.collect.Lists;
55
import dev.aoc.starter.internal.puzzleinputmanager.MissingInputPuzzleProvider;
6+
import dev.aoc.starter.internal.puzzleinputmanager.PuzzleInputManager;
67
import java.util.Collections;
78
import java.util.concurrent.Callable;
89
import picocli.CommandLine.Command;
@@ -13,44 +14,42 @@
1314
mixinStandardHelpOptions = true,
1415
description = {
1516
"Checks for puzzles with missing input files.",
16-
"Use: 'make download' to automatically download input files.",
17-
"Use: 'make redownload' to automatically download corrupted input files.",
17+
"Use: 'make fix' to automatically download missing input files.",
18+
"Use: 'make redownload' to re-download all input files.",
1819
}
1920
)
2021
public class CheckCommand implements Callable<Integer> {
2122

2223
@Option(
2324
names = { "-a", "--all" },
2425
negatable = true,
25-
description = "Show all puzzles with missing inputs."
26+
description = "Prints all puzzles with missing inputs."
2627
)
2728
boolean all;
2829

2930
@Option(
30-
names = { "-p", "--print-path" },
31+
names = { "-f", "--fix" },
3132
negatable = true,
32-
description = "Print file path where input is expected."
33+
description = "Download missing puzzle inputs. Requires TOKEN."
3334
)
34-
boolean printDownloadPath;
35-
36-
@Option(
37-
names = { "-d", "--print-download-args" },
38-
negatable = true,
39-
description = "Print args to supply to download command."
40-
)
41-
boolean printDownloadCommandArgs;
35+
boolean fix;
4236

4337
@Option(
4438
names = { "-c", "--content-check" },
4539
negatable = true,
46-
description = "If TRUE verifies input content in addition to file existance. Requires TOKEN."
40+
description = "If TRUE verifies input content in addition to file presense. Requires TOKEN."
4741
)
4842
boolean verifyContent;
4943

5044
MissingInputPuzzleProvider missingInputPuzzleProvider;
45+
PuzzleInputManager puzzleInputManager;
5146

52-
public CheckCommand(MissingInputPuzzleProvider missingInputPuzzleProvider) {
47+
public CheckCommand(
48+
MissingInputPuzzleProvider missingInputPuzzleProvider,
49+
PuzzleInputManager puzzleInputManager
50+
) {
5351
this.missingInputPuzzleProvider = missingInputPuzzleProvider;
52+
this.puzzleInputManager = puzzleInputManager;
5453
}
5554

5655
@Override
@@ -63,16 +62,10 @@ public Integer call() {
6362
var filtered = all ? missing : missing.stream().limit(1).toList();
6463

6564
filtered.forEach(p -> {
66-
if (printDownloadPath) {
67-
System.out.println(p.inputPath());
68-
} else if (printDownloadCommandArgs) {
65+
if (fix) {
66+
puzzleInputManager.save(p);
6967
System.out.println(
70-
String.format(
71-
"download -y %s -d %s -p %s",
72-
p.yearString(),
73-
p.dayString(),
74-
p.puzzleNumberString()
75-
)
68+
"Fixed input for " + p.toPuzzle().toString()
7669
);
7770
} else {
7871
System.out.println(p.toPuzzle().toString());

src/main/java/dev/aoc/starter/internal/command/DownloadCommand.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package dev.aoc.starter.internal.command;
33

44
import dev.aoc.starter.internal.aocapi.PuzzleLoader;
5+
import dev.aoc.starter.internal.puzzleinputmanager.PuzzleInputManager;
56
import dev.aoc.starter.internal.solutionrunner.PuzzleDetails;
67
import dev.aoc.starter.solution.Solution.Puzzle;
78
import java.util.Optional;
@@ -13,9 +14,9 @@
1314
name = "download",
1415
mixinStandardHelpOptions = true,
1516
description = {
16-
"Downloads input for given puzzle.",
17-
"Use: 'make download' to automatically download input files.",
18-
"Use: 'make redownload' to automatically download corrupted input files.",
17+
"Downloads input for given puzzle. Requires TOKEN.",
18+
"Use: 'make fix' to automatically download missing input files.",
19+
"Use: 'make redownload' to re-download all input files.",
1920
}
2021
)
2122
public class DownloadCommand implements Callable<Integer> {
@@ -41,22 +42,42 @@ public class DownloadCommand implements Callable<Integer> {
4142
)
4243
int puzzleNumber;
4344

45+
@Option(
46+
names = { "-o", "--stdout" },
47+
required = false,
48+
description = "Print to stdout instead of saving to file."
49+
)
50+
boolean out;
51+
4452
PuzzleLoader puzzleLoader;
4553

46-
public DownloadCommand(PuzzleLoader puzzleLoader) {
54+
PuzzleInputManager puzzleInputManager;
55+
56+
public DownloadCommand(
57+
PuzzleLoader puzzleLoader,
58+
PuzzleInputManager puzzleInputManager
59+
) {
4760
this.puzzleLoader = puzzleLoader;
61+
this.puzzleInputManager = puzzleInputManager;
4862
this.year = 0;
4963
this.day = 0;
5064
this.puzzleNumber = 0;
5165
}
5266

5367
@Override
5468
public Integer call() {
55-
var inputData = puzzleLoader.load(
56-
PuzzleDetails.fromPuzzle(
57-
new Puzzle(year, day, puzzleNumber, Optional.empty())
58-
)
69+
var puzzle = PuzzleDetails.fromPuzzle(
70+
new Puzzle(year, day, puzzleNumber, Optional.empty())
5971
);
72+
if (!out) {
73+
puzzleInputManager.save(puzzle);
74+
System.out.println(
75+
"Saved input for " + puzzle.toPuzzle().toString()
76+
);
77+
return 0;
78+
}
79+
80+
var inputData = puzzleLoader.load(puzzle);
6081

6182
System.out.println(inputData);
6283

src/main/java/dev/aoc/starter/internal/command/RootCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"CLI to quickly start solving AOC tasks without boilerplate setup.",
1313
"To run puzzle solution: 'make solve'",
1414
"To start solving new puzzle: Create Solution implementation class accoring to README.md",
15-
"To download input for new puzzle: 'make download' or 'make redownload'",
15+
"To download input for new puzzles: 'make fix' or 'make redownload'",
1616
"To run tests: 'make test'",
1717
"To add test: see src/test/java/dev/aoc/starter/solution/ExampleTest.java",
1818
}

src/main/java/dev/aoc/starter/internal/command/SolveCommand.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class SolveCommand implements Callable<Integer> {
3030
@Option(
3131
names = { "-y", "--year" },
3232
required = false,
33-
description = "Year puzzle is from."
33+
description = "Puzzle year."
3434
)
3535
int year;
3636

3737
@Option(
3838
names = { "-d", "--day" },
3939
required = false,
40-
description = "Day puzzle is from."
40+
description = "Puzzle day."
4141
)
4242
int day;
4343

@@ -60,10 +60,6 @@ public SolveCommand(Solver solver) {
6060
@Override
6161
public Integer call() {
6262
var puzzle = new Puzzle(year, day, puzzleNumber, Optional.empty());
63-
// if (year > 0 && day > 0 && puzzleNumber > 0) {
64-
// solver.apply(PuzzleDetails.fromPuzzle(puzzle).toPuzzle());
65-
// return 0;
66-
// }
6763

6864
var puzzles = Lists.newArrayList(
6965
solver
@@ -99,8 +95,9 @@ public Integer call() {
9995
"Could not find solution for: " + puzzleFilter.toString()
10096
);
10197

102-
solver.apply(latest.get().toPuzzle());
98+
var output = solver.apply(latest.get().toPuzzle());
10399

100+
System.out.println("Answer: " + output);
104101
return 0;
105102
}
106103
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dev.aoc.starter.internal.puzzleinputmanager;
2+
3+
import com.google.common.base.Preconditions;
4+
import dev.aoc.starter.internal.aocapi.PuzzleLoader;
5+
import dev.aoc.starter.internal.solutionrunner.PuzzleDetails;
6+
import java.io.FileWriter;
7+
import java.nio.charset.Charset;
8+
import java.nio.file.Path;
9+
10+
public record PuzzleInputManager(PuzzleLoader loader) {
11+
public void save(PuzzleDetails puzzle) {
12+
var dir = findResourcesRoot();
13+
var content = loader.load(puzzle);
14+
var path = dir.resolve(puzzle.inputPath()).toFile();
15+
path.getParentFile().mkdirs();
16+
try {
17+
path.createNewFile();
18+
} catch (Exception e) {
19+
throw new RuntimeException(e);
20+
}
21+
try (
22+
var writer = new FileWriter(
23+
dir.resolve(puzzle.inputPath()).toFile(),
24+
Charset.defaultCharset(),
25+
false
26+
)
27+
) {
28+
writer.append(content);
29+
} catch (Exception e) {
30+
throw new RuntimeException(e);
31+
}
32+
}
33+
34+
public Path findResourcesRoot() {
35+
var userDir = Path.of(System.getProperty("user.dir"))
36+
.toAbsolutePath()
37+
.normalize();
38+
39+
var currentDir = userDir;
40+
for (int i = 0; i < 100; ++i) {
41+
var src = currentDir.resolve("src");
42+
var main = src.resolve("main");
43+
var resources = main.resolve("resources");
44+
45+
Preconditions.checkState(currentDir.toFile().isDirectory());
46+
47+
if (
48+
src.toFile().isDirectory() &&
49+
main.toFile().isDirectory() &&
50+
resources.toFile().isDirectory()
51+
) {
52+
return resources;
53+
}
54+
}
55+
56+
throw new RuntimeException(
57+
"Failed to find project root for: " + userDir
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)