Created
September 22, 2022 20:22
-
-
Save zperk13/1718970081fcbb51fefdbd1dfc98cfd5 to your computer and use it in GitHub Desktop.
Just some code I wrote when watching https://youtu.be/EGoRJePORHs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(clippy::collapsible_else_if, dead_code)] | |
// Just some code I wrote when watching https://youtu.be/EGoRJePORHs | |
fn main() { | |
let start = 1; | |
let mut current = int_to_vec_of_binary_digits(start); | |
for _ in 0..1_000 { | |
let mut zeroes = 0; | |
let mut ones = 0; | |
for bd in ¤t { | |
match bd { | |
BinaryDigit::Zero => zeroes += 1, | |
BinaryDigit::One => ones += 1, | |
} | |
} | |
println!( | |
"las: {}, length: {}, 0s: {}, 1s: {}, ratio 1/0: {}", | |
{ | |
if current.len() < 8 { | |
vec_of_digits_to_string(¤t) | |
} else { | |
"too long".to_string() | |
} | |
}, | |
current.len(), | |
zeroes, | |
ones, | |
((ones as f64) / (zeroes as f64)) | |
); | |
current = look_and_say_binary(¤t).unwrap(); | |
if current.len() > 500_000_000 { | |
println!("Over 0.5 GB! Stopping..."); | |
break; | |
} | |
} | |
} | |
#[allow(dead_code)] | |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] | |
#[repr(u8)] | |
enum DecimalDigit { | |
Zero, | |
One, | |
Two, | |
Three, | |
Four, | |
Five, | |
Six, | |
Seven, | |
Eight, | |
Nine, | |
} | |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] | |
#[repr(u8)] | |
enum BinaryDigit { | |
Zero, | |
One, | |
} | |
impl From<DecimalDigit> for char { | |
fn from(dd: DecimalDigit) -> Self { | |
match dd { | |
DecimalDigit::Zero => '0', | |
DecimalDigit::One => '1', | |
DecimalDigit::Two => '2', | |
DecimalDigit::Three => '3', | |
DecimalDigit::Four => '4', | |
DecimalDigit::Five => '5', | |
DecimalDigit::Six => '6', | |
DecimalDigit::Seven => '7', | |
DecimalDigit::Eight => '8', | |
DecimalDigit::Nine => '9', | |
} | |
} | |
} | |
impl From<BinaryDigit> for char { | |
fn from(bd: BinaryDigit) -> Self { | |
match bd { | |
BinaryDigit::Zero => '0', | |
BinaryDigit::One => '1', | |
} | |
} | |
} | |
impl TryFrom<u8> for DecimalDigit { | |
type Error = (); | |
fn try_from(value: u8) -> Result<Self, Self::Error> { | |
match value { | |
0 => Ok(DecimalDigit::Zero), | |
1 => Ok(DecimalDigit::One), | |
2 => Ok(DecimalDigit::Two), | |
3 => Ok(DecimalDigit::Three), | |
4 => Ok(DecimalDigit::Four), | |
5 => Ok(DecimalDigit::Five), | |
6 => Ok(DecimalDigit::Six), | |
7 => Ok(DecimalDigit::Seven), | |
8 => Ok(DecimalDigit::Eight), | |
9 => Ok(DecimalDigit::Nine), | |
_ => Err(()), | |
} | |
} | |
} | |
impl TryFrom<u8> for BinaryDigit { | |
type Error = (); | |
fn try_from(value: u8) -> Result<Self, Self::Error> { | |
match value { | |
0 => Ok(BinaryDigit::Zero), | |
1 => Ok(BinaryDigit::One), | |
_ => Err(()), | |
} | |
} | |
} | |
type DecimalNumber = Vec<DecimalDigit>; | |
type BinaryNumber = Vec<BinaryDigit>; | |
fn look_and_say_decimal(num: &DecimalNumber) -> Option<DecimalNumber> { | |
let mut output = DecimalNumber::new(); | |
let mut previous_digit: Option<DecimalDigit> = None; | |
let mut count = 0u8; | |
for digit in num { | |
match previous_digit { | |
Some(pd) => { | |
if pd == *digit { | |
count += 1; | |
} else { | |
output.push(count.try_into().ok()?); | |
output.push(pd); | |
count = 1; | |
previous_digit = Some(*digit); | |
} | |
} | |
None => { | |
count = 1; | |
previous_digit = Some(*digit); | |
} | |
} | |
} | |
output.push(count.try_into().ok()?); | |
output.push(previous_digit.unwrap()); | |
Some(output) | |
} | |
fn look_and_say_binary(num: &BinaryNumber) -> Option<BinaryNumber> { | |
let mut output = BinaryNumber::new(); | |
let mut previous_digit: Option<BinaryDigit> = None; | |
let mut count = 0u8; | |
for digit in num { | |
match previous_digit { | |
Some(pd) => { | |
if pd == *digit { | |
count += 1; | |
} else { | |
for bd in int_to_vec_of_binary_digits(count as u32) { | |
output.push(bd); | |
} | |
output.push(pd); | |
count = 1; | |
previous_digit = Some(*digit); | |
} | |
} | |
None => { | |
count = 1; | |
previous_digit = Some(*digit); | |
} | |
} | |
} | |
for bd in int_to_vec_of_binary_digits(count as u32) { | |
output.push(bd); | |
} | |
output.push(previous_digit.unwrap()); | |
Some(output) | |
} | |
fn int_to_vec_of_decimal_digits(i: u32) -> DecimalNumber { | |
let mut output = DecimalNumber::new(); | |
let string_repr = i.to_string(); | |
for c in string_repr.chars() { | |
let unsigned_eight: u8 = c.to_string().parse().unwrap(); | |
let dd: DecimalDigit = unsigned_eight.try_into().unwrap(); | |
output.push(dd); | |
} | |
output | |
} | |
fn vec_of_digits_to_string<T>(dm: &[T]) -> String | |
where | |
T: Into<char> + Copy, | |
{ | |
let mut output = String::with_capacity(dm.len()); | |
for dd in dm { | |
output.push(dd.to_owned().into()); | |
} | |
output | |
} | |
fn int_to_vec_of_binary_digits(i: u32) -> BinaryNumber { | |
let mut output = BinaryNumber::new(); | |
let string_repr = format!("{i:b}"); | |
for c in string_repr.chars() { | |
let unsigned_eight: u8 = c.to_string().parse().unwrap(); | |
let bd: BinaryDigit = unsigned_eight.try_into().unwrap(); | |
output.push(bd); | |
} | |
output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment