initial commit

This commit is contained in:
0xffff00 2026-02-13 22:05:19 +01:00
commit 26850c839c
Signed by: Oxfo
GPG key ID: 1AAE7E25860BF1CC
4 changed files with 63 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View 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
View file

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

49
src/main.rs Normal file
View 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,
}
}