-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad2_1.rs
More file actions
executable file
·47 lines (44 loc) · 2.1 KB
/
ad2_1.rs
File metadata and controls
executable file
·47 lines (44 loc) · 2.1 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
use std::fs;
fn check_id(num: i64)-> i64 {
let s: String = num.to_string();
if s.len() % 2 == 0 {
let first_half = &s[..s.len()/2];
let second_half = &s[s.len()/2..];
if first_half == second_half {
return num;}
else {return 0; }}
else{return 0;}
}
fn check_id_lists(lines:Vec<&str>) -> i64 {
let mut answer = 0;
for line in lines {
let start = line.split('-').nth(0).unwrap().trim().parse::<i64>().unwrap();
let end = line.split('-').last().unwrap().trim().parse::<i64>().unwrap();
for i in start..=end {
answer += check_id(i);
}
}
return answer;
}
fn main() {
let file_path_test = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task2_test.txt";
let file_path_task = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task2.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.split(',').map(|s| s.trim()).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.split(',').map(|s| s.trim()).collect();
// println!("In the file:\n{task}");
let test1: Vec<&str> = "11-22,33-35".split(',').collect();
// let test2: Vec<&str> = "R150\nL18\nR118".lines().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, calculate_zeros(test2.clone(), 50, 100));
// 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: {}", check_id_lists(task_test));
println!("The answer is: {}", check_id_lists(task_task));
}