Skip to content

Commit 268baca

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents b10b68b + 05a0674 commit 268baca

File tree

4 files changed

+208
-222
lines changed

4 files changed

+208
-222
lines changed

src/main/java/AoC2017_02.java

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import static com.github.pareronia.aoc.IterTools.combinations;
2-
import static java.util.stream.Collectors.toList;
2+
3+
import com.github.pareronia.aoc.AssertUtils;
4+
import com.github.pareronia.aoc.solution.Sample;
5+
import com.github.pareronia.aoc.solution.Samples;
6+
import com.github.pareronia.aoc.solution.SolutionBase;
37

48
import java.util.Arrays;
59
import java.util.IntSummaryStatistics;
610
import java.util.List;
711
import java.util.function.ToIntFunction;
812

9-
import com.github.pareronia.aocd.Aocd;
10-
import com.github.pareronia.aocd.Puzzle;
11-
12-
public final class AoC2017_02 extends AoCBase {
13+
public final class AoC2017_02 extends SolutionBase<List<List<Integer>>, Integer, Integer> {
1314

14-
private final transient List<List<Integer>> input;
15-
16-
private AoC2017_02(final List<String> inputs, final boolean debug) {
15+
private AoC2017_02(final boolean debug) {
1716
super(debug);
18-
this.input = inputs.stream()
19-
.map(s -> Arrays.stream(s.split("\\s+"))
20-
.map(Integer::valueOf).collect(toList()))
21-
.collect(toList());
22-
log(this.input);
2317
}
2418

25-
public static AoC2017_02 create(final List<String> input) {
26-
return new AoC2017_02(input, false);
19+
public static AoC2017_02 create() {
20+
return new AoC2017_02(false);
21+
}
22+
23+
public static AoC2017_02 createDebug() {
24+
return new AoC2017_02(true);
2725
}
2826

29-
public static AoC2017_02 createDebug(final List<String> input) {
30-
return new AoC2017_02(input, true);
27+
@Override
28+
protected List<List<Integer>> parseInput(final List<String> inputs) {
29+
return inputs.stream()
30+
.map(s -> Arrays.stream(s.split("\\s+")).map(Integer::valueOf).toList())
31+
.toList();
3132
}
32-
33+
3334
private int differenceHighestLowest(final List<Integer> numbers) {
34-
final IntSummaryStatistics stats = numbers.stream()
35-
.mapToInt(Integer::intValue)
36-
.summaryStatistics();
35+
final IntSummaryStatistics stats =
36+
numbers.stream().mapToInt(Integer::intValue).summaryStatistics();
3737
return stats.getMax() - stats.getMin();
3838
}
39-
39+
4040
private int evenlyDivisibleQuotient(final List<Integer> numbers) {
4141
for (final int[] c : combinations(numbers.size(), 2).iterable()) {
4242
final int n1 = numbers.get(c[0]);
@@ -49,45 +49,41 @@ private int evenlyDivisibleQuotient(final List<Integer> numbers) {
4949
return n2 / n1;
5050
}
5151
}
52-
throw new IllegalStateException("Unsolvable");
52+
throw AssertUtils.unreachable();
5353
}
54-
55-
private int sum(final ToIntFunction<List<Integer>> mapper) {
56-
return this.input.stream().mapToInt(mapper).sum();
54+
55+
private int sum(final List<List<Integer>> input, final ToIntFunction<List<Integer>> mapper) {
56+
return input.stream().mapToInt(mapper).sum();
5757
}
5858

5959
@Override
60-
public Integer solvePart1() {
61-
return sum(this::differenceHighestLowest);
60+
public Integer solvePart1(final List<List<Integer>> input) {
61+
return sum(input, this::differenceHighestLowest);
6262
}
63-
63+
6464
@Override
65-
public Integer solvePart2() {
66-
return sum(this::evenlyDivisibleQuotient);
65+
public Integer solvePart2(final List<List<Integer>> input) {
66+
return sum(input, this::evenlyDivisibleQuotient);
6767
}
6868

69+
@Samples({
70+
@Sample(method = "part1", input = TEST1, expected = "18"),
71+
@Sample(method = "part2", input = TEST2, expected = "9"),
72+
})
6973
public static void main(final String[] args) throws Exception {
70-
assert AoC2017_02.createDebug(TEST1).solvePart1() == 18;
71-
assert AoC2017_02.createDebug(TEST2).solvePart2() == 9;
72-
73-
final Puzzle puzzle = Aocd.puzzle(2017, 2);
74-
final List<String> inputData = puzzle.getInputData();
75-
puzzle.check(
76-
() -> lap("Part 1", AoC2017_02.create(inputData)::solvePart1),
77-
() -> lap("Part 2", AoC2017_02.create(inputData)::solvePart2)
78-
);
74+
create().run();
7975
}
80-
81-
private static final List<String> TEST1 = splitLines(
76+
77+
private static final String TEST1 =
8278
"""
83-
5 1 9 5
84-
7 5 3
85-
2 4 6 8"""
86-
);
87-
private static final List<String> TEST2 = splitLines(
79+
5 1 9 5
80+
7 5 3
81+
2 4 6 8
82+
""";
83+
private static final String TEST2 =
8884
"""
89-
5 9 2 8
90-
9 4 7 3
91-
3 8 6 5"""
92-
);
85+
5 9 2 8
86+
9 4 7 3
87+
3 8 6 5
88+
""";
9389
}

src/main/java/AoC2017_03.java

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1+
import com.github.pareronia.aoc.AssertUtils;
2+
import com.github.pareronia.aoc.geometry.Direction;
3+
import com.github.pareronia.aoc.geometry.Position;
4+
import com.github.pareronia.aoc.solution.Sample;
5+
import com.github.pareronia.aoc.solution.Samples;
6+
import com.github.pareronia.aoc.solution.SolutionBase;
7+
18
import java.util.HashMap;
29
import java.util.Iterator;
310
import java.util.List;
411
import java.util.Map;
512
import java.util.function.Function;
613
import java.util.function.Supplier;
714

8-
import com.github.pareronia.aoc.geometry.Direction;
9-
import com.github.pareronia.aoc.geometry.Position;
10-
import com.github.pareronia.aocd.Aocd;
11-
import com.github.pareronia.aocd.Puzzle;
12-
13-
public final class AoC2017_03 extends AoCBase {
14-
15-
private final transient Integer input;
15+
public final class AoC2017_03 extends SolutionBase<Integer, Integer, Integer> {
1616

17-
private AoC2017_03(final List<String> inputs, final boolean debug) {
17+
private AoC2017_03(final boolean debug) {
1818
super(debug);
19-
assert inputs.size() == 1;
20-
this.input = Integer.valueOf(inputs.get(0));
2119
}
2220

23-
public static AoC2017_03 create(final List<String> input) {
24-
return new AoC2017_03(input, false);
21+
public static AoC2017_03 create() {
22+
return new AoC2017_03(false);
2523
}
2624

27-
public static AoC2017_03 createDebug(final List<String> input) {
28-
return new AoC2017_03(input, true);
25+
public static AoC2017_03 createDebug() {
26+
return new AoC2017_03(true);
2927
}
30-
28+
29+
@Override
30+
protected Integer parseInput(final List<String> inputs) {
31+
return Integer.valueOf(inputs.getFirst());
32+
}
33+
3134
record DirectionAndPeriod(Direction direction, int period) {
3235
public static DirectionAndPeriod of(final Direction direction, final int period) {
3336
return new DirectionAndPeriod(direction, period);
3437
}
3538
}
3639

3740
private static class CoordinateSupplier implements Supplier<Position> {
38-
private final Function<Integer, DirectionAndPeriod>
39-
directionsAndPeriods = new Function<>() {
40-
private final List<Direction> directions = List.of(
41-
Direction.RIGHT, Direction.UP,
42-
Direction.LEFT, Direction.DOWN);
43-
private final int[] periods = { 1, 1, 2, 2 };
41+
private final Function<Integer, DirectionAndPeriod> directionsAndPeriods =
42+
new Function<>() {
43+
private final List<Direction> directions =
44+
List.of(
45+
Direction.RIGHT, Direction.UP,
46+
Direction.LEFT, Direction.DOWN);
47+
private final int[] periods = {1, 1, 2, 2};
4448

4549
@Override
4650
public DirectionAndPeriod apply(final Integer t) {
@@ -50,12 +54,11 @@ public DirectionAndPeriod apply(final Integer t) {
5054
final Direction direction = directions.get(idx);
5155
return DirectionAndPeriod.of(direction, period);
5256
}
53-
};
57+
};
5458
private int x;
5559
private int y;
5660
private int k;
57-
private DirectionAndPeriod directionAndPeriod
58-
= directionsAndPeriods.apply(k);
61+
private DirectionAndPeriod directionAndPeriod = directionsAndPeriods.apply(k);
5962
private int j;
6063

6164
@Override
@@ -68,35 +71,35 @@ public Position get() {
6871
x += directionAndPeriod.direction.getX();
6972
y += directionAndPeriod.direction.getY();
7073
j++;
71-
return Position.of(x, y);
74+
return Position.of(x, y);
7275
}
7376
}
7477

7578
@Override
76-
public Integer solvePart1() {
77-
if (this.input == 1) {
79+
public Integer solvePart1(final Integer input) {
80+
if (input == 1) {
7881
return 0;
7982
}
80-
83+
8184
final CoordinateSupplier supplier = new CoordinateSupplier();
8285
int i = 1;
83-
while (i < this.input) {
86+
while (i < input) {
8487
i++;
8588
final Position position = supplier.get();
86-
if (i == this.input) {
89+
if (i == input) {
8790
return position.manhattanDistance();
8891
}
8992
}
90-
91-
throw new IllegalStateException("Unsolvable");
93+
94+
throw AssertUtils.unreachable();
9295
}
9396

9497
@Override
95-
public Integer solvePart2() {
96-
if (this.input == 1) {
98+
public Integer solvePart2(final Integer input) {
99+
if (input == 1) {
97100
return 1;
98101
}
99-
102+
100103
final Map<Position, Integer> squares = new HashMap<>();
101104
squares.put(Position.of(0, 0), 1);
102105
final CoordinateSupplier supplier = new CoordinateSupplier();
@@ -108,28 +111,24 @@ public Integer solvePart2() {
108111
value += squares.getOrDefault(it.next(), 0);
109112
}
110113
squares.put(position, value);
111-
if (value > this.input) {
114+
if (value > input) {
112115
return value;
113116
}
114117
}
115118
}
116119

120+
@Samples({
121+
@Sample(method = "part1", input = "1", expected = "0"),
122+
@Sample(method = "part1", input = "12", expected = "3"),
123+
@Sample(method = "part1", input = "23", expected = "2"),
124+
@Sample(method = "part1", input = "1024", expected = "31"),
125+
@Sample(method = "part2", input = "1", expected = "1"),
126+
@Sample(method = "part2", input = "2", expected = "4"),
127+
@Sample(method = "part2", input = "3", expected = "4"),
128+
@Sample(method = "part2", input = "4", expected = "5"),
129+
@Sample(method = "part2", input = "5", expected = "10"),
130+
})
117131
public static void main(final String[] args) throws Exception {
118-
assert AoC2017_03.createDebug(splitLines("1")).solvePart1() == 0;
119-
assert AoC2017_03.createDebug(splitLines("12")).solvePart1() == 3;
120-
assert AoC2017_03.createDebug(splitLines("23")).solvePart1() == 2;
121-
assert AoC2017_03.createDebug(splitLines("1024")).solvePart1() == 31;
122-
assert AoC2017_03.createDebug(splitLines("1")).solvePart2() == 1;
123-
assert AoC2017_03.createDebug(splitLines("2")).solvePart2() == 4;
124-
assert AoC2017_03.createDebug(splitLines("3")).solvePart2() == 4;
125-
assert AoC2017_03.createDebug(splitLines("4")).solvePart2() == 5;
126-
assert AoC2017_03.createDebug(splitLines("5")).solvePart2() == 10;
127-
128-
final Puzzle puzzle = Aocd.puzzle(2017, 3);
129-
final List<String> inputData = puzzle.getInputData();
130-
puzzle.check(
131-
() -> lap("Part 1", AoC2017_03.create(inputData)::solvePart1),
132-
() -> lap("Part 2", AoC2017_03.create(inputData)::solvePart2)
133-
);
132+
create().run();
134133
}
135134
}

0 commit comments

Comments
 (0)