-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad1_2.rs
More file actions
executable file
·62 lines (61 loc) · 2.67 KB
/
ad1_2.rs
File metadata and controls
executable file
·62 lines (61 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
58
59
60
61
62
use std::fs;
fn calculate_zeros(lines:Vec<&str>, start: i32, modulo: i32)-> i32 {
let mut current = start;
let mut answer = 0;
let mut prev;
for line in lines {
let direction = line.chars().nth(0);
let sign: i32;
if direction == Some('L'){
sign = -1;}
else{
sign = 1;
}
let turn: i32 = line[1..].trim().parse().unwrap();
prev = current;
current += sign * (turn % modulo);
answer += (turn / modulo).abs();
// println!("{current}");
if current % modulo != 0 {
answer += (current / modulo).abs();
// println!("{line}, {answer}-> zero here");
}
current = current % modulo;
// println!("{line}, {answer}, {turn}, {current},- {prev}, -> answer update here 0");
if (current == 0) && (prev != 0){
answer += 1;
// println!("{line}, {answer}-> zero here");
} else {
answer += (current / modulo).abs();
// println!("{line}, {answer} -> answer update here 1");
}
if current * prev < 0 {
answer += 1;
// println!("{line}, {answer} -> answer update here 2");
}
// println!("Current: {answer}, {current}");
}
return answer;
}
fn main() {
let file_path = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task1_test.txt";
let file_path2 = "C:\\Users\\iuliia.bubis\\Documents\\MobaXterm\\home\\advent_of_code\\task1.txt";
println!("Read the file {}", file_path);
let task = fs::read_to_string(file_path)
.expect("REad the file error?");
let test_task: Vec<&str> = task.lines().collect();
let task2 = fs::read_to_string(file_path2)
.expect("REad the file error?");
let task_lines: Vec<&str> = task2.lines().collect();
// println!("In the file:\n{task}");
let test1: Vec<&str> = "R50\nL100\nL100".lines().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, calculate_zeros(test1.clone(), 50, 100));
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: {}", calculate_zeros(test_task, 50, 100));
println!("The answer is: {}", calculate_zeros(task_lines, 50, 100));
}