-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad3_2.rs
More file actions
executable file
·72 lines (64 loc) · 2.98 KB
/
ad3_2.rs
File metadata and controls
executable file
·72 lines (64 loc) · 2.98 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
69
70
71
72
use std::fs;
fn find_battery(battery_vec: Vec<i32>,to_right: i32) -> (i32, Vec<i32>) {
let mut prev = battery_vec[0];
let mut prev_ind = 0;
let mut max_digit_first_ind = 0;
let mut max_digit_second_ind = 1;
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;
prev_ind = max_digit_first_ind;
max_digit_first = digit;
max_digit_first_ind = ind;
if battery_vec[ind..].len() <= to_right as usize {
return (prev, battery_vec[prev_ind + 1..].to_vec());
}}
}
// println!("{}{}", max_digit_first, max_digit_second);
return (max_digit_first, battery_vec[max_digit_first_ind + 1..].to_vec());
}
fn get_all_batteries(lines: Vec<&str>) -> i64 {
let answer_length: usize = 12;
let mut final_answer: i64 = 0;
for line in lines {
let mut answer: Vec<i32> = Vec::new();
let mut sub_seq: Vec<i32> = line.chars().map(|c| c.to_digit(10).unwrap() as i32).collect();
for i in 1..=12 {
let (answer_digit, new_sub_seq) = find_battery(sub_seq, (answer_length - answer.len() - 1) as i32);
sub_seq = new_sub_seq;
answer.push(answer_digit);
// println!("Current answer: {:?}", answer);
// println!("Remaining seq: {:?}", sub_seq);
// println!("{}", i);
if sub_seq.len() == 1 && i==11{
answer.push(sub_seq[0]);
break;
}
}
let answer_num: i64 = answer.iter().map(|n| n.to_string()).collect::<String>().parse().unwrap();
println!("Final answer for line {} is {:?}", line, answer_num);
final_answer += answer_num;
}
final_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();
let test2: Vec<&str> = vec!["987654321111111"];
println!("Test 1 {:?}", test1);
println!("The answer is: {:?}", find_battery(test1, 2));
println!("The answer is: {:?}", get_all_batteries(task_test));
println!("The answer is: {:?}", get_all_batteries(task_task));
}