Crate snafu_numbers

Crate snafu_numbers 

Source
Expand description

§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).

Conversion is implemented via the FromSnafu/TryFromSnafu and IntoSnafu traits.

§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 digitNameDecimal / ℤ
2two2
1one1
0zero0
-minus-1
=double-minus-2

As a result, the individual values in each position n is 2×5n-1, so

PositionBase=-012
050 = 1-21012
151 = 5-10-50510
252 = 25-50-2502550
353 = 125-250-1250125250
454 = 625-1250-62506251250
555 = 3125-6250-3125031256250

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 1s 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

use snafu_numbers::IntoSnafu;
assert_eq!(1747_u32.into_snafu(), "1=-0-2");
assert_eq!(1257_u32.into_snafu(), "20012");
assert_eq!(3_u32.into_snafu(), "1=");
DecimalSNAFU
11
22
31=
41-
510
611
712
82=
92-
1020
151=0
201-0
20221=11-2
123451-0---0
3141592651121-1110-1=0

§Example conversion from SNAFU to decimal

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);
SNAFUDecimal
1=-0-21747
12111906
2=0=198
2111
2=01201
11131
200121257
11232
1=-1=353
1-12107
127
1=3
12237

Enums§

ConversionError

Traits§

FromSnafu
Provides conversion from SNAFU numbers.
IntoSnafu
Provides conversion into SNAFU numbers.
TryFromSnafu
Provides fallible conversion from SNAFU numbers.