2025 - Day 1 part 1 - rust
This commit is contained in:
parent
18ad109e33
commit
35378c059b
1 changed files with 32 additions and 0 deletions
32
2025/rust1/src/main.rs
Normal file
32
2025/rust1/src/main.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use std::io::{BufRead, BufReader};
|
||||
use std::fs::File;
|
||||
|
||||
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();
|
||||
|
||||
if direction == 'L' {
|
||||
dial -= distance;
|
||||
} else {
|
||||
dial += distance;
|
||||
}
|
||||
|
||||
dial = dial.rem_euclid(100);
|
||||
if dial == 0{
|
||||
count += 1;
|
||||
}
|
||||
|
||||
}
|
||||
println!("count: {}\n", count)
|
||||
}
|
||||
Loading…
Reference in a new issue