diff --git a/2025/inputs/01.txt b/2025/inputs/01.txt new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/2025/inputs/01.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/2025/rust/.envrc b/2025/rust/.envrc new file mode 100644 index 0000000..4196168 --- /dev/null +++ b/2025/rust/.envrc @@ -0,0 +1 @@ +use flake .#rust diff --git a/2025/rust/day-01/Cargo.lock b/2025/rust/day-01/Cargo.lock new file mode 100644 index 0000000..6e11d14 --- /dev/null +++ b/2025/rust/day-01/Cargo.lock @@ -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" diff --git a/2025/rust/day-01/Cargo.toml b/2025/rust/day-01/Cargo.toml new file mode 100644 index 0000000..c43f1c8 --- /dev/null +++ b/2025/rust/day-01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day-01" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/2025/rust/day-01/src/main.rs b/2025/rust/day-01/src/main.rs new file mode 100644 index 0000000..d8a8346 --- /dev/null +++ b/2025/rust/day-01/src/main.rs @@ -0,0 +1,62 @@ +use std::fs::{self}; + +const STARTING_POSITION: i32 = 50; +const DIAL_RANGE: i32 = 100; + +fn main() { + let rotations = read_rotations(); + + println!("Password1: {}", count_zero_positions(&rotations)); + println!("Password2: {}", count_zero_clicks(&rotations)); +} + +fn read_rotations() -> Vec { + fs::read_to_string("../../inputs/01.txt") + .unwrap() + .lines() + .map(|line| { + let distance = line[1..].parse::().unwrap(); + if line.starts_with('L') { + -distance + } else { + distance + } + }) + .collect() +} + +fn count_zero_positions(rotations: &[i32]) -> i32 { + let mut counter = 0; + let mut position = STARTING_POSITION; + + for rotation in rotations { + position = (position + rotation).rem_euclid(DIAL_RANGE); + + if position == 0 { + counter += 1; + } + } + + counter +} + +fn count_zero_clicks(rotations: &[i32]) -> i32 { + let mut counter = 0; + let mut position = STARTING_POSITION; + + for rotation in rotations { + position += rotation; + + counter += (position / DIAL_RANGE).abs(); + if position < 0 && position != *rotation { + counter += 1; + } + if position == 0 { + counter += 1; + } + + position = position.rem_euclid(DIAL_RANGE); + } + + counter +} diff --git a/flake.lock b/flake.lock index a4ac182..b86cd41 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1759439645, - "narHash": "sha256-oiAyQaRilPk525Z5aTtTNWNzSrcdJ7IXM0/PL3CGlbI=", + "lastModified": 1764522689, + "narHash": "sha256-SqUuBFjhl/kpDiVaKLQBoD8TLD+/cTUzzgVFoaHrkqY=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "879bd460b3d3e8571354ce172128fbcbac1ed633", + "rev": "8bb5646e0bed5dbd3ab08c7a7cc15b75ab4e1d0f", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-25.05", + "ref": "nixos-25.11", "repo": "nixpkgs", "type": "github" } @@ -29,11 +29,11 @@ ] }, "locked": { - "lastModified": 1759544920, - "narHash": "sha256-yQwP0JOHi3Icq09GG5ufGuGrq2zIijglVFj3kkF2MHA=", + "lastModified": 1764557621, + "narHash": "sha256-kX5PoY8hQZ80+amMQgOO9t8Tc1JZ70gYRnzaVD4AA+o=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "bd3a63bbff2c4cb3cd48e9d49f54c2ccad457f70", + "rev": "93316876c2229460a5d6f5f052766cc4cef538ce", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 40dd6bf..27280c3 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,6 @@ { inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11"; rust-overlay = { url = "github:oxalica/rust-overlay"; @@ -20,7 +20,6 @@ packages = with pkgs; [ gdb clang-tools - qt6.full ]; };