Solution for 2025-1, in rust
This commit is contained in:
48
2025/1/deadvey/src/main.rs
Normal file
48
2025/1/deadvey/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::time::Instant;
|
||||
|
||||
// Right = up/increase
|
||||
// Left = down/decrease
|
||||
fn main() -> io::Result<()>
|
||||
{
|
||||
let start = Instant::now();
|
||||
let file_contents: String = fs::read_to_string("large.txt")?;
|
||||
let lines: Vec<&str> = file_contents
|
||||
.split("\n")
|
||||
.collect();
|
||||
|
||||
let mut position: i32 = 50;
|
||||
let mut total1: i32 = 0;
|
||||
let mut total2: i32 = 0;
|
||||
|
||||
println!("The dial starts by pointing at {position}");
|
||||
|
||||
for index in 0..&lines.len()-1
|
||||
{
|
||||
let movement = &lines[index][0..1];
|
||||
let mut number: i32 = lines[index][1..lines[index].len()].parse().unwrap();
|
||||
|
||||
if movement == "L" {
|
||||
number = -number
|
||||
};
|
||||
if number < 0 {
|
||||
total2 += (100 - position - number) / 100 - (position == 0) as i32
|
||||
}
|
||||
else {
|
||||
total2 += (number+position) / 100
|
||||
};
|
||||
position = (position + number).rem_euclid(100);
|
||||
if position == 0 {
|
||||
total1 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let end = start.elapsed();
|
||||
println!("part1: {total1}");
|
||||
println!("part2: {total2}");
|
||||
println!("Took: {:.2?}",end);
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user