-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad3_1.rs
More file actions
executable file
·57 lines (54 loc) · 2.67 KB
/
ad3_1.rs
File metadata and controls
executable file
·57 lines (54 loc) · 2.67 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
use std::fs;
fn find_battery(battery_vec: Vec<i32>) -> i32 {
let mut prev = battery_vec[0];
let mut max_digit_first = battery_vec[0];
let mut max_digit_second = battery_vec[1];
for (ind, &digit) in battery_vec.iter().enumerate().skip(1) {
// println!("{digit}, {ind}");
if digit > max_digit_first {
prev = max_digit_first;
max_digit_first = digit;
if let Some(&value) = battery_vec.get(ind + 1) {
max_digit_second = value;
} else {
// println!("HERE!{}{}", prev, max_digit_first);
return format!("{}{}", prev, max_digit_first).parse().unwrap();
}
} else if digit > max_digit_second {
max_digit_second = digit;
}
}
// println!("{}{}", max_digit_first, max_digit_second);
return format!("{}{}", max_digit_first, max_digit_second).parse().unwrap();
}
fn get_all_batteries(lines: Vec<&str>) -> i32 {
let mut answer = 0;
for line in lines {
answer += find_battery(line.chars().map(|c| c.to_digit(10).unwrap() as i32).collect());
}
return answer;
}
fn main() {
let file_path_test = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task3_test.txt";
let file_path_task = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task3.txt";
let task_test_str = fs::read_to_string(file_path_test)
.expect("Read the file error?");
let task_test: Vec<&str> = task_test_str.lines().collect();
println!("Read the file {:?}", task_test);
let task_task_str = fs::read_to_string(file_path_task)
.expect("Read the file error?");
let task_task: Vec<&str> = task_task_str.lines().collect();
// println!("In the file:\n{task}");
let test1: Vec<i32> = "0123456789".chars().map(|c| c.to_digit(10).unwrap() as i32).collect();
println!("Test 1 {:?}", test1);
println!("The answer is: {}", find_battery(test1));
// let test2: Vec<&str> = "12121212-12121223".split(',').collect();
// let test3: Vec<&str> = "L50".lines().collect();
// let test4: Vec<&str> = "L150".lines().collect();
// println!("Test 1 {:?} : {}", test1, check_id_lists(test1.clone()));
// println!("Test 2 {:?} The answer is: {}",test2, check_id_lists(test2.clone()));
// println!("Test 3 {:?} The answer is: {}", test3, calculate_zeros(test3.clone(), 50, 100));
// println!("Test 4 Test 4 {:?} The answer is: {}", test4, calculate_zeros(test4.clone(), 50, 100));
println!("The answer is: {}", get_all_batteries(task_test));
println!("The answer is: {}", get_all_batteries(task_task));
}