Wrote program for Day 45

This commit is contained in:
Mariano Riefolo 2024-09-08 11:26:30 +02:00
parent 617d141847
commit 00146175f8
5 changed files with 72 additions and 1 deletions

View File

@ -91,7 +91,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_check_mark: | | Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_check_mark: |
| Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_check_mark: | | Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_check_mark: |
| Day #44 | [Maximum Edge Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-44_Maximum-Edge-Of-A-Triangle) | :white_check_mark: | | Day #44 | [Maximum Edge Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-44_Maximum-Edge-Of-A-Triangle) | :white_check_mark: |
| Day #45 | [Subtract The Swapped Bits...](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-45_Subtract-The-Swapped-Bits-Without-Temp-Storage) | :white_large_square: | | Day #45 | [Subtract The Swapped Bits...](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-45_Subtract-The-Swapped-Bits-Without-Temp-Storage) | :white_check_mark: |
| Day #46 | [Hot Pics Of Danny Devito](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-46_Hot-Pics-Of-Danny-Devito) | :white_large_square: | | Day #46 | [Hot Pics Of Danny Devito](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-46_Hot-Pics-Of-Danny-Devito) | :white_large_square: |
| Day #47 | [Zip It](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-47_Zip-It) | :white_large_square: | | Day #47 | [Zip It](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-47_Zip-It) | :white_large_square: |
| Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_large_square: | | Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_large_square: |

View File

@ -0,0 +1,6 @@
[package]
name = "day45"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,6 @@
pub fn xor(x: &mut usize, y: &mut usize) -> isize {
*x ^= *y;
*y ^= *x;
*x ^= *y;
*x as isize - *y as isize
}

View File

@ -0,0 +1,35 @@
use std::{
io::{self, Write},
process::exit,
};
use day45::xor;
fn read_usize(request: &str) -> Result<usize, std::num::ParseIntError> {
print!("{}", request);
io::stdout().flush().expect("Failed to flush stdout");
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.expect("Failed to read from stdin");
input.trim().parse()
}
fn main() {
let mut x = match read_usize("Enter the first number: ") {
Ok(x) => x,
Err(_) => {
println!("Invalid input, please enter a valid number");
exit(1)
}
};
let mut y = match read_usize("Enter the second number: ") {
Ok(y) => y,
Err(_) => {
println!("Invalid input, please enter a valid number");
exit(1)
}
};
let result = xor(&mut x, &mut y);
println!("{x} - {y} = {result}");
}

View File

@ -0,0 +1,24 @@
#[cfg(test)]
mod examples {
use day45::xor;
#[test]
fn test_example() {
assert_eq!(xor(&mut 10, &mut 41), 31);
}
#[test]
fn test_example2() {
assert_eq!(xor(&mut 69, &mut 420), 351);
}
#[test]
fn test_example3() {
assert_eq!(xor(&mut 12345, &mut 890412), 878067);
}
#[test]
fn test_example4() {
assert_eq!(xor(&mut 2, &mut 1), -1);
}
}