-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.rs
166 lines (133 loc) · 4.03 KB
/
count.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
use std::{path::Path, process::Command};
fn build(path: &str)
{
let p = format!("./{}/compile.sh", &path);
let p = std::path::Path::new(&p);
let root = p.parent().unwrap().parent().unwrap().to_str().unwrap();
Command::new(format!("./{}/compile.sh", root))
.arg(path)
.output()
.unwrap();
}
fn get_num(s: &str) -> Option<i32>
{
let l = s.find('(')? + 1;
let r = s.find(')')?;
s[l..r - 2].parse::<i32>().ok()
}
fn run(path: &str) -> (i32, i32)
{
let f = Command::new("./main").arg("input").current_dir(path).output().unwrap();
let out = std::str::from_utf8(&f.stdout).unwrap();
assert!(f.status.success());
out.split_once('\n')
.map(|(a, b)| (get_num(a).unwrap(), get_num(b).unwrap_or(-1)))
.unwrap()
}
fn clean(path: &str)
{
let f = Command::new("rm").arg("main").current_dir(path).output().unwrap();
assert!(f.status.success());
}
fn build_and_run(path: &Path) -> (i32, i32)
{
let path = path.as_os_str().to_str().unwrap();
build(path);
let res = run(path);
clean(path);
res
}
fn main()
{
let folder = std::env::args().nth(1).expect("Usage: ./count <year path>");
let number_of_runs: i32 = std::env::args().nth(2).unwrap_or("1".into()).parse().unwrap();
let mut silver = Vec::new();
let mut gold = Vec::new();
let mut not_done = Vec::new();
for day in 1..=25
{
let day_text = if day < 10 { format!("0{}", day) } else { format!("{}", day) };
let folder = format!("{}/day_{}", folder, day_text);
let path = Path::new(&folder);
if path.exists()
{
let mut part_one = Vec::new();
let mut part_two = Vec::new();
for _ in 0..number_of_runs
{
let (s, g) = build_and_run(path);
part_one.push(s);
if g != -1
{
part_two.push((day, g));
}
}
// Calculate the average result
let len = part_one.len() as i32;
let total: i32 = part_one.into_iter().sum();
let s = total / len;
let g =
if part_two.len() == 0 { -1 }
else
{
let len = part_two.len() as i32;
let total: i32 = part_two.into_iter().map(|(_, time)| time).sum();
total / len
};
silver.push((day, s));
if g != -1
{
gold.push((day, g));
}
}
else
{
not_done.push(day);
}
}
if !not_done.is_empty()
{
let mut s = String::new();
let mut first = true;
for day in not_done
{
if !first
{
s.push_str(", ");
}
let s2 = format!("\x1b[0;33;31m{}\x1b[0m", day);
s.push_str(s2.as_str());
first = false;
}
println!("Days not completed: {}", s);
}
println!("STATS:");
println!("Number of runs: {number_of_runs}:\n");
print_info(Task::Silver, &silver);
print_info(Task::Gold, &gold);
let total = gold.iter().chain(silver.iter()).map(|(_, time)| time).sum::<i32>();
println!("\nTOTAL TIME: {}ms", total);
}
fn print_info(task: Task, vec: &[(i32, i32)])
{
match task
{
Task::Silver => println!("\x1b[0;34;34m{}\x1b[0m:", "Silver"),
Task::Gold => println!("\x1b[0;33;10m{}\x1b[0m:", "Gold"),
};
let mut _vec: Vec<_> = vec.iter().map(|(_, time)| *time).collect();
_vec.sort();
let median = _vec[_vec.len() / 2];
let total = vec.iter().map(|(_, time)| time).sum::<i32>();
let avg = total / vec.len() as i32;
let (day, time) = vec.iter().max_by_key(|k| k.1).unwrap();
println!("\t Total time:\t{}ms", total);
println!("\t Average time:\t{}ms", avg);
println!("\t Median time:\t{}ms", median);
println!("\t Highest time:\t{}ms, day: {}", time, day);
}
enum Task
{
Silver,
Gold,
}