Skip to content

raj-open/code-challenges

Repository files navigation

Rust version: 1.86.*

qa manual:main qa manual:staging

qa auto:staging qa auto:current

Code Challenges

This repository contains code snippets written entirely by the repository's owner / author, as solutions to problems on various platforms.

We work here primarily with

  • Rust
  • python

System requirements

Important

We rely on Zig for cross-compilation, which avoids gcc-compiler issues on local machine and linux images, which in turn are required by the rust compiler.

Tip

To verify, open a bash terminal and call.

just --version
# rust
rustup --version
rustc --version
cargo --version
# python
. .venv/bin/activate && python3 --version # for unix
. .venv/Scripts/activate && python --version # for windows
# zig
zig version

Setup

Run

# only needed once
just setup
# now modify the created .env file

# only needed when code changes
just build

Testing

Unit tests

Unit tests for rust code accompany files in the same (sub)modules e.g. src/path/to/file.rs has unit test src/path/to/tests_file.rs. To run a unit test for a single file, call

just test-unit "path/to/file.rs"
# or
just test-unit "path/to/tests_file.rs"

Both options will run just the tests in the test_-file.

To run all unit tests throughout the repository, call

just tests-unit

Integration tests

The tests folder contains integration tests for rust code, and unit tests for python code.

Execution of Binaries

This repository contains some binaries (see Cargo.toml):

  • CodeChallenges
  • HackerRankMathematics
  • GeniusSquare

To run a binary call

# runs with optimisation (slower compile time)
just run-rust {NAME_OF_BINARY} [flags]
# runs without optimisation (faster compile time, slower run time)
just dev-rust {NAME_OF_BINARY} [flags]

Genius Squares

The binary GeniusSquare solves instances of the Smart Games puzzle Genius Square. Usage is as follows:

# provides random instance of the puzzle and solves it:
just run-rust GeniusSquare
# ... with random seeding for repeatability:
just run-rust GeniusSquare {Seed}
# solves an instance of the game for a particular initialisation (roll of the dice):
just run-rust GeniusSquare {Dice1} {Dice2} ... {Dice7}

e.g.

just run-rust GeniusSquare 1234 # with random seed
just run-rust GeniusSquare B1 C4 D6 F1 F2 F3 F5 # with given initialisation

The run command builds and runs the binary. To perform this separately, use

just build-compile GeniusSquare

which copy the binary in target/release/GeniusSquare to the dist folder. The standalone binary can be called as above:

./dist/GeniusSquare
# with random seed
./dist/GeniusSquare {Seed}
./dist/GeniusSquare 1234
# with given initialisation
./dist/GeniusSquare {Dice1} {Dice2} ... {Dice7}
./dist/GeniusSquare B1 C4 D6 F1 F2 F3 F5

Note on the algorithm

The solver currently relies on a depth-first tree-search algorithm, with adaptive sorting, i.e. once pieces have been placed, the order of computation of the remaining pieces are locally sorted (in ascending order) based on the number of next possible moves.

Example

Calling

just run-rust GeniusSquare B1 C4 D2 D6 E5 F1 F3

results in

Roll: B1 C4 D2 D6 E5 F1 F3.

Problem:
      ╔═══╦═══╦═══╦═══╦═══╦═══╗
      ║ A ║ B ║ C ║ D ║ E ║ F ║
      ╚═══╩═══╩═══╩═══╩═══╩═══╝
╔═══╗     ┼───┼           ┼───┼
║ 1 ║     │ ■ │           │ ■ │
╠═══╣     ┼───┼   ┼───┼   ┼───┼
║ 2 ║             │ ■ │
╠═══╣             ┼───┼   ┼───┼
║ 3 ║                     │ ■ │
╠═══╣         ┼───┼       ┼───┼
║ 4 ║         │ ■ │
╠═══╣         ┼───┼   ┼───┼
║ 5 ║                 │ ■ │
╠═══╣             ┼───┼───┼
║ 6 ║             │ ■ │
╚═══╝             ┼───┼

Compute solution ... found 33 solutions.
Time for 1st solution:      3.929ms
Average time per solution:  2.48506ms
Total time:                 82.007ms

Solution 1:
      ╔═══╦═══╦═══╦═══╦═══╦═══╗
      ║ A ║ B ║ C ║ D ║ E ║ F ║
      ╚═══╩═══╩═══╩═══╩═══╩═══╝
╔═══╗ ┼───┼───┼───┼───┼───┼───┼
║ 1 ║ │ 2 │ ■ │ Z   Z │ C │ ■ │
╠═══╣ ┼   ┼───┼   ┼───┼   ┼───┼
║ 2 ║ │ 2 │ Z   Z │ ■ │ C   C │
╠═══╣ ┼───┼───┼───┼───┼───┼───┼
║ 3 ║ │ T   T   T │ X   X │ ■ │
╠═══╣ ┼───┼   ┼───┼       ┼───┼
║ 4 ║ │ 1 │ T │ ■ │ X   X │ L │
╠═══╣ ┼───┼───┼───┼───┼───┼   ┼
║ 5 ║ │ 4   4   4   4 │ ■ │ L │
╠═══╣ ┼───┼───┼───┼───┼───┼   ┼
║ 6 ║ │ 3   3   3 │ ■ │ L   L │
╚═══╝ ┼───┼───┼───┼───┼───┼───┼

in the console. The algoirithm provides solutions at the Wizard level, viz. no collisions occur and none of the pieces

┌───┐ ┌───┐ ┌───┐ ┌───┬───┐
│ 1 │ │ 2 │ │ 3 │ │ C   C │
└───┘ ├   ┤ ├   ┤ ├   ┼───┘
      │ 2 │ │ 3 │ │ C │
      └───┘ ├   ┤ └───┘
            │ 3 │
            └───┘

are adjacent (in the sense of touching edges).