1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::fs::File;
use std::io::{self, Error, Read, Write};
use std::path::Path;
pub type ReadResult = Result<Vec<u8>, Error>;
pub type WriteResult = Result<(), Error>;
fn atoi(chars: &[u8]) -> u64 {
chars.iter()
.take_while(|&n| 48 <= *n && *n <= 57)
.fold(0, |acc, &n| (acc * 10) + (n - 48) as u64)
}
pub fn split_into_numbers(chars: Vec<u8>) -> Vec<u64> {
chars.split(|&c| c == 32)
.filter(|s| !s.is_empty())
.map(atoi)
.collect::<Vec<u64>>()
}
fn read<T: Read>(mut reader: T) -> ReadResult {
let mut res = Vec::new();
match reader.read_to_end(&mut res) {
Ok(_) => Ok(res),
Err(e) => Err(e),
}
}
pub fn read_from<P>(file: Option<P>) -> ReadResult
where P: AsRef<Path>
{
match file {
Some(file) => File::open(file).and_then(read),
None => read(io::stdin()),
}
}
fn write<T: Write>(mut write: T, content: String) -> WriteResult {
write.write_all(content.as_bytes())
}
pub fn write_to<P>(file: Option<P>, content: String) -> WriteResult
where P: AsRef<Path>
{
match file {
Some(file) => File::create(file).and_then(|f| write(f, content)),
None => write(io::stdout(), content),
}
}
#[cfg(test)]
mod tests {
use super::split_into_numbers;
fn chars(numbers: &str) -> Vec<u8> {
numbers.chars()
.map(|c| c as u8)
.collect::<Vec<u8>>()
}
#[test]
fn test_starting_zero() {
let bytes = chars("01 002 0030");
assert_eq!(split_into_numbers(bytes), vec![1, 2, 30]);
}
#[test]
fn test_middle_letter() {
let bytes = chars("435a 33ss56 aa");
assert_eq!(split_into_numbers(bytes), vec![435, 33, 0]);
}
#[test]
fn test_multiple_spaces() {
let bytes = chars("45 545 76");
assert_eq!(split_into_numbers(bytes), vec![45, 545, 76]);
}
}