Skip to content

Commit 3416399

Browse files
Add hard exercises (#170)
1 parent f613afe commit 3416399

33 files changed

Lines changed: 899 additions & 0 deletions

config.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,38 @@
517517
"practices": [],
518518
"prerequisites": [],
519519
"difficulty": 7
520+
},
521+
{
522+
"slug": "nth-prime",
523+
"name": "Nth Prime",
524+
"uuid": "cbd8dbf2-67da-49fe-a89b-db82e4a9f440",
525+
"practices": [],
526+
"prerequisites": [],
527+
"difficulty": 8
528+
},
529+
{
530+
"slug": "palindrome-products",
531+
"name": "Palindrome Products",
532+
"uuid": "a8c0261a-53e9-48e6-baab-a0951e6ed5e3",
533+
"practices": [],
534+
"prerequisites": [],
535+
"difficulty": 8
536+
},
537+
{
538+
"slug": "pythagorean-triplet",
539+
"name": "Pythagorean Triplet",
540+
"uuid": "45237b5d-1f62-43df-846c-fcc4631d9eef",
541+
"practices": [],
542+
"prerequisites": [],
543+
"difficulty": 8
544+
},
545+
{
546+
"slug": "connect",
547+
"name": "Connect",
548+
"uuid": "aa62b866-3c6b-4804-9a62-dddc45ab5d17",
549+
"practices": [],
550+
"prerequisites": [],
551+
"difficulty": 9
520552
}
521553
]
522554
},
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Compute the result for a game of Hex / Polygon.
4+
5+
The abstract boardgame known as [Hex][hex] / Polygon / CON-TAC-TIX is quite simple in rules, though complex in practice.
6+
Two players place stones on a parallelogram with hexagonal fields.
7+
The player to connect his/her stones to the opposite side first wins.
8+
The four sides of the parallelogram are divided between the two players (i.e. one player gets assigned a side and the side directly opposite it and the other player gets assigned the two other sides).
9+
10+
Your goal is to build a program that given a simple representation of a board computes the winner (or lack thereof).
11+
Note that all games need not be "fair".
12+
(For example, players may have mismatched piece counts or the game's board might have a different width and height.)
13+
14+
The boards look like this:
15+
16+
```text
17+
. O . X .
18+
. X X O .
19+
O O O X .
20+
. X O X O
21+
X O O O X
22+
```
23+
24+
"Player `O`" plays from top to bottom, "Player `X`" plays from left to right.
25+
In the above example `O` has made a connection from left to right but nobody has won since `O` didn't connect top and bottom.
26+
27+
[hex]: https://en.wikipedia.org/wiki/Hex_%28board_game%29
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"connect/connect.factor"
8+
],
9+
"test": [
10+
"connect/connect-tests.factor"
11+
],
12+
"example": [
13+
".meta/example.factor"
14+
]
15+
},
16+
"blurb": "Compute the result for a game of Hex / Polygon."
17+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
USING: arrays assocs hashtables kernel locals math sequences
2+
strings vectors ;
3+
IN: connect
4+
5+
CONSTANT: hex-dirs { { -1 0 } { -1 1 } { 0 -1 } { 0 1 } { 1 -1 } { 1 0 } }
6+
7+
:: cell-at ( board rows cols r c -- ch )
8+
r 0 >= r rows < and c 0 >= c cols < and and
9+
[ r board nth r c 2 * + swap nth ] [ CHAR: \s ] if ;
10+
11+
:: wins? ( board rows cols ch start-rc goal-rc -- ? )
12+
H{ } clone :> visited
13+
V{ } clone :> queue
14+
start-rc [| rc |
15+
board rows cols rc first rc second cell-at ch =
16+
[ rc first cols * rc second + queue push ] when
17+
] each
18+
f :> found!
19+
0 :> qi!
20+
[ qi queue length < found not and ] [
21+
qi queue nth :> key
22+
qi 1 + qi!
23+
key visited key? not [
24+
t key visited set-at
25+
key cols /i :> r
26+
key cols mod :> c
27+
r c goal-rc call( r c -- ? ) [ t found! ] [
28+
hex-dirs [| d |
29+
r d first + :> nr
30+
c d second + :> nc
31+
board rows cols nr nc cell-at ch = [
32+
nr cols * nc + :> nk
33+
nk visited key? not [ nk queue push ] when
34+
] when
35+
] each
36+
] if
37+
] when
38+
] while
39+
found ;
40+
41+
:: winner ( board -- str )
42+
board length :> rows
43+
rows 0 = [ "" ] [
44+
board first length 1 + 2 /i :> cols
45+
board rows cols CHAR: X
46+
rows <iota> [ 0 2array ] map >array
47+
[ nip cols 1 - = ] wins?
48+
[ "X" ] [
49+
board rows cols CHAR: O
50+
cols <iota> [ 0 swap 2array ] map >array
51+
[ drop rows 1 - = ] wins?
52+
[ "O" ] [ "" ] if
53+
] if
54+
] if ;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Connect
2+
3+
function gen_test_case(case)
4+
expected = case["expected"]
5+
board = case["input"]["board"]
6+
if isempty(board)
7+
board_str = "{ }"
8+
else
9+
lines = map(s -> " \"$(escape_factor(s))\"", board)
10+
board_str = "{\n" * join(lines, "\n") * "\n}"
11+
end
12+
return """{ "$(expected)" }\n[ $(board_str) winner ] unit-test"""
13+
end
14+
15+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[6eff0df4-3e92-478d-9b54-d3e8b354db56]
13+
description = "an empty board has no winner"
14+
15+
[298b94c0-b46d-45d8-b34b-0fa2ea71f0a4]
16+
description = "X can win on a 1x1 board"
17+
18+
[763bbae0-cb8f-4f28-bc21-5be16a5722dc]
19+
description = "O can win on a 1x1 board"
20+
21+
[819fde60-9ae2-485e-a024-cbb8ea68751b]
22+
description = "only edges does not make a winner"
23+
24+
[2c56a0d5-9528-41e5-b92b-499dfe08506c]
25+
description = "illegal diagonal does not make a winner"
26+
27+
[41cce3ef-43ca-4963-970a-c05d39aa1cc1]
28+
description = "nobody wins crossing adjacent angles"
29+
30+
[cd61c143-92f6-4a8d-84d9-cb2b359e226b]
31+
description = "X wins crossing from left to right"
32+
33+
[495e33ed-30a9-4012-b46e-d7c4d5fe13c3]
34+
description = "X wins with left-hand dead end fork"
35+
36+
[ab167ab0-4a98-4d0f-a1c0-e1cddddc3d58]
37+
description = "X wins with right-hand dead end fork"
38+
39+
[73d1eda6-16ab-4460-9904-b5f5dd401d0b]
40+
description = "O wins crossing from top to bottom"
41+
42+
[c3a2a550-944a-4637-8b3f-1e1bf1340a3d]
43+
description = "X wins using a convoluted path"
44+
45+
[17e76fa8-f731-4db7-92ad-ed2a285d31f3]
46+
description = "X wins using a spiral path"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
USING: connect io kernel lexer tools.test unicode ;
2+
IN: connect.tests
3+
4+
: STOP-HERE ( -- ) lexer get [ text>> length ] keep line<< ; parsing
5+
6+
"Connect:" print
7+
8+
"an empty board has no winner" print
9+
{ "" }
10+
[ {
11+
". . . . ."
12+
" . . . . ."
13+
" . . . . ."
14+
" . . . . ."
15+
" . . . . ."
16+
} winner ] unit-test
17+
18+
STOP-HERE
19+
20+
"X can win on a 1x1 board" print
21+
{ "X" }
22+
[ {
23+
"X"
24+
} winner ] unit-test
25+
26+
"O can win on a 1x1 board" print
27+
{ "O" }
28+
[ {
29+
"O"
30+
} winner ] unit-test
31+
32+
"only edges does not make a winner" print
33+
{ "" }
34+
[ {
35+
"O O O X"
36+
" X . . X"
37+
" X . . X"
38+
" X O O O"
39+
} winner ] unit-test
40+
41+
"illegal diagonal does not make a winner" print
42+
{ "" }
43+
[ {
44+
"X O . ."
45+
" O X X X"
46+
" O X O ."
47+
" . O X ."
48+
" X X O O"
49+
} winner ] unit-test
50+
51+
"nobody wins crossing adjacent angles" print
52+
{ "" }
53+
[ {
54+
"X . . ."
55+
" . X O ."
56+
" O . X O"
57+
" . O . X"
58+
" . . O ."
59+
} winner ] unit-test
60+
61+
"X wins crossing from left to right" print
62+
{ "X" }
63+
[ {
64+
". O . ."
65+
" O X X X"
66+
" O X O ."
67+
" X X O X"
68+
" . O X ."
69+
} winner ] unit-test
70+
71+
"X wins with left-hand dead end fork" print
72+
{ "X" }
73+
[ {
74+
". . X ."
75+
" X X . ."
76+
" . X X X"
77+
" O O O O"
78+
} winner ] unit-test
79+
80+
"X wins with right-hand dead end fork" print
81+
{ "X" }
82+
[ {
83+
". . X X"
84+
" X X . ."
85+
" . X X ."
86+
" O O O O"
87+
} winner ] unit-test
88+
89+
"O wins crossing from top to bottom" print
90+
{ "O" }
91+
[ {
92+
". O . ."
93+
" O X X X"
94+
" O O O ."
95+
" X X O X"
96+
" . O X ."
97+
} winner ] unit-test
98+
99+
"X wins using a convoluted path" print
100+
{ "X" }
101+
[ {
102+
". X X . ."
103+
" X . X . X"
104+
" . X . X ."
105+
" . X X . ."
106+
" O O O O O"
107+
} winner ] unit-test
108+
109+
"X wins using a spiral path" print
110+
{ "X" }
111+
[ {
112+
"O X X X X X X X X"
113+
" O X O O O O O O O"
114+
" O X O X X X X X O"
115+
" O X O X O O O X O"
116+
" O X O X X X O X O"
117+
" O X O O O X O X O"
118+
" O X X X X X O X O"
119+
" O O O O O O O X O"
120+
" X X X X X X X X O"
121+
} winner ] unit-test
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
USING: kernel ;
2+
IN: connect
3+
4+
: winner ( board -- str )
5+
"unimplemented" throw ;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Given a number n, determine what the nth prime is.
4+
5+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
6+
7+
If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"nth-prime/nth-prime.factor"
8+
],
9+
"test": [
10+
"nth-prime/nth-prime-tests.factor"
11+
],
12+
"example": [
13+
".meta/example.factor"
14+
]
15+
},
16+
"blurb": "Given a number n, determine what the nth prime is.",
17+
"source": "A variation on Problem 7 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=7"
19+
}

0 commit comments

Comments
 (0)