mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 14:31:34 +01:00
Improve 2025 day 1 in rust
This commit is contained in:
parent
e2af8dddbe
commit
99554bc83b
1 changed files with 22 additions and 27 deletions
|
|
@ -1,5 +1,8 @@
|
|||
use std::fs::{self};
|
||||
|
||||
const STARTING_POSITION: i32 = 50;
|
||||
const DIAL_RANGE: i32 = 100;
|
||||
|
||||
fn main() {
|
||||
let rotations = read_rotations();
|
||||
|
||||
|
|
@ -8,34 +11,26 @@ fn main() {
|
|||
}
|
||||
|
||||
fn read_rotations() -> 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,
|
||||
"R" => distance,
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
rotations.push(rotation);
|
||||
}
|
||||
|
||||
rotations
|
||||
fs::read_to_string("../../inputs/01.txt")
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let distance = line[1..].parse::<i32>().unwrap();
|
||||
if line.starts_with('L') {
|
||||
-distance
|
||||
} else {
|
||||
distance
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_zero_positions(rotations: &Vec<i32>) -> i32 {
|
||||
fn count_zero_positions(rotations: &[i32]) -> i32 {
|
||||
let mut counter = 0;
|
||||
let mut position = 50;
|
||||
let mut position = STARTING_POSITION;
|
||||
|
||||
for rotation in rotations {
|
||||
position = (position + rotation).rem_euclid(100);
|
||||
position = (position + rotation).rem_euclid(DIAL_RANGE);
|
||||
|
||||
if position == 0 {
|
||||
counter += 1;
|
||||
|
|
@ -45,14 +40,14 @@ fn count_zero_positions(rotations: &Vec<i32>) -> i32 {
|
|||
counter
|
||||
}
|
||||
|
||||
fn count_zero_clicks(rotations: &Vec<i32>) -> i32 {
|
||||
fn count_zero_clicks(rotations: &[i32]) -> i32 {
|
||||
let mut counter = 0;
|
||||
let mut position = 50;
|
||||
let mut position = STARTING_POSITION;
|
||||
|
||||
for rotation in rotations {
|
||||
position += rotation;
|
||||
|
||||
counter += (position / 100).abs();
|
||||
counter += (position / DIAL_RANGE).abs();
|
||||
if position < 0 && position != *rotation {
|
||||
counter += 1;
|
||||
}
|
||||
|
|
@ -60,7 +55,7 @@ fn count_zero_clicks(rotations: &Vec<i32>) -> i32 {
|
|||
counter += 1;
|
||||
}
|
||||
|
||||
position = position.rem_euclid(100);
|
||||
position = position.rem_euclid(DIAL_RANGE);
|
||||
}
|
||||
|
||||
counter
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue