-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy paththe-cash-register.rs
More file actions
68 lines (62 loc) · 1.86 KB
/
the-cash-register.rs
File metadata and controls
68 lines (62 loc) · 1.86 KB
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
use std::io;
macro_rules! parse_input {
($t:ident) => {{
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').parse::<$t>().unwrap()
}}
}
fn perform(register: &[i32], goal: i32) -> Result<Vec<i32>, &'static str> {
if goal == 0 {
return Ok(vec![0]);
}
if register.contains(&goal) {
return Ok(vec![goal]);
}
let mut open: Vec<Vec<i32>> = register.iter().map(|&coin| vec![coin]).collect();
let mut amounts: Vec<i32> = Vec::new();
loop {
let mut cur: Vec<Vec<i32>> = Vec::new();
for poss in open {
for &coin in register {
let mut new_poss = poss.clone();
new_poss.push(coin);
let rendu =new_poss.iter().sum();
if rendu == goal {
return Ok(new_poss);
}
if rendu < goal && !amounts.contains(&rendu) {
cur.push(new_poss);
amounts.push(rendu);
}
}
}
if cur.is_empty() {
return Err("IMPOSSIBLE");
}
open = cur;
}
}
fn main() {
let register: Vec<i32> = parse_input!(String)
.trim()
.split(' ')
.map(|s| s.parse().unwrap_or(0))
.collect();
let goal_amount: i32 = parse_input!(i32);
match perform(®ister, goal_amount) {
Ok(res) => {
println!("{}",
if let Some((last, elements)) = res.split_last() {
std::iter::once(last)
.chain(elements.iter().rev())
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(" ")
} else {
"".to_string()
});
}
Err(e) => println!("{}", e),
}
}