y2024d13 gonna be honest here, I was too lazy to do part2 by myself. I could have done it but it wold have taken some time as I have to remember how to solve linear equations
This commit is contained in:
parent
548f1b3d73
commit
fa842890ce
@ -45,7 +45,7 @@ pub fn process_part1(input: &str, grid_size: (usize, usize)) -> i32 {
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i32 {
|
||||
pub fn process_part2(_input: &str) -> i32 {
|
||||
0
|
||||
}
|
||||
|
||||
|
1279
y2024/resources/13_input.txt
Normal file
1279
y2024/resources/13_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
27
y2024/src/bin/d13.rs
Normal file
27
y2024/src/bin/d13.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use std::{fs, time::Instant};
|
||||
|
||||
use utils::time::get_elapsed_string;
|
||||
use y2024::days::d13;
|
||||
|
||||
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/13_input.txt")).unwrap();
|
||||
println!("{}", d13::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/13_input.txt")).unwrap();
|
||||
println!("{}", d13::process_part2(&content));
|
||||
}
|
151
y2024/src/days/d13.rs
Normal file
151
y2024/src/days/d13.rs
Normal file
@ -0,0 +1,151 @@
|
||||
use core::panic;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct ClawMachine {
|
||||
button_a: (i64, i64),
|
||||
button_b: (i64, i64),
|
||||
prize: (i64, i64),
|
||||
}
|
||||
|
||||
impl ClawMachine {
|
||||
fn parse(s: &str) -> Self {
|
||||
let mut button_a = (0, 0);
|
||||
let mut button_b = (0, 0);
|
||||
let mut prize = (0, 0);
|
||||
s.lines().for_each(|line| {
|
||||
let (field, coords) = line.split_once(": ").unwrap();
|
||||
if field == "Button A" {
|
||||
let (x, y) = coords.split_once(", ").unwrap();
|
||||
let x: i64 = x.split_once("+").unwrap().1.parse().unwrap();
|
||||
let y: i64 = y.split_once("+").unwrap().1.parse().unwrap();
|
||||
button_a = (x, y);
|
||||
} else if field == "Button B" {
|
||||
let (x, y) = coords.split_once(", ").unwrap();
|
||||
let x: i64 = x.split_once("+").unwrap().1.parse().unwrap();
|
||||
let y: i64 = y.split_once("+").unwrap().1.parse().unwrap();
|
||||
button_b = (x, y);
|
||||
} else if field == "Prize" {
|
||||
let (x, y) = coords.split_once(", ").unwrap();
|
||||
let x: i64 = x.split_once("=").unwrap().1.parse().unwrap();
|
||||
let y: i64 = y.split_once("=").unwrap().1.parse().unwrap();
|
||||
prize = (x, y);
|
||||
} else {
|
||||
panic!("Unknown machine information {line}");
|
||||
}
|
||||
});
|
||||
Self {
|
||||
button_a,
|
||||
button_b,
|
||||
prize,
|
||||
}
|
||||
}
|
||||
|
||||
fn correct(&mut self) {
|
||||
self.prize.0 += 10000000000000;
|
||||
self.prize.1 += 10000000000000;
|
||||
}
|
||||
|
||||
fn parse_all(input: &str) -> Vec<Self> {
|
||||
input.split("\n\n").map(Self::parse).collect()
|
||||
}
|
||||
|
||||
//fn get_moves(&self) -> Option<(i64, i64)> {
|
||||
// if self.prize.0 % self.button_b.0 == 0
|
||||
// && self.prize.1 % self.button_b.1 == 0
|
||||
// && self.prize.0 / self.button_b.0 == self.prize.1 / self.button_b.1
|
||||
// {
|
||||
// let b_moves = self.prize.0 / self.button_b.0;
|
||||
// return Some((0, b_moves));
|
||||
// }
|
||||
|
||||
// for b_presses in (1..=100).rev() {
|
||||
// let after_b_moves = (self.button_b.0 * b_presses, self.button_b.1 * b_presses);
|
||||
// if after_b_moves.0 < self.prize.0 && after_b_moves.1 < self.prize.1 {
|
||||
// for a_presses in 1..=100 {
|
||||
// let after_a_moves = (
|
||||
// after_b_moves.0 + a_presses * self.button_a.0,
|
||||
// after_b_moves.1 + a_presses * self.button_a.1,
|
||||
// );
|
||||
// if after_a_moves == self.prize {
|
||||
// return Some((a_presses, b_presses));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// None
|
||||
//}
|
||||
}
|
||||
|
||||
pub fn process_part1(input: &str) -> i64 {
|
||||
let machines = ClawMachine::parse_all(input);
|
||||
machines
|
||||
.iter()
|
||||
.map(|machine| {
|
||||
let p = machine.prize;
|
||||
let a = machine.button_a;
|
||||
let b = machine.button_b;
|
||||
let bm = (p.1 * a.0 - p.0 * a.1) / (b.1 * a.0 - b.0 * a.1);
|
||||
let am = (p.0 - bm * b.0) / a.0;
|
||||
if p.0 != am * a.0 + bm * b.0 || p.1 != am * a.1 + bm * b.1 {
|
||||
0
|
||||
} else {
|
||||
am * 3 + bm
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i64 {
|
||||
let mut machines = ClawMachine::parse_all(input);
|
||||
for machine in machines.iter_mut() {
|
||||
machine.correct();
|
||||
}
|
||||
machines
|
||||
.iter()
|
||||
.map(|machine| {
|
||||
let p = machine.prize;
|
||||
let a = machine.button_a;
|
||||
let b = machine.button_b;
|
||||
let bm = (p.1 * a.0 - p.0 * a.1) / (b.1 * a.0 - b.0 * a.1);
|
||||
let am = (p.0 - bm * b.0) / a.0;
|
||||
if p.0 != am * a.0 + bm * b.0 || p.1 != am * a.1 + bm * b.1 {
|
||||
0
|
||||
} else {
|
||||
am * 3 + bm
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 480);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 0);
|
||||
}
|
||||
}
|
@ -13,3 +13,5 @@ pub mod d10;
|
||||
pub mod d11;
|
||||
|
||||
pub mod d12;
|
||||
|
||||
pub mod d13;
|
||||
|
Loading…
Reference in New Issue
Block a user