Solve 2025 day 1 part 1 in rust

This commit is contained in:
SebastianStork 2025-12-01 14:50:26 +01:00
parent 47023fc3dd
commit d14d435b0a
Signed by: SebastianStork
SSH key fingerprint: SHA256:iEM011ogNMG1q8+U500adGu/9rpPuZ2KnFtbdLeqTiI
5 changed files with 69 additions and 0 deletions

7
2025/rust/day-01/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "day-01"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "day-01"
version = "0.1.0"
edition = "2024"
[dependencies]

View file

@ -0,0 +1,45 @@
use std::fs::{self};
fn main() {
let rotations = read_input();
println!("Password: {}", count_neutral_positions(rotations));
}
fn read_input() -> Vec<i32> {
let contents = fs::read_to_string("../../inputs/01.txt").unwrap();
let lines = contents.lines();
let mut rotations = vec![];
for line in lines {
let (direction, distance) = line.split_at(1);
let distance = distance.parse::<i32>().unwrap();
let rotation = match direction {
"L" => distance * -1,
"R" => distance,
_ => panic!(),
};
rotations.push(rotation);
}
rotations
}
fn count_neutral_positions(rotations: Vec<i32>) -> i32 {
let mut counter = 0;
let mut position = 50;
for rotation in rotations {
position = (position + rotation) % 100;
if position == 0 {
counter += 1;
}
}
counter
}