1
0
Fork 0

Example solved but bug in input

This commit is contained in:
Pcornat 2025-12-03 23:05:12 +01:00
commit 1566e3e374
Signed by: Pcornat
GPG key ID: E0326CC678A00BDD
6 changed files with 4678 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
Cargo.lock
.idea

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "adventofcode2025"
version = "0.1.0"
edition = "2024"
[dependencies]

4542
input/day1.txt Normal file

File diff suppressed because it is too large Load diff

10
sample/day1.txt Normal file
View file

@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

110
src/day1.rs Normal file
View file

@ -0,0 +1,110 @@
use std::str::FromStr;
const NEUTRAL: i32 = 100;
const MIN: i32 = 0;
const MAX: i32 = 99;
const INIT: i32 = 50;
#[derive(Debug)]
enum Direction {
L,
R,
}
#[derive(Debug, PartialEq, Eq)]
struct ParseDirectionError;
impl FromStr for Direction {
type Err = ParseDirectionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"L" => Ok(Direction::L),
"R" => Ok(Direction::R),
_ => Err(ParseDirectionError),
}
}
}
#[derive(Debug)]
struct Instruction {
direction: Direction,
value: i32,
}
impl FromStr for Instruction {
type Err = ParseDirectionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(dir) = Direction::from_str(s.get(..1).unwrap()) {
Ok(Instruction {
direction: dir,
value: s.get(1..).unwrap().parse().unwrap(),
})
} else {
Err(ParseDirectionError)
}
}
}
pub fn solve_part1(input: &str) -> usize {
input
.lines()
.map(|x| Instruction::from_str(x).unwrap())
.scan(INIT, |acc, instr| {
match instr.direction {
Direction::L => {
*acc -= instr.value;
if *acc < MIN {
*acc += NEUTRAL;
}
}
Direction::R => {
*acc += instr.value;
if *acc > MAX {
*acc -= NEUTRAL;
}
}
}
Some(*acc)
})
.filter(|&val| val == 0)
.count()
}
fn intermediate_values(input: &str) -> Vec<i32> {
input
.lines()
.map(|x| Instruction::from_str(x).unwrap())
.scan(INIT, |acc, instr| {
match instr.direction {
Direction::L => {
*acc -= instr.value;
if *acc < MIN {
*acc += NEUTRAL;
}
}
Direction::R => {
*acc += instr.value;
if *acc > MAX {
*acc -= NEUTRAL;
}
}
}
Some(*acc)
})
.collect()
}
#[cfg(test)]
mod test {
use crate::day1::{intermediate_values, solve_part1};
#[test]
fn part1_sample() {
assert_eq!(
intermediate_values(include_str!("../sample/day1.txt")),
[82, 52, 0, 95, 55, 0, 99, 0, 14, 32]
);
assert_eq!(solve_part1(include_str!("../sample/day1.txt")), 3);
}
}

7
src/main.rs Normal file
View file

@ -0,0 +1,7 @@
mod day1;
fn main() {
println!("Hello, world!");
let res = day1::solve_part1(include_str!("../input/day1.txt"));
println!("Solution: {}", res);
}