Day 1 part 1

main
kirbylife 2025-12-01 22:33:47 -06:00
commit 73246304f5
2 changed files with 59 additions and 0 deletions

6
day-1/Cargo.toml 100644
View File

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

53
day-1/src/main.rs 100644
View File

@ -0,0 +1,53 @@
use std::fs::File;
use std::io::prelude::*;
#[derive(Debug)]
enum Direction {
LEFT,
RIGHT,
}
impl Direction {
fn from_str(s: &str) -> Self {
match s {
"L" => Direction::LEFT,
"R" => Direction::RIGHT,
_ => Direction::RIGHT,
}
}
}
fn parse_input() -> Vec<(Direction, i32)> {
let mut file = File::open("input.txt").unwrap();
let mut buffer = String::new();
file.read_to_string(&mut buffer).unwrap();
return buffer
.lines()
.map(|line| {
let (dir, steps) = line.split_at(1);
(Direction::from_str(dir), steps.parse().unwrap())
})
.collect::<Vec<(Direction, i32)>>();
}
fn part_1() {
let input = parse_input();
let mut dial = 50;
let mut counter = 0;
for (dir, steps) in input {
dial += match dir {
Direction::LEFT => steps * -1,
Direction::RIGHT => steps,
};
dial = dial % 100;
if dial == 0 {
counter += 1;
}
}
println!("{}", counter);
}
fn main() {
println!("Part 1:");
part_1();
}