Day 1 part 1
commit
73246304f5
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "day-1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue