From 278da7592823565f4569ae6ed25bc28c0f56faba Mon Sep 17 00:00:00 2001 From: Andy Li <1450947+andy1li@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:43:12 +0800 Subject: [PATCH] Add stage 2 solutions for js and ts --- .../02-rg2/code/.codecrafters/compile.sh | 11 ++++++ .../02-rg2/code/.codecrafters/run.sh | 11 ++++++ .../javascript/02-rg2/code/.gitattributes | 1 + solutions/javascript/02-rg2/code/.gitignore | 1 + solutions/javascript/02-rg2/code/README.md | 33 ++++++++++++++++++ solutions/javascript/02-rg2/code/app/main.js | 8 +++++ .../javascript/02-rg2/code/codecrafters.yml | 11 ++++++ .../javascript/02-rg2/code/package-lock.json | 12 +++++++ solutions/javascript/02-rg2/code/package.json | 5 +++ .../javascript/02-rg2/code/your_program.sh | 15 ++++++++ .../javascript/02-rg2/diff/app/main.js.diff | 9 +++++ solutions/javascript/02-rg2/explanation.md | 20 +++++++++++ .../02-rg2/code/.codecrafters/compile.sh | 11 ++++++ .../02-rg2/code/.codecrafters/run.sh | 11 ++++++ .../typescript/02-rg2/code/.gitattributes | 1 + solutions/typescript/02-rg2/code/.gitignore | 1 + solutions/typescript/02-rg2/code/README.md | 33 ++++++++++++++++++ solutions/typescript/02-rg2/code/app/main.ts | 8 +++++ solutions/typescript/02-rg2/code/bun.lockb | Bin 0 -> 2709 bytes .../typescript/02-rg2/code/codecrafters.yml | 11 ++++++ solutions/typescript/02-rg2/code/package.json | 11 ++++++ .../typescript/02-rg2/code/tsconfig.json | 27 ++++++++++++++ .../typescript/02-rg2/code/your_program.sh | 15 ++++++++ .../typescript/02-rg2/diff/app/main.ts.diff | 9 +++++ solutions/typescript/02-rg2/explanation.md | 20 +++++++++++ 25 files changed, 295 insertions(+) create mode 100755 solutions/javascript/02-rg2/code/.codecrafters/compile.sh create mode 100755 solutions/javascript/02-rg2/code/.codecrafters/run.sh create mode 100644 solutions/javascript/02-rg2/code/.gitattributes create mode 100644 solutions/javascript/02-rg2/code/.gitignore create mode 100644 solutions/javascript/02-rg2/code/README.md create mode 100644 solutions/javascript/02-rg2/code/app/main.js create mode 100644 solutions/javascript/02-rg2/code/codecrafters.yml create mode 100644 solutions/javascript/02-rg2/code/package-lock.json create mode 100644 solutions/javascript/02-rg2/code/package.json create mode 100755 solutions/javascript/02-rg2/code/your_program.sh create mode 100644 solutions/javascript/02-rg2/diff/app/main.js.diff create mode 100644 solutions/javascript/02-rg2/explanation.md create mode 100755 solutions/typescript/02-rg2/code/.codecrafters/compile.sh create mode 100755 solutions/typescript/02-rg2/code/.codecrafters/run.sh create mode 100644 solutions/typescript/02-rg2/code/.gitattributes create mode 100644 solutions/typescript/02-rg2/code/.gitignore create mode 100644 solutions/typescript/02-rg2/code/README.md create mode 100644 solutions/typescript/02-rg2/code/app/main.ts create mode 100755 solutions/typescript/02-rg2/code/bun.lockb create mode 100644 solutions/typescript/02-rg2/code/codecrafters.yml create mode 100644 solutions/typescript/02-rg2/code/package.json create mode 100644 solutions/typescript/02-rg2/code/tsconfig.json create mode 100755 solutions/typescript/02-rg2/code/your_program.sh create mode 100644 solutions/typescript/02-rg2/diff/app/main.ts.diff create mode 100644 solutions/typescript/02-rg2/explanation.md diff --git a/solutions/javascript/02-rg2/code/.codecrafters/compile.sh b/solutions/javascript/02-rg2/code/.codecrafters/compile.sh new file mode 100755 index 00000000..e13a8a7a --- /dev/null +++ b/solutions/javascript/02-rg2/code/.codecrafters/compile.sh @@ -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 + +# (This file is empty since JavaScript programs don't use a compile step) diff --git a/solutions/javascript/02-rg2/code/.codecrafters/run.sh b/solutions/javascript/02-rg2/code/.codecrafters/run.sh new file mode 100755 index 00000000..66bbd824 --- /dev/null +++ b/solutions/javascript/02-rg2/code/.codecrafters/run.sh @@ -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 node app/main.js "$@" diff --git a/solutions/javascript/02-rg2/code/.gitattributes b/solutions/javascript/02-rg2/code/.gitattributes new file mode 100644 index 00000000..176a458f --- /dev/null +++ b/solutions/javascript/02-rg2/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/javascript/02-rg2/code/.gitignore b/solutions/javascript/02-rg2/code/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/solutions/javascript/02-rg2/code/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/solutions/javascript/02-rg2/code/README.md b/solutions/javascript/02-rg2/code/README.md new file mode 100644 index 00000000..f850a7b5 --- /dev/null +++ b/solutions/javascript/02-rg2/code/README.md @@ -0,0 +1,33 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png) + +This is a starting point for JavaScript 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/main.js`. 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 `node (21)` installed locally +1. Run `./your_program.sh` to run your Redis server, which is implemented in + `app/main.js`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/solutions/javascript/02-rg2/code/app/main.js b/solutions/javascript/02-rg2/code/app/main.js new file mode 100644 index 00000000..24cee2dc --- /dev/null +++ b/solutions/javascript/02-rg2/code/app/main.js @@ -0,0 +1,8 @@ +const net = require("net"); + +const server = net.createServer((connection) => { + // Handle connection + connection.write(`+PONG\r\n`); +}); + +server.listen(6379, "127.0.0.1"); diff --git a/solutions/javascript/02-rg2/code/codecrafters.yml b/solutions/javascript/02-rg2/code/codecrafters.yml new file mode 100644 index 00000000..e61c476c --- /dev/null +++ b/solutions/javascript/02-rg2/code/codecrafters.yml @@ -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 JavaScript version used to run your code +# on Codecrafters. +# +# Available versions: nodejs-21 +language_pack: nodejs-21 diff --git a/solutions/javascript/02-rg2/code/package-lock.json b/solutions/javascript/02-rg2/code/package-lock.json new file mode 100644 index 00000000..6dcd28c9 --- /dev/null +++ b/solutions/javascript/02-rg2/code/package-lock.json @@ -0,0 +1,12 @@ +{ + "name": "@codecrafters/redis", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@codecrafters/redis", + "version": "1.0.0" + } + } +} diff --git a/solutions/javascript/02-rg2/code/package.json b/solutions/javascript/02-rg2/code/package.json new file mode 100644 index 00000000..6c6a61ba --- /dev/null +++ b/solutions/javascript/02-rg2/code/package.json @@ -0,0 +1,5 @@ +{ + "name": "@codecrafters/redis", + "version": "1.0.0", + "dependencies": {} +} diff --git a/solutions/javascript/02-rg2/code/your_program.sh b/solutions/javascript/02-rg2/code/your_program.sh new file mode 100755 index 00000000..8b264dae --- /dev/null +++ b/solutions/javascript/02-rg2/code/your_program.sh @@ -0,0 +1,15 @@ +#!/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/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec node app/main.js "$@" diff --git a/solutions/javascript/02-rg2/diff/app/main.js.diff b/solutions/javascript/02-rg2/diff/app/main.js.diff new file mode 100644 index 00000000..0241ef8e --- /dev/null +++ b/solutions/javascript/02-rg2/diff/app/main.js.diff @@ -0,0 +1,9 @@ +@@ -1,7 +1,8 @@ + const net = require("net"); + + const server = net.createServer((connection) => { + // Handle connection ++ connection.write(`+PONG\r\n`); + }); + + server.listen(6379, "127.0.0.1"); diff --git a/solutions/javascript/02-rg2/explanation.md b/solutions/javascript/02-rg2/explanation.md new file mode 100644 index 00000000..11620436 --- /dev/null +++ b/solutions/javascript/02-rg2/explanation.md @@ -0,0 +1,20 @@ +The entry point for your Redis implementation is in `app/main.js`. + +Study and uncomment the relevant code: + +```javascript +// Uncomment this block to pass the first stage +const server = net.createServer((connection) => { + // Handle connection +}); + +server.listen(6379, "127.0.0.1"); +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` diff --git a/solutions/typescript/02-rg2/code/.codecrafters/compile.sh b/solutions/typescript/02-rg2/code/.codecrafters/compile.sh new file mode 100755 index 00000000..d8b7a95f --- /dev/null +++ b/solutions/typescript/02-rg2/code/.codecrafters/compile.sh @@ -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 + +# (This file is empty since TypeScript programs don't use a compile step) diff --git a/solutions/typescript/02-rg2/code/.codecrafters/run.sh b/solutions/typescript/02-rg2/code/.codecrafters/run.sh new file mode 100755 index 00000000..f41df1fe --- /dev/null +++ b/solutions/typescript/02-rg2/code/.codecrafters/run.sh @@ -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 bun run $(dirname $0)/app/main.ts "$@" diff --git a/solutions/typescript/02-rg2/code/.gitattributes b/solutions/typescript/02-rg2/code/.gitattributes new file mode 100644 index 00000000..176a458f --- /dev/null +++ b/solutions/typescript/02-rg2/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/typescript/02-rg2/code/.gitignore b/solutions/typescript/02-rg2/code/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/solutions/typescript/02-rg2/code/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/solutions/typescript/02-rg2/code/README.md b/solutions/typescript/02-rg2/code/README.md new file mode 100644 index 00000000..5b785454 --- /dev/null +++ b/solutions/typescript/02-rg2/code/README.md @@ -0,0 +1,33 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png) + +This is a starting point for TypeScript 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/main.ts`. 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 `bun (1.1)` installed locally +1. Run `./your_program.sh` to run your Redis server, which is implemented in + `app/main.ts`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/solutions/typescript/02-rg2/code/app/main.ts b/solutions/typescript/02-rg2/code/app/main.ts new file mode 100644 index 00000000..a6b3738e --- /dev/null +++ b/solutions/typescript/02-rg2/code/app/main.ts @@ -0,0 +1,8 @@ +import * as net from "net"; + +const server: net.Server = net.createServer((connection: net.Socket) => { + // Handle connection + connection.write(`+PONG\r\n`); +}); + +server.listen(6379, "127.0.0.1"); diff --git a/solutions/typescript/02-rg2/code/bun.lockb b/solutions/typescript/02-rg2/code/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..968db942c3656f91f1dad181e2779c928a31b564 GIT binary patch literal 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explanation.md b/solutions/typescript/02-rg2/explanation.md new file mode 100644 index 00000000..56d7ec0a --- /dev/null +++ b/solutions/typescript/02-rg2/explanation.md @@ -0,0 +1,20 @@ +The entry point for your Redis implementation is in `app/main.ts`. + +Study and uncomment the relevant code: + +```typescript +// Uncomment this block to pass the first stage +const server: net.Server = net.createServer((connection: net.Socket) => { + // Handle connection +}); + +server.listen(6379, "127.0.0.1"); +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +```