add day 15
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day15"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day15"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Disc #1 has 5 positions; at time=0, it is at position 4.
|
||||||
|
Disc #2 has 2 positions; at time=0, it is at position 1.
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
Disc #1 has 13 positions; at time=0, it is at position 1.
|
||||||
|
Disc #2 has 19 positions; at time=0, it is at position 10.
|
||||||
|
Disc #3 has 3 positions; at time=0, it is at position 2.
|
||||||
|
Disc #4 has 7 positions; at time=0, it is at position 1.
|
||||||
|
Disc #5 has 5 positions; at time=0, it is at position 3.
|
||||||
|
Disc #6 has 17 positions; at time=0, it is at position 5.
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
#[cfg(debug_assertions)]
|
||||||
|
const FILENAME: &str = "example.txt";
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
const FILENAME: &str = "input.txt";
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut discs: Vec<_> = std::fs::read_to_string(FILENAME)
|
||||||
|
.unwrap()
|
||||||
|
.lines()
|
||||||
|
.map(Disc::parse)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
part_1(&discs);
|
||||||
|
|
||||||
|
discs.push(Disc {
|
||||||
|
positions: 11,
|
||||||
|
current_position: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
part_1(&discs);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(discs: &[Disc]) {
|
||||||
|
let mut initial_state = Simulation::new(discs);
|
||||||
|
|
||||||
|
let mut ball_drop_time = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut simulation = initial_state.clone();
|
||||||
|
|
||||||
|
let success = simulation.run();
|
||||||
|
if success {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ball_drop_time += 1;
|
||||||
|
initial_state.advance_discs();
|
||||||
|
}
|
||||||
|
println!("{ball_drop_time}")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct Simulation {
|
||||||
|
discs: Vec<Disc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Simulation {
|
||||||
|
fn new(discs: &[Disc]) -> Self {
|
||||||
|
Self {
|
||||||
|
discs: discs.to_vec(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&mut self) -> bool {
|
||||||
|
let mut ball = 0;
|
||||||
|
loop {
|
||||||
|
ball += 1;
|
||||||
|
self.advance_discs();
|
||||||
|
|
||||||
|
if let Some(disc) = self.discs.get(ball - 1) {
|
||||||
|
if disc.current_position != 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn advance_discs(&mut self) {
|
||||||
|
self.discs.iter_mut().for_each(|disc| disc.advance());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct Disc {
|
||||||
|
positions: usize,
|
||||||
|
current_position: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Disc {
|
||||||
|
fn parse(s: &str) -> Self {
|
||||||
|
let mut iter = s.split_whitespace();
|
||||||
|
let positions = iter.nth(3).unwrap().parse().unwrap();
|
||||||
|
|
||||||
|
let mut last_iter = iter.next_back().unwrap().chars();
|
||||||
|
last_iter.next_back();
|
||||||
|
let current_position = last_iter.collect::<String>().parse().unwrap();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
positions,
|
||||||
|
current_position,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn advance(&mut self) {
|
||||||
|
self.current_position += 1;
|
||||||
|
self.current_position %= self.positions;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user