Skip to content

Commit 2349a4f

Browse files
committed
Day 21 refactor
1 parent c69d1b0 commit 2349a4f

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

2021/go/d21/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ func playRound(currentRoll int, state State, cache map[State][2]int64) [2]int64
5757
state = state.roll(currentRoll)
5858

5959
if state.gameOver(21) { // We have winner, return it as array 0,1 or 1,0
60-
res := [2]int64{0, 0}
6160
currentPlayer := (state.totalRolls / 3) % 2
62-
res[currentPlayer] = 1
63-
return res
61+
if currentPlayer == 0 {
62+
return [2]int64{1, 0}
63+
} else {
64+
return [2]int64{0, 1}
65+
}
6466
}
6567
return playRecursiveUniverses(state, cache)
6668
}
@@ -75,12 +77,11 @@ func (state State) rollPracticeDice() State {
7577
}
7678

7779
func (state State) roll(currentRoll int) State {
78-
playerRollNumber := state.totalRolls % 3 // number of rolls for current player
80+
playerRollNumber := state.totalRolls % 3 // number of roll for current player
7981
currentPlayer := (state.totalRolls / 3) % 2
8082
state.totalRolls += 1
8183

82-
if playerRollNumber == 2 {
83-
// third roll for the player, move it
84+
if playerRollNumber == 2 { // third roll for the player, move it
8485
totalRollSum := currentRoll + state.prevRoll + state.prevRoll2 // rolls sum
8586
moved := state.players[currentPlayer].move(totalRollSum)
8687
state.players[currentPlayer] = moved
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package aoc2021;
22

3-
import java.util.Arrays;
43
import java.util.HashMap;
54
import java.util.Map;
65

@@ -11,39 +10,55 @@ public class D21 {
1110

1211
record Player(int pos, int score) {
1312
Player move(int m) {
14-
int newPos = (this.pos + m - 1) % 10 + 1;
13+
int newPos = (this.pos + m - 1) % 10 + 1; // calculate new position
1514
return new Player(newPos, score + newPos);
1615
}
1716
}
1817

19-
record State(int totalRolls, int prev, int prev2, Player p1, Player p2) {}
18+
record State(int totalRolls, int prevRoll, int prevRoll2, Player p1, Player p2) {
19+
boolean gameOver(int score) {
20+
return p1.score >= score || p2.score >= score;
21+
}
22+
int playerInTurn() {
23+
return (totalRolls / 3) % 2;
24+
}
25+
State roll(int currentRoll) {
26+
int playerRollNumber = totalRolls % 3; // number of roll for current player
27+
Player p1 = this.p1;
28+
Player p2 = this.p2;
29+
if (playerRollNumber == 2) { // third roll for the player, move it
30+
int totalRollSum = currentRoll + prevRoll + prevRoll2; // rolls sum
31+
if (playerInTurn() == 0) {
32+
p1 = this.p1.move(totalRollSum);
33+
} else {
34+
p2 = this.p2.move(totalRollSum);
35+
}
36+
}
37+
return new State(totalRolls+1, currentRoll, prevRoll, p1, p2);
38+
}
39+
}
2040

21-
static int rollPracticeDice(int rolls) {
22-
return (rolls + 1) % 100 - 1;
41+
static State rollPracticeDice(State state) {
42+
int currentRoll = (state.totalRolls) %100 + 1;
43+
return state.roll(currentRoll);
2344
}
2445

2546
public static void main(String... args)
2647
{
27-
Player[] players = {new Player(8,0), new Player(6,0)};
28-
int totalRolls = 0;
29-
while(players[0].score < 1000 && players[1].score < 1000) { // until wins
30-
int player = (totalRolls / 3) % 2; // player in turn, index to players table
31-
int roll = 0;
32-
roll += rollPracticeDice(++totalRolls); // roll 1
33-
roll += rollPracticeDice(++totalRolls); // roll 2
34-
roll += rollPracticeDice(++totalRolls); // roll 3
35-
players[player] = players[player].move(roll); // move player and update players table
48+
State state = new State(0, 0, 0, new Player(8,0), new Player(6,0));
49+
while(!state.gameOver(1000)) { // until wins
50+
state = rollPracticeDice(state);
3651
}
37-
int losingScore = Arrays.stream(players).mapToInt(p -> p.score).min().getAsInt();
38-
System.out.printf("part 1: %s\n", losingScore * totalRolls);
52+
int losingScore = Math.min(state.p1.score, state.p2.score);
53+
System.out.printf("part 1: %s\n", losingScore * state.totalRolls);
3954

4055
Map<State, long[]> cache = new HashMap<>();
41-
State s = new State(0, 0, 0,new Player(8,0), new Player(6,0));
42-
var res = play(s, cache);
56+
state = new State(0, 0, 0, new Player(8,0), new Player(6,0));
57+
var res = play(state, cache);
4358
System.out.printf("part 2: %s\n", Math.max(res[0], res[1]));
4459
}
4560

46-
// play state, return table of two, player 1 wins and player 2 wins
61+
// play state, return array of two, player 1 wins and player 2 wins
4762
static long[] play(State state, Map<State, long[]> cache) {
4863
long[] cached = cache.get(state);
4964
if (cached == null) {
@@ -58,29 +73,12 @@ static long[] play(State state, Map<State, long[]> cache) {
5873
return cached;
5974
}
6075

61-
// play state with dice rolling given value, return table of two, player 1 wins and player 2 wins
76+
// play state with dice rolling given value, return array of two, player 1 wins and player 2 wins
6277
static long[] playRound(int currentRoll, State state, Map<State, long[]> cache) {
63-
boolean firstPlayer = (state.totalRolls / 3) % 2 == 0; // get the player based on dice rolls
64-
int playerRollNumber = state.totalRolls % 3; // number of rolls for current player
65-
66-
Player p1 = state.p1;
67-
Player p2 = state.p2;
68-
if (playerRollNumber == 2) {
69-
// third roll for the player
70-
int totalRollSum = currentRoll + state.prev + state.prev2; // rolls sum
71-
Player moved = (firstPlayer ? state.p1 : state.p2).move(totalRollSum);
72-
73-
if (moved.score >= 21) {
74-
return firstPlayer ? new long[]{1, 0} : new long[]{ 0, 1};
75-
}
76-
77-
if (firstPlayer) {
78-
p1 = moved;
79-
} else {
80-
p2 = moved;
81-
}
78+
state = state.roll(currentRoll);
79+
if (state.gameOver(21)) {
80+
return state.playerInTurn() == 0 ? new long[]{1,0} : new long[]{0,1};
8281
}
83-
var newState = new State(state.totalRolls+1, currentRoll, state.prev, p1, p2);
84-
return play(newState, cache);
82+
return play(state, cache);
8583
}
8684
}

0 commit comments

Comments
 (0)