Skip to content

Commit

Permalink
Merge pull request #274 from codecrafters-io/add-more-stage-2-solutions
Browse files Browse the repository at this point in the history
CC-1502: Add solutions for stage 2 Redis (go, java, rust, cpp)
  • Loading branch information
andy1li authored Nov 15, 2024
2 parents 1e9d521 + 6ae78e3 commit b2a4347
Show file tree
Hide file tree
Showing 52 changed files with 1,517 additions and 205 deletions.
73 changes: 36 additions & 37 deletions compiled_starters/cpp/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,49 @@ int main(int argc, char **argv) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
std::cerr << "Failed to create server socket\n";
return 1;
}

// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
std::cerr << "setsockopt failed\n";
return 1;
}

struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(6379);

if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
std::cerr << "Failed to bind to port 6379\n";
return 1;
}

int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
std::cerr << "listen failed\n";
return 1;
}

struct sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
std::cout << "Waiting for a client to connect...\n";

// You can use print statements as follows for debugging, they'll be visible when running tests.
std::cout << "Logs from your program will appear here!\n";

// Uncomment this block to pass the first stage
//
// int server_fd = socket(AF_INET, SOCK_STREAM, 0);
// if (server_fd < 0) {
// std::cerr << "Failed to create server socket\n";
// return 1;
// }
//
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
// // ensures that we don't run into 'Address already in use' errors
// int reuse = 1;
// if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
// std::cerr << "setsockopt failed\n";
// return 1;
// }
//
// struct sockaddr_in server_addr;
// server_addr.sin_family = AF_INET;
// server_addr.sin_addr.s_addr = INADDR_ANY;
// server_addr.sin_port = htons(6379);
//
// if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
// std::cerr << "Failed to bind to port 6379\n";
// return 1;
// }
//
// int connection_backlog = 5;
// if (listen(server_fd, connection_backlog) != 0) {
// std::cerr << "listen failed\n";
// return 1;
// }
//
// struct sockaddr_in client_addr;
// int client_addr_len = sizeof(client_addr);
//
// std::cout << "Waiting for a client to connect...\n";
//
//
// accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
// std::cout << "Client connected\n";
//
//
// close(server_fd);

return 0;
Expand Down
13 changes: 6 additions & 7 deletions solutions/cpp/01-jm1/code/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,39 @@ int main(int argc, char **argv) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
std::cerr << "Failed to create server socket\n";
return 1;
}

// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
std::cerr << "setsockopt failed\n";
return 1;
}

struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(6379);

if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
std::cerr << "Failed to bind to port 6379\n";
return 1;
}

int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
std::cerr << "listen failed\n";
return 1;
}

struct sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);

std::cout << "Waiting for a client to connect...\n";

accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
Expand Down
118 changes: 29 additions & 89 deletions solutions/cpp/01-jm1/diff/src/Server.cpp.diff
Original file line number Diff line number Diff line change
@@ -1,100 +1,40 @@
@@ -1,62 +1,57 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc, char **argv) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;
@@ -25,37 +25,32 @@
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
std::cerr << "setsockopt failed\n";
return 1;
}

struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(6379);

if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
std::cerr << "Failed to bind to port 6379\n";
return 1;
}

int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
std::cerr << "listen failed\n";
return 1;
}

struct sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
std::cout << "Waiting for a client to connect...\n";

- // You can use print statements as follows for debugging, they'll be visible when running tests.
- std::cout << "Logs from your program will appear here!\n";
+ int server_fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (server_fd < 0) {
+ std::cerr << "Failed to create server socket\n";
+ return 1;
+ }
+ accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
+ std::cout << "Client connected\n";

- // Uncomment this block to pass the first stage
- //
- // int server_fd = socket(AF_INET, SOCK_STREAM, 0);
- // if (server_fd < 0) {
- // std::cerr << "Failed to create server socket\n";
- // return 1;
- // }
- //
- // // Since the tester restarts your program quite often, setting SO_REUSEADDR
- // // ensures that we don't run into 'Address already in use' errors
- // int reuse = 1;
- // if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
- // std::cerr << "setsockopt failed\n";
- // return 1;
- // }
- //
- // struct sockaddr_in server_addr;
- // server_addr.sin_family = AF_INET;
- // server_addr.sin_addr.s_addr = INADDR_ANY;
- // server_addr.sin_port = htons(6379);
- //
- // if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
- // std::cerr << "Failed to bind to port 6379\n";
- // return 1;
- // }
- //
- // int connection_backlog = 5;
- // if (listen(server_fd, connection_backlog) != 0) {
- // std::cerr << "listen failed\n";
- // return 1;
- // }
- //
- // struct sockaddr_in client_addr;
- // int client_addr_len = sizeof(client_addr);
- //
- // std::cout << "Waiting for a client to connect...\n";
- //
- //
- // accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
- // std::cout << "Client connected\n";
- //
- //
- // close(server_fd);
+ // Since the tester restarts your program quite often, setting SO_REUSEADDR
+ // ensures that we don't run into 'Address already in use' errors
+ int reuse = 1;
+ if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
+ std::cerr << "setsockopt failed\n";
+ return 1;
+ }
+
+ struct sockaddr_in server_addr;
+ server_addr.sin_family = AF_INET;
+ server_addr.sin_addr.s_addr = INADDR_ANY;
+ server_addr.sin_port = htons(6379);
+
+ if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
+ std::cerr << "Failed to bind to port 6379\n";
+ return 1;
+ }
+
+ int connection_backlog = 5;
+ if (listen(server_fd, connection_backlog) != 0) {
+ std::cerr << "listen failed\n";
+ return 1;
+ }
+
+ struct sockaddr_in client_addr;
+ int client_addr_len = sizeof(client_addr);
+
+ std::cout << "Waiting for a client to connect...\n";
+
+ accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
+ std::cout << "Client connected\n";
+
+ close(server_fd);

return 0;
Expand Down
35 changes: 0 additions & 35 deletions solutions/cpp/01-jm1/explanation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,6 @@ Study and uncomment the relevant code:
```cpp
// Uncomment this block to pass the first stage

int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
std::cerr << "Failed to create server socket\n";
return 1;
}

// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
std::cerr << "setsockopt failed\n";
return 1;
}

struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(6379);

if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
std::cerr << "Failed to bind to port 6379\n";
return 1;
}

int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
std::cerr << "listen failed\n";
return 1;
}

struct sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);

std::cout << "Waiting for a client to connect...\n";

accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
std::cout << "Client connected\n";

Expand Down
12 changes: 12 additions & 0 deletions solutions/cpp/02-rg2/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
11 changes: 11 additions & 0 deletions solutions/cpp/02-rg2/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec ./build/server "$@"
1 change: 1 addition & 0 deletions solutions/cpp/02-rg2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
49 changes: 49 additions & 0 deletions solutions/cpp/02-rg2/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
server

# CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps

build/
vcpkg_installed
Loading

0 comments on commit b2a4347

Please sign in to comment.