-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad2_2.rs
More file actions
executable file
·59 lines (55 loc) · 2.51 KB
/
ad2_2.rs
File metadata and controls
executable file
·59 lines (55 loc) · 2.51 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
use std::fs;
use std::collections::HashSet;
fn check_id(num: i64)-> i64 {
let s: String = num.to_string();
let pos_parts = (s.len() as f64 / 2.0).round() as usize;
if s.len() > 1 {
for chunck_size in 1..=pos_parts {
if s.len() % chunck_size == 0 {
let chunks: Vec<String> = s.chars()
.collect::<Vec<char>>()
.chunks(chunck_size)
.map(|c| c.iter().collect())
.collect();
let chunks_set: HashSet<String> = chunks.into_iter().collect();
if chunks_set.len() == 1 {
return num;
}
}
}
}
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> = "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: {}", check_id_lists(task_test));
println!("The answer is: {}", check_id_lists(task_task));
}