initial commit
This commit is contained in:
commit
26850c839c
4 changed files with 63 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pathshortener"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "pathshortener"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
49
src/main.rs
Normal file
49
src/main.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
fn main() {
|
||||||
|
let directions = "ENWESWNS".to_string();
|
||||||
|
let res = shorten(directions);
|
||||||
|
println!("{:?}", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shorten(directions: String) -> String {
|
||||||
|
let characters: Vec<_> = directions.chars().collect();
|
||||||
|
let mut result = remove_doubles(&characters);
|
||||||
|
loop {
|
||||||
|
let out = remove_doubles(&result);
|
||||||
|
if out.len() == result.len() {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result = out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.iter().map(|f| f.to_string()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_doubles(characters: &Vec<char>) -> Vec<char> {
|
||||||
|
let mut shortenable_map = vec![false; characters.len()];
|
||||||
|
let windows = characters.windows(2);
|
||||||
|
|
||||||
|
for (idx, window) in windows.enumerate() {
|
||||||
|
let shortenable = is_shortenable(window);
|
||||||
|
if shortenable {
|
||||||
|
shortenable_map[idx] = true;
|
||||||
|
shortenable_map[idx + 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
characters
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(idx, _)| !shortenable_map[*idx])
|
||||||
|
.map(|(_, itm)| *itm)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_shortenable(chars: &[char]) -> bool {
|
||||||
|
match chars {
|
||||||
|
&['N', 'S'] => true,
|
||||||
|
&['S', 'N'] => true,
|
||||||
|
&['W', 'E'] => true,
|
||||||
|
&['E', 'W'] => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue