-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2021_21.java
168 lines (147 loc) Β· 5.24 KB
/
AoC2021_21.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import com.github.pareronia.aocd.Aocd;
import com.github.pareronia.aocd.Puzzle;
// TODO: add iterative verson
public class AoC2021_21 extends AoCBase {
private final int p1;
private final int p2;
private AoC2021_21(final List<String> input, final boolean debug) {
super(debug);
assert input.size() == 2;
this.p1 = Character.digit(input.get(0).charAt(input.get(0).length() - 1), 10);
this.p2 = Character.digit(input.get(1).charAt(input.get(1).length() - 1), 10);
}
public static final AoC2021_21 create(final List<String> input) {
return new AoC2021_21(input, false);
}
public static final AoC2021_21 createDebug(final List<String> input) {
return new AoC2021_21(input, true);
}
private static final class Game {
private int pos1;
private int pos2;
private int score1;
private int score2;
public Game(final int pos1, final int pos2, final int score1, final int score2) {
this.pos1 = pos1;
this.pos2 = pos2;
this.score1 = score1;
this.score2 = score2;
}
public void turn1(final int[] rolls) {
for (final int roll : rolls) {
pos1 = (pos1 + roll) % 10;
}
score1 += pos1 + 1;
}
public void turn2(final int[] rolls) {
for (final int roll : rolls) {
pos2 = (pos2 + roll) % 10;
}
score2 += pos2 + 1;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Game [pos1=").append(pos1).append(", pos2=")
.append(pos2).append(", score1=").append(score1)
.append(", score2=").append(score2).append("]");
return builder.toString();
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Game other = (Game) obj;
return pos1 == other.pos1 && pos2 == other.pos2 && score1 == other.score1 && score2 == other.score2;
}
@Override
public int hashCode() {
return Objects.hash(pos1, pos2, score1, score2);
}
}
@Override
public Integer solvePart1() {
final Game game = new Game(this.p1 - 1, this.p2 - 1, 0, 0);
int die = 0;
int cnt = 0;
while (true) {
game.turn1(new int[] { 1 + die++, 1 + die++, 1 + die++ });
cnt += 3;
if (game.score1 >= 1000) {
log("p1 wins");
return game.score2 * cnt;
}
game.turn2(new int[] { 1 + die++, 1 + die++, 1 + die++ });
cnt += 3;
if (game.score2 >= 1000) {
log("p2 wins");
return game.score1 * cnt;
}
}
}
private final Map<Game, LongPair> winsCache = new HashMap<>();
private static final Map<Integer, Integer> ROLLS = Map.of(
3, 1,
4, 3,
5, 6,
6, 7,
7, 6,
8, 3,
9, 1
);
private LongPair solve2(final Game game) {
if (winsCache.containsKey(game)) {
return winsCache.get(game);
}
LongPair wins = new LongPair(0L, 0L);
for (final Entry<Integer, Integer> roll : ROLLS.entrySet()) {
final int nPos = (game.pos1 + roll.getKey()) % 10;
final int nScore = game.score1 + nPos + 1;
if (nScore >= 21 ) {
wins = new LongPair(wins.one() + roll.getValue(), wins.two());
} else {
final Game newGame = new Game(game.pos2, nPos, game.score2, nScore);
final LongPair nwins = solve2(newGame);
wins = new LongPair(
wins.one() + roll.getValue() * nwins.two(),
wins.two() + roll.getValue() * nwins.one());
}
}
winsCache.put(game, wins);
return wins;
}
@Override
public Long solvePart2() {
final LongPair ans
= solve2(new Game(this.p1 - 1, this.p2 - 1, 0, 0));
log(ans);
return Math.max(ans.one(), ans.two());
}
public static void main(final String[] args) throws Exception {
assert AoC2021_21.create(TEST).solvePart1() == 739_785;
assert AoC2021_21.create(TEST).solvePart2() == 444_356_092_776_315L;
final Puzzle puzzle = Aocd.puzzle(2021, 21);
final List<String> inputData = puzzle.getInputData();
puzzle.check(
() -> lap("Part 1", AoC2021_21.create(inputData)::solvePart1),
() -> lap("Part 2", AoC2021_21.create(inputData)::solvePart2)
);
}
private static final List<String> TEST = splitLines(
"Player 1 starting position: 4\r\n" +
"Player 2 starting position: 8"
);
record LongPair(Long one, Long two) {}
}