Skip to content

Commit

Permalink
Add stage 2 solutions for go, java and rust
Browse files Browse the repository at this point in the history
  • Loading branch information
andy1li committed Nov 15, 2024
1 parent 1e9d521 commit fe6352f
Show file tree
Hide file tree
Showing 34 changed files with 1,075 additions and 0 deletions.
11 changes: 11 additions & 0 deletions solutions/go/02-rg2/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/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

go build -o /tmp/codecrafters-build-redis-go app/*.go
11 changes: 11 additions & 0 deletions solutions/go/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 /tmp/codecrafters-build-redis-go "$@"
1 change: 1 addition & 0 deletions solutions/go/02-rg2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
33 changes: 33 additions & 0 deletions solutions/go/02-rg2/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for Go solutions to the
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).

In this challenge, you'll build a toy Redis clone that's capable of handling
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
event loops, the Redis protocol and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Redis implementation is in `app/server.go`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `go (1.19)` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`app/server.go`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
21 changes: 21 additions & 0 deletions solutions/go/02-rg2/code/app/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"net"
"os"
)

func main() {
l, err := net.Listen("tcp", "0.0.0.0:6379")
if err != nil {
fmt.Println("Failed to bind to port 6379")
os.Exit(1)
}
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err.Error())
os.Exit(1)
}
conn.Write([]byte("+PONG\r\n"))
}
11 changes: 11 additions & 0 deletions solutions/go/02-rg2/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Go version used to run your code
# on Codecrafters.
#
# Available versions: go-1.22
language_pack: go-1.22
11 changes: 11 additions & 0 deletions solutions/go/02-rg2/code/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// DON'T EDIT THIS!
//
// Codecrafters relies on this file being intact to run tests successfully. Any changes
// here will not reflect when CodeCrafters tests your code, and might even cause build
// failures.
//
// DON'T EDIT THIS!

module github.com/codecrafters-io/redis-starter-go

go 1.22
Empty file added solutions/go/02-rg2/code/go.sum
Empty file.
24 changes: 24 additions & 0 deletions solutions/go/02-rg2/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/compile.sh
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
go build -o /tmp/codecrafters-build-redis-go app/*.go
)

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec /tmp/codecrafters-build-redis-go "$@"
27 changes: 27 additions & 0 deletions solutions/go/02-rg2/diff/app/server.go.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@@ -1,24 +1,21 @@
package main

import (
"fmt"
"net"
"os"
)

-// Ensures gofmt doesn't remove the "net" and "os" imports in stage 1 (feel free to remove this!)
-var _ = net.Listen
-var _ = os.Exit
-
func main() {
l, err := net.Listen("tcp", "0.0.0.0:6379")
if err != nil {
fmt.Println("Failed to bind to port 6379")
os.Exit(1)
}
- _, err = l.Accept()
+ conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err.Error())
os.Exit(1)
}
+ conn.Write([]byte("+PONG\r\n"))
}
26 changes: 26 additions & 0 deletions solutions/go/02-rg2/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
The entry point for your Redis implementation is in `app/server.go`.

Study and uncomment the relevant code:

```go
// Uncomment this block to pass the first stage

l, err := net.Listen("tcp", "0.0.0.0:6379")
if err != nil {
fmt.Println("Failed to bind to port 6379")
os.Exit(1)
}
_, err = l.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err.Error())
os.Exit(1)
}
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
11 changes: 11 additions & 0 deletions solutions/java/02-rg2/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/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

mvn -B package -Ddir=/tmp/codecrafters-build-redis-java
11 changes: 11 additions & 0 deletions solutions/java/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 java -jar /tmp/codecrafters-build-redis-java/codecrafters-redis.jar "$@"
1 change: 1 addition & 0 deletions solutions/java/02-rg2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
3 changes: 3 additions & 0 deletions solutions/java/02-rg2/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.jar
target/
.idea/
34 changes: 34 additions & 0 deletions solutions/java/02-rg2/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for Java solutions to the
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).

In this challenge, you'll build a toy Redis clone that's capable of handling
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
event loops, the Redis protocol and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your Redis implementation is in `src/main/java/Main.java`.
Study and uncomment the relevant code, and push your changes to pass the first
stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `mvn` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`src/main/java/Main.java`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions solutions/java/02-rg2/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Java version used to run your code
# on Codecrafters.
#
# Available versions: java-23
language_pack: java-23
52 changes: 52 additions & 0 deletions solutions/java/02-rg2/code/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>codecrafters-redis</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>23</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>codecrafters-redis</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
32 changes: 32 additions & 0 deletions solutions/java/02-rg2/code/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket clientSocket = null;
int port = 6379;
try {
serverSocket = new ServerSocket(port);
// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
serverSocket.setReuseAddress(true);
// Wait for connection from client.
clientSocket = serverSocket.accept();
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write("+PONG\r\n".getBytes());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
} finally {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
}
24 changes: 24 additions & 0 deletions solutions/java/02-rg2/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/compile.sh
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
mvn -B package -Ddir=/tmp/codecrafters-build-redis-java
)

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec java -jar /tmp/codecrafters-build-redis-java/codecrafters-redis.jar "$@"
Loading

0 comments on commit fe6352f

Please sign in to comment.