-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay21.cs
170 lines (147 loc) · 5.42 KB
/
Day21.cs
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
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode._2021
{
public class Day21
{
const int StartA = 8;
const int StartB = 5;
private class Dice
{
private IEnumerator<int> _enumerator;
public Dice()
{
_enumerator = GetEnumerator();
}
public int Roll()
{
_enumerator.MoveNext();
return _enumerator.Current;
}
private IEnumerator<int> GetEnumerator()
{
while (true)
for (int i = 1; i <= 100; i++)
yield return i;
}
}
private class Player
{
private int _position;
public int Position => _position + 1;
public int Score { get; private set; }
public Player(int initialPosition)
{
_position = initialPosition == 0 ? 9 : initialPosition - 1;
}
public void DoTurn(Dice dice)
{
_position += dice.Roll() + dice.Roll() + dice.Roll();
_position %= 10;
Score += Position;
}
}
Dice _dice;
Player _a;
Player _b;
public Day21()
{
_dice = new Dice();
_a = new Player(StartA);
_b = new Player(StartB);
}
[Fact]
public void Part1()
{
int answer = Play();
Assert.Equal(597600, answer);
}
[Fact]
public void Part2()
{
var positions = new long[21, 21, 10, 10, 2];
positions[0, 0, StartA - 1, StartB - 1, 0] = 1;
long aWins = 0;
long bWins = 0;
for (int scoreA = 0; scoreA < 21; scoreA++)
{
for (int scoreB = 0; scoreB < 21; scoreB++)
{
for (int posA = 0; posA < 10; posA++)
{
for (int posB = 0; posB < 10; posB++)
{
for (int turn = 0; turn < 2; turn++)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
for (int k = 1; k <= 3; k++)
{
int dice = i + j + k;
int pos;
int score;
if (turn == 0)
{
pos = posA;
score = scoreA;
}
else
{
pos = posB;
score = scoreB;
}
int newPos = pos + dice;
newPos %= 10;
int newScore = score + newPos + 1;
if (newScore >= 21)
{
if (turn is 0)
aWins += positions[scoreA, scoreB, posA, posB, 0];
else
bWins += positions[scoreA, scoreB, posA, posB, 1];
continue;
}
if (turn is 0)
{
positions[newScore, scoreB, newPos, posB, 1] +=
positions[scoreA, scoreB, posA, posB, 0];
}
else
{
positions[scoreA, newScore, posA, newPos, 0] +=
positions[scoreA, scoreB, posA, posB, 1];
}
}
}
}
}
}
}
}
}
long answer = aWins > bWins ? aWins : bWins;
Assert.Equal(634769613696613, answer);
}
private int Play()
{
Player current = _a;
Player next = _b;
int turns = 0;
while (true)
{
current.DoTurn(_dice);
turns++;
if (current.Score >= 1000)
{
return turns * 3 * next.Score;
}
(current, next) = (next, current);
}
}
}
}