Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sunsided/snafu-numbers
Special Numeral-Analogue Fuel Units in Rust
https://github.com/sunsided/snafu-numbers
advent-of-code advent-of-code-2022 rust snafu-numbers
Last synced: 19 days ago
JSON representation
Special Numeral-Analogue Fuel Units in Rust
- Host: GitHub
- URL: https://github.com/sunsided/snafu-numbers
- Owner: sunsided
- License: eupl-1.2
- Created: 2023-05-21T16:17:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-07T20:17:54.000Z (about 1 year ago)
- Last Synced: 2024-11-09T08:12:46.804Z (about 2 months ago)
- Topics: advent-of-code, advent-of-code-2022, rust, snafu-numbers
- Language: Rust
- Homepage: https://crates.io/crates/snafu-numbers
- Size: 38.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Special Numeral-Analogue Fuel Units
A Rust crate ~~and utility~~ to deal with conversions of SNAFU values
from Advent of Code 2022, Day 25 ([here](https://adventofcode.com/2022/day/25)).
The file [AOC-22-25.md](AOC-22-25.md) contains the full puzzle in case the
website is unavailable.Example conversions from SNAFU:
```rust
use snafu_numbers::FromSnafu;
assert_eq!(u32::from_snafu("1=-0-2"), 1747);
assert_eq!(u32::from_snafu("20012"), 1257);
assert_eq!(u32::from_snafu("1="), 3);
```... and into SNAFU:
```rust
use snafu_numbers::IntoSnafu;
assert_eq!(1747_u32.into_snafu(), "1=-0-2");
```## SNAFU numbers
SNAFU numbers are a power-of-5 centric base-10 system written right to left.
The zero-th (i.e., right-most) place represents a multiple of 50 = 0, the first
represents a multiple 51 = 5, the second place 52 = 25,
the third place 53 = 625, etc.Five different digits are used. Here is a list alongside their decimal integer representation:
| SNAFU digit | Name | Decimal / [ℤ] |
|-------------|--------------|---------------|
| `2` | two | `2` |
| `1` | one | `1` |
| `0` | zero | `0` |
| `-` | minus | `-1` |
| `=` | double-minus | `-2` |[ℤ]: https://en.wikipedia.org/wiki/Integer
As a result, the individual values in each position `n` is 2×5n-1, so
| Position | Base | `=` | `-` | `0` | `1` | `2` |
|----------|------------------------|---------|---------|-----|--------|--------|
| 0 | 50 = `1` | `-2` | `1` | `0` | `1` | `2` |
| 1 | 51 = `5` | `-10` | `-5` | `0` | `5` | `10` |
| 2 | 52 = `25` | `-50` | `-25` | `0` | `25` | `50` |
| 3 | 53 = `125` | `-250` | `-125` | `0` | `125` | `250` |
| 4 | 54 = `625` | `-1250` | `-625` | `0` | `625` | `1250` |
| 5 | 55 = `3125` | `-6250` | `-3125` | `0` | `3125` | `6250` |etc.
To quote the rules:
> Say you have the SNAFU number `2=-01`. That's `2` in
the 625s place, `=` (double-minus) in the 125s place, `-` (minus) in the 25s place,
`0` in the 5s place, and 1 in the `1`s place.
(2 times 625) plus (-2 times 125) plus (-1 times 25) plus (0 times 5) plus (1 times 1).
That's 1250 plus -250 plus -25 plus 0 plus 1. **976**!"### Example conversion from decimal to SNAFU
| Decimal | SNAFU |
|-------------|-----------------|
| `1` | `1` |
| `2` | `2` |
| `3` | `1=` |
| `4` | `1-` |
| `5` | `10` |
| `6` | `11` |
| `7` | `12` |
| `8` | `2=` |
| `9` | `2-` |
| `10` | `20` |
| `15` | `1=0` |
| `20` | `1-0` |
| `2022` | `1=11-2` |
| `12345` | `1-0---0` |
| `314159265` | `1121-1110-1=0` |### Example conversion from SNAFU to decimal
| SNAFU | Decimal |
|----------|---------|
| `1=-0-2` | `1747` |
| `12111` | `906` |
| `2=0=` | `198` |
| `21` | `11` |
| `2=01` | `201` |
| `111` | `31` |
| `20012` | `1257` |
| `112` | `32` |
| `1=-1=` | `353` |
| `1-12` | `107` |
| `12` | `7` |
| `1=` | `3` |
| `122` | `37` |