-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
executable file
·134 lines (109 loc) · 3.73 KB
/
main.ts
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
#!/usr/bin/env deno run --allow-read
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { matchGroups, sum } from "../../2020/utils.ts";
const matchInput = matchGroups(
/Player \d+ starting position: (?<pos>\d+)/,
);
function parseInput(string: string): number[] {
const lines = string.trim().split("\n");
return lines.map((line) => parseInt(matchInput(line).pos, 10));
}
const text = await Deno.readTextFile("input.txt");
export const input = parseInput(text);
function wrapAtTen(n: number) {
return (n - 1) % 10 + 1;
}
function part1(input: number[]): number {
const positions = [...input];
const scores = input.map(() => 0);
let dice = 1;
const roll = () => (dice++ - 1) % 100 + 1;
while (true) {
for (let i = 0; i < positions.length; i++) {
const forward = roll() + roll() + roll();
positions[i] = wrapAtTen(positions[i] + forward);
scores[i] += positions[i];
if (scores[i] >= 1000) {
const rolls = dice - 1;
const looserScore = scores[(i + 1) % scores.length];
return rolls * looserScore;
}
}
}
}
const example = parseInput(`
Player 1 starting position: 4
Player 2 starting position: 8
`);
assertEquals(part1(example), 739785);
console.log("Result part 1: " + part1(input));
const WIN_SCORE_PART_2 = 21;
type State = {
scores: number[];
positions: number[];
amount: number;
};
const threeRollsCounts: Record<number, number> = {};
for (let roll1 = 1; roll1 <= 3; roll1++) {
for (let roll2 = 1; roll2 <= 3; roll2++) {
for (let roll3 = 1; roll3 <= 3; roll3++) {
const sum = roll1 + roll2 + roll3;
threeRollsCounts[sum] ??= 0;
threeRollsCounts[sum]++;
}
}
}
const threeRollsCountsArray = Object.entries(threeRollsCounts);
function numbersOfWins(positions: number[]): number[] {
const statesAfterRolls: State[][] = [
[{ scores: [0, 0], positions: positions, amount: 1 }],
];
for (let roll = 1; roll <= WIN_SCORE_PART_2; roll++) {
const currentPlayer = (roll - 1) % 2;
const otherPlayer = roll % 2;
const prevStates = statesAfterRolls[roll - 1];
const nextStates: State[] = [];
for (const [nextRollString, nextRollAmount] of threeRollsCountsArray) {
const nextRoll = parseInt(nextRollString, 10);
for (const prevState of prevStates) {
const {
scores: oldScores,
positions: oldPositions,
amount: oldAmount,
} = prevState;
if (oldScores[otherPlayer] >= WIN_SCORE_PART_2) continue;
const newPositions = [...oldPositions];
const newScores = [...oldScores];
newPositions[currentPlayer] = wrapAtTen(
oldPositions[currentPlayer] + nextRoll,
);
newScores[currentPlayer] = oldScores[currentPlayer] +
newPositions[currentPlayer];
let state = nextStates.find(({ scores, positions }) =>
scores[0] === newScores[0] && positions[0] === newPositions[0] &&
scores[1] === newScores[1] && positions[1] === newPositions[1]
);
if (state === undefined) {
state = { scores: newScores, positions: newPositions, amount: 0 };
nextStates.push(state);
}
state.amount += oldAmount * nextRollAmount;
}
}
statesAfterRolls[roll] = nextStates;
}
return positions.map((_, i) => {
const winAmounts = statesAfterRolls.flatMap((state) =>
state
.filter(({ scores }) => scores[i] >= WIN_SCORE_PART_2)
.map(({ amount }) => amount)
);
return sum(winAmounts);
});
}
function part2(input: number[]): number {
const positions = [...input];
return Math.max(...numbersOfWins(positions));
}
assertEquals(part2(example), 444356092776315);
console.log("Result part 2: " + part2(input));