mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 13:21:34 +01:00
Solve 2025 day 1 part 1 in rust
This commit is contained in:
parent
47023fc3dd
commit
d14d435b0a
5 changed files with 69 additions and 0 deletions
10
2025/inputs/01.txt
Normal file
10
2025/inputs/01.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
1
2025/rust/.envrc
Normal file
1
2025/rust/.envrc
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
use flake .#rust
|
||||||
7
2025/rust/day-01/Cargo.lock
generated
Normal file
7
2025/rust/day-01/Cargo.lock
generated
Normal 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"
|
||||||
6
2025/rust/day-01/Cargo.toml
Normal file
6
2025/rust/day-01/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "day-01"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
45
2025/rust/day-01/src/main.rs
Normal file
45
2025/rust/day-01/src/main.rs
Normal 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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue