Wrote program for Day 26

This commit is contained in:
Mariano Riefolo 2024-08-20 11:47:02 +02:00
parent 9595929b38
commit b9b2d1d67c
5 changed files with 63 additions and 1 deletions

View File

@ -72,7 +72,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: | | Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: |
| Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: | | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: |
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: | | Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: |
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_large_square: | | Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: |
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: | | Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: |
| Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: |
| Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: | | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: |

View File

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

View File

@ -0,0 +1,13 @@
pub fn min_turns(current_lock: &str, target_lock: &str) -> u8 {
let mut result = 0;
for (i, c) in current_lock.bytes().enumerate() {
let tl = target_lock.as_bytes()[i];
result += std::cmp::min(
(10 + (c - b'0') - (tl - b'0')) % 10,
(10 + (tl - b'0') - (c - b'0')) % 10,
);
}
result
}

View File

@ -0,0 +1,24 @@
use std::io::{self, Write};
use day26::min_turns;
fn read_lock(request: &str) -> String {
let mut buffer = String::new();
print!("{}", request);
io::stdout().flush().expect("Failed to flush");
io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
buffer.trim().to_owned()
}
fn main() {
let current_lock = read_lock("Insert the current lock: ");
let target_lock = read_lock("Insert the target lock: ");
let result = min_turns(&current_lock, &target_lock);
println!("The minimum number of turns required is {}", result);
}

View File

@ -0,0 +1,19 @@
#[cfg(test)]
mod examples {
use day26::min_turns;
#[test]
fn example1() {
assert_eq!(min_turns("4089", "5672"), 9);
}
#[test]
fn example2() {
assert_eq!(min_turns("1111", "1100"), 2);
}
#[test]
fn example3() {
assert_eq!(min_turns("2391", "4984"), 10);
}
}