Muh code games
This commit is contained in:
parent
4985114cb7
commit
5355d14e3b
8
fibonacci/Cargo.toml
Executable file
8
fibonacci/Cargo.toml
Executable file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
37
fibonacci/src/main.rs
Executable file
37
fibonacci/src/main.rs
Executable file
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
let fib_instance = FibItr::default();
|
||||
println!("{}",
|
||||
fib_instance
|
||||
.filter(is_even)
|
||||
.take_while(|x| *x < 4000000)
|
||||
.sum::<usize>()
|
||||
);
|
||||
}
|
||||
|
||||
fn is_even(x: &usize) -> bool {
|
||||
x % 2 == 0
|
||||
}
|
||||
|
||||
struct FibItr {
|
||||
index: usize,
|
||||
prev: usize
|
||||
}
|
||||
|
||||
impl Iterator for FibItr {
|
||||
type Item = usize;
|
||||
fn next ( &mut self ) -> Option<usize> {
|
||||
let result = self.index + self.prev;
|
||||
self.prev = self.index;
|
||||
self.index = result;
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FibItr {
|
||||
fn default() -> Self {
|
||||
FibItr {
|
||||
index: 1,
|
||||
prev: 1
|
||||
}
|
||||
}
|
||||
}
|
8
guess/Cargo.toml
Executable file
8
guess/Cargo.toml
Executable file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "hello"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
19
guess/src/main.rs
Executable file
19
guess/src/main.rs
Executable file
|
@ -0,0 +1,19 @@
|
|||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let mut theguess_tm = String::new();
|
||||
|
||||
println!("Hello noob");
|
||||
print!("Guess a number or something lol: ");
|
||||
|
||||
io::stdout()
|
||||
.flush()
|
||||
.expect("couldn't flush ?");
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut theguess_tm)
|
||||
.expect("Stinky computer lol ur mom");
|
||||
|
||||
println!("ur guess was: {}", theguess_tm);
|
||||
}
|
8
line_racing/Cargo.toml
Normal file
8
line_racing/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "line_racing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
33
line_racing/src/main.rs
Normal file
33
line_racing/src/main.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use std::io;
|
||||
|
||||
macro_rules! parse_input {
|
||||
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
loop {
|
||||
let mut input_line = String::new();
|
||||
|
||||
io::stdin().read_line(&mut input_line).unwrap();
|
||||
|
||||
let inputs = input_line.split(" ").collect::<Vec<_>>();
|
||||
|
||||
let n = parse_input!(inputs[0], usize); // total number of players (2 to 4).
|
||||
let p = parse_input!(inputs[1], usize); // your player number (0 to 3).
|
||||
|
||||
for i in 0..n as usize {
|
||||
let mut input_line = String::new();
|
||||
io::stdin().read_line(&mut input_line).unwrap();
|
||||
let inputs = input_line.split(" ").collect::<Vec<_>>();
|
||||
let x0 = parse_input!(inputs[0], usize); // starting X coordinate of lightcycle (or -1)
|
||||
let y0 = parse_input!(inputs[1], usize); // starting Y coordinate of lightcycle (or -1)
|
||||
let x1 = parse_input!(inputs[2], usize); // starting X coordinate of lightcycle (can be the same as X0 if you play before this player)
|
||||
let y1 = parse_input!(inputs[3], usize); // starting Y coordinate of lightcycle (can be the same as Y0 if you play before this player)
|
||||
}
|
||||
|
||||
// Write an action using println!("message...");
|
||||
// To debug: eprintln!("Debug message...");
|
||||
|
||||
println!("LEFT"); // A single line with UP, DOWN, LEFT or RIGHT
|
||||
}
|
||||
}
|
8
multiples/Cargo.toml
Executable file
8
multiples/Cargo.toml
Executable file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "multiples"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
15
multiples/src/main.rs
Executable file
15
multiples/src/main.rs
Executable file
|
@ -0,0 +1,15 @@
|
|||
fn main() {
|
||||
let mut piss_list: Vec<usize> = Vec::new();
|
||||
let numbers = 1..1000;
|
||||
|
||||
for x in numbers {
|
||||
if x % 3 == 0 {
|
||||
piss_list.push(x);
|
||||
}
|
||||
else if x % 5 == 0 {
|
||||
piss_list.push(x);
|
||||
}
|
||||
}
|
||||
let total: usize = piss_list.into_iter().sum();
|
||||
println!("ur sum esssssssssssssssssssssss uhhh: {} ", total);
|
||||
}
|
8
palindrome/Cargo.toml
Executable file
8
palindrome/Cargo.toml
Executable file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "palindrome"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
157
palindrome/src/main.rs
Executable file
157
palindrome/src/main.rs
Executable file
|
@ -0,0 +1,157 @@
|
|||
use std::iter::Peekable;
|
||||
|
||||
const ERR_TXT: &str = "unexpected item in bagging area";
|
||||
|
||||
fn main() {
|
||||
// let mut something: Option<(usize,usize)> = None;
|
||||
// for x in (100..999).rev() {
|
||||
// for y in (100..999).rev() {
|
||||
// if reverse( x * y ) == x * y {
|
||||
// something = Some(( x, y ));
|
||||
// break
|
||||
// }
|
||||
// else {
|
||||
// something = None
|
||||
// }
|
||||
// }
|
||||
// if !something.is_none() {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// println!("{} * {} = {}", something.expect("").0, something.expect("").1, something.expect("").0 * something.expect("").1)
|
||||
|
||||
// let palindrome = (0..999*999).rev()
|
||||
// .filter( |x| *x == reverse(*x))
|
||||
// .find( |x| palin_valid(*x) )
|
||||
// .expect(ERR_TXT);
|
||||
// println!("{}", palindrome);
|
||||
|
||||
let palindrome = (0..999*999).rev()
|
||||
.filter( |x| *x == reverse(*x))
|
||||
.filter( |x| palin_valid(*x, false))
|
||||
.take(100);
|
||||
for x in palindrome {
|
||||
println!("{}, {}", x, palin_valid(x, true));
|
||||
}
|
||||
|
||||
// println!("{}", palin_valid(906609, true))
|
||||
}
|
||||
|
||||
fn palin_valid(palin: usize, debug: bool) -> bool {
|
||||
let mut x: usize = 1;
|
||||
let mut y: usize = 1;
|
||||
let mut x_check = true;
|
||||
let factors = PrimeFactItr::factors(palin).collect::<Vec<_>>().into_iter().rev();
|
||||
for factor in factors {
|
||||
if debug { println!("x : {}, y : {}, factor : {}", x, y, factor); }
|
||||
if x_check {
|
||||
if x * factor <= 999 {
|
||||
x *= factor;
|
||||
}
|
||||
else if y * factor <= 999 {
|
||||
y *= factor;
|
||||
}
|
||||
else {
|
||||
if debug { println!("x* : {}, y* : {}, factor : {}", x*factor, y*factor, factor); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if y * factor <= 999 {
|
||||
y *= factor;
|
||||
}
|
||||
else if x * factor <= 999 {
|
||||
x *= factor;
|
||||
}
|
||||
else {
|
||||
if debug { println!("x* : {}, y* : {}, factor : {}", x * factor, y * factor, factor); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
x_check = !x_check;
|
||||
}
|
||||
if debug { println!("x : {}, y : {}", x, y); }
|
||||
return true;
|
||||
}
|
||||
|
||||
fn reverse(mut n:usize) -> usize {
|
||||
let radix = 10;
|
||||
let mut reversed = 0;
|
||||
while n != 0 {
|
||||
reversed = reversed * radix + n % radix;
|
||||
n /= radix;
|
||||
}
|
||||
reversed
|
||||
}
|
||||
|
||||
//prime iterator
|
||||
|
||||
struct PrimeItr {
|
||||
record: Vec<usize>,
|
||||
index: usize
|
||||
}
|
||||
|
||||
impl Iterator for PrimeItr {
|
||||
type Item = usize;
|
||||
fn next ( &mut self ) -> Option<usize> {
|
||||
let is_prime = !self.record.iter()
|
||||
.take_while( |x| **x * **x < self.index )
|
||||
.any( |x| self.index % *x == 0 );
|
||||
let result = self.index;
|
||||
self.index += if self.index == 2 { 1 } else { 2 };
|
||||
if is_prime {
|
||||
self.record.push(result);
|
||||
Some(result)
|
||||
}
|
||||
else {
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PrimeItr {
|
||||
fn default() -> Self {
|
||||
PrimeItr {
|
||||
record: vec![],
|
||||
index: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// prime factorization iterator
|
||||
|
||||
struct PrimeFactItr {
|
||||
record: Vec<usize>,
|
||||
chuck: usize,
|
||||
prime: Peekable<PrimeItr>
|
||||
}
|
||||
|
||||
impl Iterator for PrimeFactItr {
|
||||
type Item = usize;
|
||||
fn next ( &mut self ) -> Option<usize> {
|
||||
let x = *self.prime.peek().expect(ERR_TXT);
|
||||
let is_factor = self.chuck % x == 0;
|
||||
if is_factor {
|
||||
self.record.push(x);
|
||||
self.chuck /= x;
|
||||
Some(x)
|
||||
}
|
||||
else if x > self.chuck {
|
||||
None
|
||||
}
|
||||
else {
|
||||
self.prime.next();
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimeFactItr {
|
||||
fn factors(x: usize) -> Self {
|
||||
PrimeFactItr {
|
||||
record: vec![],
|
||||
chuck: x,
|
||||
prime: PrimeItr::default().peekable()
|
||||
}
|
||||
}
|
||||
}
|
15
primes.py
Executable file
15
primes.py
Executable file
|
@ -0,0 +1,15 @@
|
|||
primes = [2]
|
||||
counter = 1
|
||||
while counter <= 2000000:
|
||||
counter += 2
|
||||
for x in primes:
|
||||
if x * x > counter:
|
||||
# print(str(counter) + " found")
|
||||
primes += [counter]
|
||||
break
|
||||
elif counter % x == 0:
|
||||
break
|
||||
counter = 0
|
||||
for x in primes:
|
||||
counter += x
|
||||
print(counter)
|
8
primes/Cargo.toml
Executable file
8
primes/Cargo.toml
Executable file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "primes"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
94
primes/src/main.rs
Executable file
94
primes/src/main.rs
Executable file
|
@ -0,0 +1,94 @@
|
|||
use std::iter::Peekable;
|
||||
|
||||
fn main() {
|
||||
//
|
||||
// println!("sum of primes less than 2,000,000 is : {}",
|
||||
// PrimeItr::default()
|
||||
// .take_while( |x| x < 2_000_000 )
|
||||
// .sum::<usize>())
|
||||
//
|
||||
// println!("largest prime factor is : {}", PrimeItr::default()
|
||||
// .take_while( |x| *x < 600851475143/2 )
|
||||
// .filter( |x| x % 600851475143 != 0 )
|
||||
// .last()
|
||||
// .expect(PRIME_ERR)
|
||||
// )
|
||||
//
|
||||
for x in PrimeFactItr::factors(600851475143) {
|
||||
println!("{}", x)
|
||||
}
|
||||
}
|
||||
|
||||
const PRIME_ERR: &str = "unexpected item in bagging area";
|
||||
|
||||
//prime iterator
|
||||
|
||||
struct PrimeItr {
|
||||
record: Vec<usize>,
|
||||
index: usize
|
||||
}
|
||||
|
||||
impl Iterator for PrimeItr {
|
||||
type Item = usize;
|
||||
fn next ( &mut self ) -> Option<usize> {
|
||||
let is_prime = !self.record.iter()
|
||||
.take_while( |x| **x * **x < self.index )
|
||||
.any( |x| self.index % *x == 0 );
|
||||
let result = self.index;
|
||||
self.index += if self.index == 2 { 1 } else { 2 };
|
||||
if is_prime {
|
||||
self.record.push(result);
|
||||
Some(result)
|
||||
}
|
||||
else {
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PrimeItr {
|
||||
fn default() -> Self {
|
||||
PrimeItr {
|
||||
record: vec![],
|
||||
index: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// prime factorization iterator
|
||||
|
||||
struct PrimeFactItr {
|
||||
record: Vec<usize>,
|
||||
chuck: usize,
|
||||
prime: Peekable<PrimeItr>
|
||||
}
|
||||
|
||||
impl Iterator for PrimeFactItr {
|
||||
type Item = usize;
|
||||
fn next ( &mut self ) -> Option<usize> {
|
||||
let x = *self.prime.peek().expect(PRIME_ERR);
|
||||
let is_factor = self.chuck % x == 0;
|
||||
if is_factor {
|
||||
self.record.push(x);
|
||||
self.chuck /= x;
|
||||
Some(x)
|
||||
}
|
||||
else if x > self.chuck {
|
||||
None
|
||||
}
|
||||
else {
|
||||
self.prime.next();
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimeFactItr {
|
||||
fn factors(x: usize) -> Self {
|
||||
PrimeFactItr {
|
||||
record: vec![],
|
||||
chuck: x,
|
||||
prime: PrimeItr::default().peekable()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue