158 Zeilen
3,8 KiB
Rust
158 Zeilen
3,8 KiB
Rust
const MAXMUL: u32 = 100000;
|
|
|
|
pub fn splitspace(input: &str) -> (&str, &str) {
|
|
let mut output = input.split_ascii_whitespace();
|
|
(output.next().unwrap(), output.next().unwrap())
|
|
}
|
|
|
|
pub fn parsenumber(input: &str) -> u32 {
|
|
let mut output = 0;
|
|
let mut mul = 1;
|
|
for c in input.chars().rev() {
|
|
let i: u32 = match c {
|
|
'1' => 1,
|
|
'2' => 2,
|
|
'3' => 3,
|
|
'4' => 4,
|
|
'5' => 5,
|
|
'6' => 6,
|
|
'7' => 7,
|
|
'8' => 8,
|
|
'9' => 9,
|
|
'0' => 0,
|
|
_ => 0,
|
|
};
|
|
if mul > MAXMUL {
|
|
return output;
|
|
}
|
|
output += i * mul;
|
|
mul *= 10;
|
|
}
|
|
output
|
|
}
|
|
|
|
pub fn get_numbers(input: &str) -> Vec<u32> {
|
|
let mut numbers = Vec::new();
|
|
for char in input.chars() {
|
|
match char {
|
|
'1' => numbers.push(1),
|
|
'2' => numbers.push(2),
|
|
'3' => numbers.push(3),
|
|
'4' => numbers.push(4),
|
|
'5' => numbers.push(5),
|
|
'6' => numbers.push(6),
|
|
'7' => numbers.push(7),
|
|
'8' => numbers.push(8),
|
|
'9' => numbers.push(9),
|
|
'0' => numbers.push(0),
|
|
_ => {}
|
|
}
|
|
}
|
|
numbers
|
|
}
|
|
|
|
pub fn get_string_numbers(input: &str) -> Vec<u32> {
|
|
let len = input.len();
|
|
let mut output = Vec::new();
|
|
for i in 0..len {
|
|
match input.get(i..i + 1).unwrap() {
|
|
"1" => {
|
|
output.push(1);
|
|
continue;
|
|
}
|
|
"2" => {
|
|
output.push(2);
|
|
continue;
|
|
}
|
|
"3" => {
|
|
output.push(3);
|
|
continue;
|
|
}
|
|
"4" => {
|
|
output.push(4);
|
|
continue;
|
|
}
|
|
"5" => {
|
|
output.push(5);
|
|
continue;
|
|
}
|
|
"6" => {
|
|
output.push(6);
|
|
continue;
|
|
}
|
|
"7" => {
|
|
output.push(7);
|
|
continue;
|
|
}
|
|
"8" => {
|
|
output.push(8);
|
|
continue;
|
|
}
|
|
"9" => {
|
|
output.push(9);
|
|
continue;
|
|
}
|
|
"0" => {
|
|
output.push(0);
|
|
continue;
|
|
}
|
|
_ => {}
|
|
}
|
|
if (i + 3) <= len {
|
|
match input.get(i..i + 3).unwrap() {
|
|
"one" => {
|
|
output.push(1);
|
|
continue;
|
|
}
|
|
"two" => {
|
|
output.push(2);
|
|
continue;
|
|
}
|
|
"six" => {
|
|
output.push(6);
|
|
continue;
|
|
}
|
|
_ => {}
|
|
};
|
|
}
|
|
if (i + 4) <= len {
|
|
match input.get(i..i + 4).unwrap() {
|
|
"four" => {
|
|
output.push(4);
|
|
continue;
|
|
}
|
|
"five" => {
|
|
output.push(5);
|
|
continue;
|
|
}
|
|
"nine" => {
|
|
output.push(9);
|
|
continue;
|
|
}
|
|
"zero" => {
|
|
output.push(0);
|
|
continue;
|
|
}
|
|
_ => {}
|
|
};
|
|
}
|
|
if (i + 5) <= len {
|
|
match input.get(i..i + 5).unwrap() {
|
|
"three" => {
|
|
output.push(3);
|
|
continue;
|
|
}
|
|
"seven" => {
|
|
output.push(7);
|
|
continue;
|
|
}
|
|
"eight" => {
|
|
output.push(8);
|
|
continue;
|
|
}
|
|
_ => {}
|
|
};
|
|
}
|
|
}
|
|
output
|
|
}
|