Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 4c48646

Browse files
committed
Added word-building question
1 parent a08ad8d commit 4c48646

File tree

5 files changed

+460
-0
lines changed

5 files changed

+460
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

competition/word-building/Cargo.lock

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "word-building"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
rand = "0.9.1"
8+
rand_chacha = "0.9.0"

competition/word-building/prob.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---toml
2+
[fuzz]
3+
exec = ["cargo", "run", "--release", "--", "generate"]
4+
env = {}
5+
6+
[judge]
7+
exec = ["cargo", "run", "--release", "--quiet", "--", "validate"]
8+
9+
[problem]
10+
points = 15
11+
difficulty = 2
12+
---
13+
14+
# 🔠 Word-Building Competition
15+
16+
Alice and Bob are playing a game where they take turns to place down letters to form a word.
17+
Completing a word ends the game and awards both players a certain number of points.
18+
This is determined by a *dictionary* they are given at the start of every game,
19+
containing the list of valid words and the points awarded by forming each word.
20+
21+
The only restriction on the letter they can play is that the word they form by playing it
22+
must either reach a valid word in the dictionary, or be the prefix of one such word.
23+
For example, if only the words `car` and `cat` are in the dictionary,
24+
and the letters placed down currently are `ca`, then Alice can only play either `r` or `t`.
25+
26+
Since Alice and Bob are very competitive, however, they have opposing goals in this game.
27+
Alice wishes to maximise the number of points at the end of the game,
28+
while Bob wishes to minimise the number of points.
29+
Out of courtesy, Bob says that *Alice will play first*.
30+
31+
While they are both human, Alice and Bob are both experts in this game.
32+
They are both *guaranteed to play optimally*.
33+
34+
Your task is to answer the following question:
35+
when both Alice and Bob **always play optimally**,
36+
what is the **number of points they will receive at the end of the game**?
37+
38+
## Example
39+
40+
Consider a game with the following dictionary.
41+
```
42+
aaa 28
43+
aab -45
44+
ab 17
45+
baa -10
46+
bab 19
47+
bba 18
48+
bbb -7
49+
```
50+
If Alice plays `a`:
51+
* If Bob plays `a`, then Alice will choose to play `a`,
52+
forming the word `aaa` and scoring **28** (since `aab` scores less than this).
53+
* If Bob plays `b`, the word `ab` is formed, scoring **17**.
54+
Therefore, if Alice plays `a`, then Bob will play `b` (to minimise the score),
55+
meaning the game will end with score of **17**.
56+
57+
If Alice plays `b`:
58+
* If Bob plays `a`, then Alice will choose to play `b`,
59+
forming the word `bab` and scoring **19**.
60+
* If Bob plays `b`, then Alice will choose to play `a`,
61+
forming the word `bba` and scoring **18**.
62+
Knowing this, Bob will choose to play `b` in this situation,
63+
so that Alice will score 18 instead of 19 (thus minimising her score in optimal play).
64+
65+
After having considered all of the above,
66+
Alice now realises that if she plays `a`, the game will end with 17 points,
67+
and if she plays `b`, the game will end with 18 points.
68+
69+
Therefore, she will play `b`, and the game will end with the word `bba` and **18** points.
70+
The answer is here thus:
71+
```
72+
18
73+
```
74+
75+
## Input
76+
77+
Your puzzle input will be the dictionary that Alice and Bob receive at the start of the game,
78+
each line containing a valid word that can be formed to signal the end of a game
79+
and its associated points.
80+
81+
Another example of the input is as follows.
82+
```
83+
aa -1
84+
ab -2
85+
ba 8
86+
bba 7
87+
bbb 6
88+
bc 10
89+
c 4
90+
```
91+
92+
### Constraints
93+
94+
* The words in the dictionary are given in alphabetical (lexicographical) order.
95+
* Every line consists of a word followed by an integer, separated by whitespace.
96+
* Every score is an integer from `-100,000` to `100,000` inclusive.
97+
Numbers are given without commas.
98+
* At every turn, the set of playable letters always spans alphabetically from `a`.
99+
For example, if the word `aaf` exists in the dictionary,
100+
it is guaranteed that `aaa`, `aab`, `aac`, `aad`, and `aae` are either valid words,
101+
or are prefixes of one or more valid words.
102+
103+
Please note that the your input file will be very large (approximately 10,000+ lines).
104+
105+
## Output
106+
107+
A single integer, which is the final score of the game when both Alice and Bob play optimally.
108+
109+
For this example, the output should be
110+
```
111+
7
112+
```

0 commit comments

Comments
 (0)