mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 14:31:34 +01:00
Solve 2025 day 1 part 2 in rust
This commit is contained in:
parent
d14d435b0a
commit
e2af8dddbe
1 changed files with 28 additions and 6 deletions
|
|
@ -1,12 +1,13 @@
|
|||
use std::fs::{self};
|
||||
|
||||
fn main() {
|
||||
let rotations = read_input();
|
||||
let rotations = read_rotations();
|
||||
|
||||
println!("Password: {}", count_neutral_positions(rotations));
|
||||
println!("Password1: {}", count_zero_positions(&rotations));
|
||||
println!("Password2: {}", count_zero_clicks(&rotations));
|
||||
}
|
||||
|
||||
fn read_input() -> Vec<i32> {
|
||||
fn read_rotations() -> Vec<i32> {
|
||||
let contents = fs::read_to_string("../../inputs/01.txt").unwrap();
|
||||
let lines = contents.lines();
|
||||
|
||||
|
|
@ -18,7 +19,7 @@ fn read_input() -> Vec<i32> {
|
|||
let distance = distance.parse::<i32>().unwrap();
|
||||
|
||||
let rotation = match direction {
|
||||
"L" => distance * -1,
|
||||
"L" => -distance,
|
||||
"R" => distance,
|
||||
_ => panic!(),
|
||||
};
|
||||
|
|
@ -29,12 +30,12 @@ fn read_input() -> Vec<i32> {
|
|||
rotations
|
||||
}
|
||||
|
||||
fn count_neutral_positions(rotations: Vec<i32>) -> i32 {
|
||||
fn count_zero_positions(rotations: &Vec<i32>) -> i32 {
|
||||
let mut counter = 0;
|
||||
let mut position = 50;
|
||||
|
||||
for rotation in rotations {
|
||||
position = (position + rotation) % 100;
|
||||
position = (position + rotation).rem_euclid(100);
|
||||
|
||||
if position == 0 {
|
||||
counter += 1;
|
||||
|
|
@ -43,3 +44,24 @@ fn count_neutral_positions(rotations: Vec<i32>) -> i32 {
|
|||
|
||||
counter
|
||||
}
|
||||
|
||||
fn count_zero_clicks(rotations: &Vec<i32>) -> i32 {
|
||||
let mut counter = 0;
|
||||
let mut position = 50;
|
||||
|
||||
for rotation in rotations {
|
||||
position += rotation;
|
||||
|
||||
counter += (position / 100).abs();
|
||||
if position < 0 && position != *rotation {
|
||||
counter += 1;
|
||||
}
|
||||
if position == 0 {
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
position = position.rem_euclid(100);
|
||||
}
|
||||
|
||||
counter
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue