y2025d1
This commit is contained in:
27
y2025/src/bin/d1.rs
Normal file
27
y2025/src/bin/d1.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::{fs, time::Instant};
|
||||
|
||||
use utils::time::get_elapsed_string;
|
||||
use y2025::days::d1;
|
||||
|
||||
fn main() {
|
||||
let now = Instant::now();
|
||||
println!("Part 1:");
|
||||
part1();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
let now = Instant::now();
|
||||
println!("Part 2:");
|
||||
part2();
|
||||
println!("Ran in {}", get_elapsed_string(now.elapsed()));
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap();
|
||||
println!("{}", d1::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap();
|
||||
println!("{}", d1::process_part2(&content));
|
||||
}
|
||||
95
y2025/src/days/d1.rs
Normal file
95
y2025/src/days/d1.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
pub fn process_part1(input: &str) -> i32 {
|
||||
let mut current = 50;
|
||||
let mut zeroes = 0;
|
||||
for line in input.lines() {
|
||||
let (direction, distance) = line.split_at(1);
|
||||
let distance = distance.parse::<i32>().unwrap() % 100;
|
||||
match direction {
|
||||
"L" => current -= distance,
|
||||
"R" => current += distance,
|
||||
_ => panic!("Invalid direction"),
|
||||
};
|
||||
if current < 0 {
|
||||
current += 100;
|
||||
} else if current > 99 {
|
||||
current -= 100;
|
||||
}
|
||||
if current == 0 {
|
||||
zeroes += 1;
|
||||
}
|
||||
}
|
||||
return zeroes;
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i32 {
|
||||
let mut current = 50;
|
||||
let mut zeroes = 0;
|
||||
for line in input.lines() {
|
||||
let is_zero = current == 0;
|
||||
let (direction, distance) = line.split_at(1);
|
||||
let distance = distance.parse::<i32>().unwrap();
|
||||
let mut rotations = {
|
||||
if direction == "L" {
|
||||
if current - distance < 0 && !is_zero {
|
||||
(current - distance).abs() / 100 + 1
|
||||
} else if current - distance < 0 && is_zero {
|
||||
(current - distance).abs() / 100
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
if current + distance > 99 {
|
||||
(current + distance) / 100
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
};
|
||||
match direction {
|
||||
"L" => current -= distance % 100,
|
||||
"R" => current += distance % 100,
|
||||
_ => panic!("Invalid direction"),
|
||||
};
|
||||
if current < 0 {
|
||||
current += 100;
|
||||
} else if current > 99 {
|
||||
current -= 100;
|
||||
}
|
||||
if current == 0 {
|
||||
zeroes += 1;
|
||||
if rotations > 0 {
|
||||
rotations -= 1;
|
||||
}
|
||||
}
|
||||
zeroes += rotations;
|
||||
}
|
||||
return zeroes;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 6);
|
||||
}
|
||||
}
|
||||
1
y2025/src/days/mod.rs
Normal file
1
y2025/src/days/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod d1;
|
||||
1
y2025/src/lib.rs
Normal file
1
y2025/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod days;
|
||||
Reference in New Issue
Block a user