From 1d8c4546a63bedac5a5638bd502a06c36918f3dd Mon Sep 17 00:00:00 2001 From: pompydev Date: Fri, 26 Apr 2024 20:35:15 +0900 Subject: [PATCH] chore: initial commit --- .gitignore | 1 + .vscode/extensions.json | 9 +++++++++ .vscode/settings.json | 35 +++++++++++++++++++++++++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++++++++ asm/.asm | 20 ++++++++++++++++++++ asm/.gitignore | 2 ++ asm/asm.justfile | 11 +++++++++++ c/.c | 6 ++++++ c/.gitignore | 1 + c/c.justfile | 8 ++++++++ cpp/.gitignore | 1 + cpp/a.cpp | 12 ++++++++++++ cpp/cpp.justfile | 8 ++++++++ go/.gitignore | 1 + go/go.justfile | 8 ++++++++ go/go.mod | 3 +++ go/main.go | 32 ++++++++++++++++++++++++++++++++ input.txt | 1 + js/.js | 16 ++++++++++++++++ js/.prettierrc | 3 +++ js/js.justfile | 5 +++++ justfile | 10 ++++++++++ py/.gitignore | 0 py/.py | 16 ++++++++++++++++ py/py.justfile | 5 +++++ rust/.gitignore | 20 ++++++++++++++++++++ rust/Cargo.toml | 6 ++++++ rust/rust.justfile | 2 ++ rust/src/main.rs | 32 ++++++++++++++++++++++++++++++++ 29 files changed, 313 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 README.md create mode 100644 asm/.asm create mode 100644 asm/.gitignore create mode 100644 asm/asm.justfile create mode 100644 c/.c create mode 100644 c/.gitignore create mode 100644 c/c.justfile create mode 100644 cpp/.gitignore create mode 100644 cpp/a.cpp create mode 100644 cpp/cpp.justfile create mode 100644 go/.gitignore create mode 100644 go/go.justfile create mode 100644 go/go.mod create mode 100644 go/main.go create mode 100644 input.txt create mode 100644 js/.js create mode 100644 js/.prettierrc create mode 100644 js/js.justfile create mode 100644 justfile create mode 100644 py/.gitignore create mode 100644 py/.py create mode 100644 py/py.justfile create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.toml create mode 100644 rust/rust.justfile create mode 100644 rust/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40e5032 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/_/ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..1bf6bc0 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "esbenp.prettier-vscode", + "ms-python.black-formatter", + "ms-vscode.cpptools", + "13xforever.language-x86-64-assembly", + "skellock.just" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2098f48 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,35 @@ +{ + "editor.formatOnPaste": true, + "editor.formatOnSave": true, + "editor.formatOnType": true, + + "cSpell.words": ["developomp", "fstat", "nums", "rustup", "substr", "yasm"], + "dotnet.preferCSharpExtension": true, + + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "[json]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[jsonc]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[markdown]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[c]": { + "editor.defaultFormatter": "ms-vscode.cpptools" + }, + "files.associations": { + "*.ejs": "html", + "ostream": "cpp", + "format": "cpp" + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..6737cc4 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Playground + +This is [developomp](https://github.com/developomp)'s vscode-based coding playground. + +The playground is used to... + +- quickly test and benchmark snippets of code +- solve algorithm problems + +## Setting up + +Setup the following: + +- shell (only if you're a Windows user. Get [Git bash](https://git-scm.com/downloads) / [GitHub Desktop](https://desktop.github.com) / [Cygwin](https://cygwin.com/install.html) / ... ) +- [just](https://github.com/casey/just) +- [hyperfine](https://github.com/sharkdp/hyperfine) +- [python](https://python.org) +- [node](https://nodejs.org) +- [rust](https://rust-lang.org) (recommend using [rustup](https://github.com/rust-lang/rustup)) +- [go](https://go.dev) +- [clang](https://clang.llvm.org) +- [GCC](https://gcc.gnu.org) +- [yasm](https://yasm.tortall.net) + +You can check other dev tools I use and more over [here](https://github.com/developomp/pompup). + +## Usage + +Simply call just recipes using the `just` command runner CLI. + +Examples: + +```bash +just run-go # automatically build go code before running it and pass arguments from input.txt +``` + +```bash +just bench-asm # automatically build and benchmark program from x86_64 assembly using yasm and hyperfine +``` diff --git a/asm/.asm b/asm/.asm new file mode 100644 index 0000000..9e571ca --- /dev/null +++ b/asm/.asm @@ -0,0 +1,20 @@ +; export _start label and make entry point to the programing +global _start + +section .text + _start: + ; write(1, message, length) + mov rax, 1 ; system call for write + mov rdi, 1 ; making file handle stdout + mov rsi, message ; passing adress of string to output + mov rdx, length ; number of bytes + syscall ; invoking os to write + + ; exit(0) + mov rax, 60 ; sys call for exit + xor rdi, rdi ; exit code 0 + syscall ; invoke os to exit + +section .data + message: db "Hello, asm!", 0xa ; const char* message = "Hello, asm!" oxa is "\n" + length: equ $-message ; int length = len(message); diff --git a/asm/.gitignore b/asm/.gitignore new file mode 100644 index 0000000..84c23a7 --- /dev/null +++ b/asm/.gitignore @@ -0,0 +1,2 @@ +a +.o diff --git a/asm/asm.justfile b/asm/asm.justfile new file mode 100644 index 0000000..ebd49f3 --- /dev/null +++ b/asm/asm.justfile @@ -0,0 +1,11 @@ +# https://www.devdungeon.com/content/hello-world-nasm-assembler + +@build-asm: + yasm -f elf64 asm/.asm -o asm/.o + ld asm/.o -o asm/a + +@run-asm: build-asm + ./asm/a < ./input.txt + +@bench-asm: build-asm + hyperfine './asm/a < ./input.txt' diff --git a/c/.c b/c/.c new file mode 100644 index 0000000..30e5abb --- /dev/null +++ b/c/.c @@ -0,0 +1,6 @@ +#include + +int main() +{ + printf("Hello C++!\n"); +} diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/c/.gitignore @@ -0,0 +1 @@ +a diff --git a/c/c.justfile b/c/c.justfile new file mode 100644 index 0000000..a57f30d --- /dev/null +++ b/c/c.justfile @@ -0,0 +1,8 @@ +@build-c: + clang ./c/.c -o ./c/a + +@run-c: build-c + ./c/a < ./input.txt + +@bench-c: build-c + hyperfine './c/a < ./input.txt' diff --git a/cpp/.gitignore b/cpp/.gitignore new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/cpp/.gitignore @@ -0,0 +1 @@ +a diff --git a/cpp/a.cpp b/cpp/a.cpp new file mode 100644 index 0000000..778ad69 --- /dev/null +++ b/cpp/a.cpp @@ -0,0 +1,12 @@ +#include + +#pragma GCC optimize("O3") +#pragma GCC optimize("Ofast") +#pragma GCC optimization("unroll-loops") + +using namespace std; + +int main() +{ + cout << "Hello, C++!\n"; +} diff --git a/cpp/cpp.justfile b/cpp/cpp.justfile new file mode 100644 index 0000000..0e14e02 --- /dev/null +++ b/cpp/cpp.justfile @@ -0,0 +1,8 @@ +@build-cpp: + g++ ./cpp/a.cpp -o ./cpp/a + +@run-cpp: build-cpp + ./cpp/a < ./input.txt + +@bench-cpp: build-cpp + hyperfine './cpp/a < ./input.txt' \ No newline at end of file diff --git a/go/.gitignore b/go/.gitignore new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/go/.gitignore @@ -0,0 +1 @@ +a diff --git a/go/go.justfile b/go/go.justfile new file mode 100644 index 0000000..f382c0f --- /dev/null +++ b/go/go.justfile @@ -0,0 +1,8 @@ +@build-go: + cd go && go build -o a + +@run-go: build-go + ./go/a < ./input.txt + +@bench-go: build-go + hyperfine './go/a < ./input.txt' diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..ed373ea --- /dev/null +++ b/go/go.mod @@ -0,0 +1,3 @@ +module github.com/developomp/playground + +go 1.22 diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..aa3faac --- /dev/null +++ b/go/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "bufio" + "os" + "strconv" + "strings" +) + +// prepare stuff for faster IO +var reader = bufio.NewReader(os.Stdin) +var writer = bufio.NewWriter(os.Stdout) + +func main() { + defer writer.Flush() // manually flush stdout + + // read stdin + b, _, _ := reader.ReadLine() + + // parse string + s := string(b) + sum := 0 + for _, s := range strings.Fields(s) { + num, _ := strconv.Atoi(s) + sum += num + } + + // print sum of nums + writer.WriteString(strings.ReplaceAll(s, " ", " + ") + " = " + strconv.Itoa(sum) + "\n") + // or + // fmt.Fprintf(writer, "%s = %d\n", strings.ReplaceAll(s, " ", " + "), sum) +} diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..703ca85 --- /dev/null +++ b/input.txt @@ -0,0 +1 @@ +1 2 3 \ No newline at end of file diff --git a/js/.js b/js/.js new file mode 100644 index 0000000..c0db5c3 --- /dev/null +++ b/js/.js @@ -0,0 +1,16 @@ +// prepare stuff for faster IO +const fs = require("fs") + +// read stdin +const s = fs.readFileSync(process.stdin.fd).toString() + +// parse string +const nums = s.split(" ").map(Number) + +// print sum of nums +process.stdout.write( + s.replaceAll(" ", " + ") + + " = " + + nums.reduce((acc, curr) => acc + curr, 0) + + "\n" +) diff --git a/js/.prettierrc b/js/.prettierrc new file mode 100644 index 0000000..cce9d3c --- /dev/null +++ b/js/.prettierrc @@ -0,0 +1,3 @@ +{ + "semi": false +} diff --git a/js/js.justfile b/js/js.justfile new file mode 100644 index 0000000..0f0f23e --- /dev/null +++ b/js/js.justfile @@ -0,0 +1,5 @@ +@run-js: + node ./js/.js < ./input.txt + +@bench-js: + hyperfine 'node ./js/.js < ./input.txt' diff --git a/justfile b/justfile new file mode 100644 index 0000000..35d09ba --- /dev/null +++ b/justfile @@ -0,0 +1,10 @@ +import 'asm/asm.justfile' +import 'c/c.justfile' +import 'cpp/cpp.justfile' +import 'go/go.justfile' +import 'js/js.justfile' +import 'py/py.justfile' +import 'rust/rust.justfile' + +@default: + just --list --unsorted diff --git a/py/.gitignore b/py/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/py/.py b/py/.py new file mode 100644 index 0000000..65be342 --- /dev/null +++ b/py/.py @@ -0,0 +1,16 @@ +import io +import os +import sys + +# prepare stuff for faster IO +input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline +print = sys.stdout.write + +# read line +s = input().decode("utf-8") + +# parse string +nums = map(int, s.split(" ")) + +# print sum of nums +print(s.replace(" ", " + ") + " = " + str(sum(nums)) + "\n") diff --git a/py/py.justfile b/py/py.justfile new file mode 100644 index 0000000..ad25413 --- /dev/null +++ b/py/py.justfile @@ -0,0 +1,5 @@ +@run-py: + python py/.py < ./input.txt + +@bench-py: + hyperfine 'python py/.py < ./input.txt' diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..389159a --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,20 @@ +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# End of https://www.toptal.com/developers/gitignore/api/rust diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..91ee8d2 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "playground" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/rust/rust.justfile b/rust/rust.justfile new file mode 100644 index 0000000..00daf75 --- /dev/null +++ b/rust/rust.justfile @@ -0,0 +1,2 @@ +@run-rust: + cd rust && cargo run --quiet < ../input.txt diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..16dbf8f --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,32 @@ +use std::{ + fs::File, + io::{BufRead, BufReader, BufWriter, Write}, + os::unix::io::FromRawFd, +}; + +fn main() { + // prepare stuff for faster IO + let mut s = String::new(); + let stdin = unsafe { File::from_raw_fd(0) }; + let stdout = unsafe { File::from_raw_fd(1) }; + let mut reader = BufReader::new(stdin); + let mut writer = BufWriter::new(stdout); + + // read line + reader.read_line(&mut s).expect("Failed to read from stdin"); + + // parse string + let nums: Vec = s + .split(" ") + .map(|substr| substr.parse().expect("Failed to convert &str to usize")) + .collect(); + + // print sum of nums + writer + .write_fmt(format_args!( + "{} = {}\n", + s.replace(" ", " + "), + nums.iter().sum::() + )) + .expect("Failed to write to stdout"); +}