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+
18import java .util .HashMap ;
29import java .util .Iterator ;
310import java .util .List ;
411import java .util .Map ;
512import java .util .function .Function ;
613import 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