Skip to content

Commit eeea18a

Browse files
marcelweikumWeikum, Marcel
authored andcommitted
Rebased branch
1 parent e6e47e1 commit eeea18a

12 files changed

Lines changed: 18309 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,21 @@
12951295
"practices": [],
12961296
"prerequisites": [],
12971297
"difficulty": 3
1298+
},
1299+
{
1300+
"slug": "connect",
1301+
"name": "Connect",
1302+
"uuid": "86ba61cc-f88b-46b3-af2c-8ae304892cad",
1303+
"practices": [
1304+
"if-statements"
1305+
],
1306+
"prerequisites": [
1307+
"strings",
1308+
"vector-arrays",
1309+
"if-statements",
1310+
"loops"
1311+
],
1312+
"difficulty": 7
12981313
}
12991314
],
13001315
"foregone": [
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+
"files": {
4+
"solution": [
5+
"connect.cpp",
6+
"connect.h"
7+
],
8+
"test": [
9+
"connect_test.cpp"
10+
],
11+
"example": [
12+
".meta/example.cpp",
13+
".meta/example.h"
14+
]
15+
},
16+
"blurb": "Compute the result for a game of Hex / Polygon."
17+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "connect.h"
2+
3+
#include <vector>
4+
#include <string>
5+
#include <queue>
6+
7+
namespace connect {
8+
9+
static const int dx[6] = {-1, -1, 0, 0, 1, 1};
10+
static const int dy[6] = {0, 1, -1, 1, 0, -1};
11+
12+
std::string winner(const std::vector<std::string>& board) {
13+
int n = board.size();
14+
if (n == 0) return "";
15+
16+
std::vector<std::vector<char>> grid(n);
17+
for (int i = 0; i < n; ++i) {
18+
for (char c : board[i]) {
19+
if (c != ' ') {
20+
grid[i].push_back(c);
21+
}
22+
}
23+
}
24+
int m = grid[0].size();
25+
26+
auto bfs = [&](char player) {
27+
std::vector<std::vector<bool>> vis(n, std::vector<bool>(m, false));
28+
std::queue<std::pair<int,int>> q;
29+
30+
if (player == 'X') {
31+
for (int i = 0; i < n; ++i) {
32+
if (grid[i][0] == 'X') {
33+
q.push({i, 0});
34+
vis[i][0] = true;
35+
}
36+
}
37+
} else {
38+
for (int j = 0; j < m; ++j) {
39+
if (grid[0][j] == 'O') {
40+
q.push({0, j});
41+
vis[0][j] = true;
42+
}
43+
}
44+
}
45+
46+
while (!q.empty()) {
47+
auto [x, y] = q.front(); q.pop();
48+
if ((player == 'X' && y == m - 1) ||
49+
(player == 'O' && x == n - 1)) {
50+
return true;
51+
}
52+
for (int d = 0; d < 6; ++d) {
53+
int nx = x + dx[d];
54+
int ny = y + dy[d];
55+
if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
56+
!vis[nx][ny] && grid[nx][ny] == player) {
57+
vis[nx][ny] = true;
58+
q.push({nx, ny});
59+
}
60+
}
61+
}
62+
return false;
63+
};
64+
65+
if (bfs('X')) return "X";
66+
if (bfs('O')) return "O";
67+
return "";
68+
}
69+
70+
} // namespace connect
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <string>
5+
6+
namespace connect {
7+
8+
std::string winner(const std::vector<std::string>& board);
9+
10+
} // namespace connect
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[73d1eda6-16ab-4460-9904-b5f5dd401d0b]
34+
description = "O wins crossing from top to bottom"
35+
36+
[c3a2a550-944a-4637-8b3f-1e1bf1340a3d]
37+
description = "X wins using a convoluted path"
38+
39+
[17e76fa8-f731-4db7-92ad-ed2a285d31f3]
40+
description = "X wins using a spiral path"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Get the exercise name from the current directory
2+
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
3+
4+
# Basic CMake project
5+
cmake_minimum_required(VERSION 3.5.1)
6+
7+
# Name the project after the exercise
8+
project(${exercise} CXX)
9+
10+
# Get a source filename from the exercise name by replacing -'s with _'s
11+
string(REPLACE "-" "_" file ${exercise})
12+
13+
# Implementation could be only a header
14+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
15+
set(exercise_cpp ${file}.cpp)
16+
else()
17+
set(exercise_cpp "")
18+
endif()
19+
20+
# Use the common Catch library?
21+
if(EXERCISM_COMMON_CATCH)
22+
# For Exercism track development only
23+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
24+
elseif(EXERCISM_TEST_SUITE)
25+
# The Exercism test suite is being run, the Docker image already
26+
# includes a pre-built version of Catch.
27+
find_package(Catch2 REQUIRED)
28+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
29+
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
30+
# When Catch is installed system wide we need to include a different
31+
# header, we need this define to use the correct one.
32+
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
33+
else()
34+
# Build executable from sources and headers
35+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
36+
endif()
37+
38+
set_target_properties(${exercise} PROPERTIES
39+
CXX_STANDARD 17
40+
CXX_STANDARD_REQUIRED OFF
41+
CXX_EXTENSIONS OFF
42+
)
43+
44+
set(CMAKE_BUILD_TYPE Debug)
45+
46+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
47+
set_target_properties(${exercise} PROPERTIES
48+
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
49+
)
50+
endif()
51+
52+
# Configure to run all the tests?
53+
if(${EXERCISM_RUN_ALL_TESTS})
54+
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
55+
endif()
56+
57+
# Tell MSVC not to warn us about unchecked iterators in debug builds
58+
# Treat warnings as errors
59+
# Treat type conversion warnings C4244 and C4267 as level 4 warnings, i.e. ignore them in level 3
60+
if(${MSVC})
61+
set_target_properties(${exercise} PROPERTIES
62+
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS
63+
COMPILE_FLAGS "/WX /w44244 /w44267")
64+
endif()
65+
66+
# Run the tests on every build
67+
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "connect.h"
2+
3+
namespace connect {
4+
5+
// TODO: add your solution here
6+
7+
} // namespace connect
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace connect {
4+
5+
// TODO: add your solution here
6+
7+
} // namespace connect
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "connect.h"
2+
#ifdef EXERCISM_TEST_SUITE
3+
#include <catch2/catch.hpp>
4+
#else
5+
#include "test/catch.hpp"
6+
#endif
7+
8+
#include <vector>
9+
#include <string>
10+
11+
TEST_CASE("an empty board has no winner", "[6eff0df4-3e92-478d-9b54-d3e8b354db56]") {
12+
std::vector<std::string> board = {
13+
". . . . .",
14+
" . . . . .",
15+
" . . . . .",
16+
" . . . . .",
17+
" . . . . ."
18+
};
19+
REQUIRE(connect::winner(board) == "");
20+
}
21+
22+
TEST_CASE("X can win on a 1x1 board", "[298b94c0-b46d-45d8-b34b-0fa2ea71f0a4]") {
23+
std::vector<std::string> board = { "X" };
24+
REQUIRE(connect::winner(board) == "X");
25+
}
26+
27+
TEST_CASE("O can win on a 1x1 board", "[763bbae0-cb8f-4f28-bc21-5be16a5722dc]") {
28+
std::vector<std::string> board = { "O" };
29+
REQUIRE(connect::winner(board) == "O");
30+
}
31+
32+
TEST_CASE("only edges does not make a winner", "[819fde60-9ae2-485e-a024-cbb8ea68751b]") {
33+
std::vector<std::string> board = {
34+
"O O O X",
35+
" X . . X",
36+
" X . . X",
37+
" X O O O"
38+
};
39+
REQUIRE(connect::winner(board) == "");
40+
}
41+
42+
TEST_CASE("illegal diagonal does not make a winner", "[2c56a0d5-9528-41e5-b92b-499dfe08506c]") {
43+
std::vector<std::string> board = {
44+
"X O . .",
45+
" O X X X",
46+
" O X O .",
47+
" . O X .",
48+
" X X O O"
49+
};
50+
REQUIRE(connect::winner(board) == "");
51+
}
52+
53+
TEST_CASE("nobody wins crossing adjacent angles", "[41cce3ef-43ca-4963-970a-c05d39aa1cc1]") {
54+
std::vector<std::string> board = {
55+
"X . . .",
56+
" . X O .",
57+
" O . X O",
58+
" . O . X",
59+
" . . O ."
60+
};
61+
REQUIRE(connect::winner(board) == "");
62+
}
63+
64+
TEST_CASE("X wins crossing from left to right", "[cd61c143-92f6-4a8d-84d9-cb2b359e226b]") {
65+
std::vector<std::string> board = {
66+
". O . .",
67+
" O X X X",
68+
" O X O .",
69+
" X X O X",
70+
" . O X ."
71+
};
72+
REQUIRE(connect::winner(board) == "X");
73+
}
74+
75+
TEST_CASE("O wins crossing from top to bottom", "[73d1eda6-16ab-4460-9904-b5f5dd401d0b]") {
76+
std::vector<std::string> board = {
77+
". O . .",
78+
" O X X X",
79+
" O O O .",
80+
" X X O X",
81+
" . O X ."
82+
};
83+
REQUIRE(connect::winner(board) == "O");
84+
}
85+
86+
TEST_CASE("X wins using a convoluted path", "[c3a2a550-944a-4637-8b3f-1e1bf1340a3d]") {
87+
std::vector<std::string> board = {
88+
". X X . .",
89+
" X . X . X",
90+
" . X . X .",
91+
" . X X . .",
92+
" O O O O O"
93+
};
94+
REQUIRE(connect::winner(board) == "X");
95+
}
96+
97+
TEST_CASE("X wins using a spiral path", "[17e76fa8-f731-4db7-92ad-ed2a285d31f3]") {
98+
std::vector<std::string> board = {
99+
"O X X X X X X X X",
100+
" O X O O O O O O O",
101+
" O X O X X X X X O",
102+
" O X O X O O O X O",
103+
" O X O X X X O X O",
104+
" O X O O O X O X O",
105+
" O X X X X X O X O",
106+
" O O O O O O O X O",
107+
" X X X X X X X X O"
108+
};
109+
REQUIRE(connect::winner(board) == "X");
110+
}

0 commit comments

Comments
 (0)