2025 - Day 1 part 2 - rust
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Some checks reported errors
continuous-integration/drone/push Build encountered an error
This commit is contained in:
parent
e1fa29815d
commit
0ac8fa9f18
1 changed files with 42 additions and 0 deletions
42
2025/rust1_2/src/main.rs
Normal file
42
2025/rust1_2/src/main.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
fn main() {
|
||||
// let filename = "../1/input_example.txt";
|
||||
let filename = "../1/input.txt";
|
||||
let file = BufReader::new(File::open(filename).expect("Unable to open file"));
|
||||
|
||||
let mut dial: i32 = 50;
|
||||
let mut count: i32 = 0;
|
||||
|
||||
for line in file.lines() {
|
||||
let mut wline: String = line.unwrap();
|
||||
let direction = wline.chars().next().unwrap();
|
||||
|
||||
wline.remove(0);
|
||||
let distance: i32 = wline.parse().unwrap();
|
||||
let was_zero: i32;
|
||||
|
||||
if direction == 'L' {
|
||||
if dial == 0 {
|
||||
was_zero = 0;
|
||||
} else {
|
||||
was_zero = 1;
|
||||
}
|
||||
dial -= distance;
|
||||
if dial <= 0 {
|
||||
count += (dial / 100).abs() + was_zero;
|
||||
}
|
||||
println!("Left distance={} dial={} count={}", distance, dial, count);
|
||||
} else {
|
||||
dial += distance;
|
||||
if dial > 99 {
|
||||
count += dial / 100;
|
||||
}
|
||||
println!("Right distance={} dial={} count={}", distance, dial, count);
|
||||
}
|
||||
|
||||
dial = dial.rem_euclid(100)
|
||||
}
|
||||
println!("count: {}\n", count)
|
||||
}
|
||||
Loading…
Reference in a new issue