-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
170 lines (158 loc) · 4.91 KB
/
main.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Keylogger
// https://open.kattis.com/problems/keylogger
use std::io::stdin;
fn main() {
let mut key_pressed: String = String::new();
stdin().read_line(&mut key_pressed).unwrap();
let mut input: Vec<String> = Vec::new();
for _ in 0..key_pressed.trim().parse().unwrap() {
let mut temp: String = String::new();
stdin().read_line(&mut temp).unwrap();
input.push(temp);
}
println!("{}", calc_key_presses(&input));
}
fn calc_key_presses(input: &[String]) -> String {
let mut output: String = String::new();
let mut is_caps_lock: bool = false;
let mut shift_pressed: bool = false;
for key in input.iter().map(|s| s.trim()).collect::<Vec<&str>>() {
let mut letter: char = match key.trim().as_bytes() {
b"clank" => 'a',
b"bong" => 'b',
b"click" => 'c',
b"tap" => 'd',
b"poing" => 'e',
b"clonk" => 'f',
b"clack" => 'g',
b"ping" => 'h',
b"tip" => 'i',
b"cloing" => 'j',
b"tic" => 'k',
b"cling" => 'l',
b"bing" => 'm',
b"pong" => 'n',
b"clang" => 'o',
b"pang" => 'p',
b"clong" => 'q',
b"tac" => 'r',
b"boing" => 's',
b"boink" => 't',
b"cloink" => 'u',
b"rattle" => 'v',
b"clock" => 'w',
b"toc" => 'x',
b"clink" => 'y',
b"tuc" => 'z',
b"whack" => ' ',
// Special key presses
b"pop" => {
output.pop();
continue;
}
b"bump" => {
if is_caps_lock {
is_caps_lock = false;
continue;
}
is_caps_lock = true;
continue;
}
b"dink" => {
shift_pressed = true;
continue;
}
b"thumb" => {
shift_pressed = false;
continue;
}
_ => {
println!("Unexpected input: {key}");
panic!();
}
};
if is_caps_lock && !shift_pressed || !is_caps_lock && shift_pressed {
letter = letter.to_ascii_uppercase();
}
output.push(letter);
}
output
}
#[cfg(test)]
mod samples {
use crate::calc_key_presses;
#[test]
fn sample_one() {
let input: Vec<String> = vec![
String::from("clank\n"),
String::from("bong\n"),
String::from("click\n"),
String::from("tap\n"),
String::from("poing\n"),
String::from("clonk\n"),
String::from("clack\n"),
String::from("ping\n"),
String::from("tip\n"),
String::from("cloing\n"),
String::from("tic\n"),
String::from("cling\n"),
String::from("bing\n"),
String::from("pong\n"),
String::from("clang\n"),
String::from("pang\n"),
String::from("clong\n"),
String::from("tac\n"),
String::from("boing\n"),
String::from("boink\n"),
String::from("cloink\n"),
String::from("rattle\n"),
String::from("clock\n"),
String::from("toc\n"),
String::from("clink\n"),
String::from("tuc\n"),
];
let expected: String = String::from("abcdefghijklmnopqrstuvwxyz");
let actual = calc_key_presses(&input);
assert_eq!(actual, expected);
}
#[test]
fn sample_two() {
let input: Vec<String> = vec![
String::from("bump\n"),
String::from("tip\n"),
String::from("whack\n"),
String::from("bump\n"),
String::from("clock\n"),
String::from("clank\n"),
String::from("pong\n"),
String::from("boink\n"),
String::from("whack\n"),
String::from("pang\n"),
String::from("tip\n"),
String::from("tuc\n"),
String::from("tuc\n"),
String::from("clank\n"),
];
let expected: String = String::from("I want pizza");
let actual = calc_key_presses(&input);
assert_eq!(actual, expected);
}
#[test]
fn sample_three() {
let input: Vec<String> = vec![
String::from("dink\n"),
String::from("pong\n"),
String::from("clang\n"),
String::from("whack\n"),
String::from("bump\n"),
String::from("click\n"),
String::from("thumb\n"),
String::from("clank\n"),
String::from("pang\n"),
String::from("boing\n"),
];
let expected: String = String::from("NO cAPS");
let actual = calc_key_presses(&input);
assert_eq!(actual, expected);
}
}