Skip to content

Commit

Permalink
AoC 2024 Day 13 - rust
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 13, 2024
1 parent 1b3ad89 commit 5121ab2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | [](src/main/python/AoC2024_08.py) | [](src/main/python/AoC2024_09.py) | [](src/main/python/AoC2024_10.py) | [](src/main/python/AoC2024_11.py) | [](src/main/python/AoC2024_12.py) | [](src/main/python/AoC2024_13.py) | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | | | | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | | | | [](src/main/rust/AoC2024_13/src/main.rs) | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

## 2023
Expand Down
7 changes: 7 additions & 0 deletions src/main/rust/AoC2024_13/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "AoC2024_13"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
108 changes: 108 additions & 0 deletions src/main/rust/AoC2024_13/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#![allow(non_snake_case)]

use aoc::Puzzle;

#[derive(Debug)]
struct Machine {
ax: i64,
ay: i64,
bx: i64,
by: i64,
px: i64,
py: i64,
}

impl Machine {
fn from_input(block: &[&String]) -> Self {
let sp: Vec<&str> = block[2].split(", ").collect();
Self {
ax: block[0][12..14].parse::<i64>().unwrap(),
ay: block[0][18..20].parse::<i64>().unwrap(),
bx: block[1][12..14].parse::<i64>().unwrap(),
by: block[1][18..20].parse::<i64>().unwrap(),
px: sp[0].split_once("=").unwrap().1.parse::<i64>().unwrap(),
py: sp[1][2..].parse::<i64>().unwrap(),
}
}
}

struct AoC2024_13;

impl AoC2024_13 {
fn solve(&self, machines: &[Machine], offset: i64) -> u64 {
fn calc_tokens(machine: &Machine, offset: i64) -> Option<u64> {
let (px, py) = (machine.px + offset, machine.py + offset);
let div =
(machine.bx * machine.ay - machine.ax * machine.by) as f64;
let ans_a = (py * machine.bx - px * machine.by) as f64 / div;
let ans_b = (px * machine.ay - py * machine.ax) as f64 / div;
match ans_a.fract() == 0.0 && ans_b.fract() == 0.0 {
true => Some(ans_a as u64 * 3 + ans_b as u64),
false => None,
}
}

machines.iter().filter_map(|m| calc_tokens(m, offset)).sum()
}
}

impl aoc::Puzzle for AoC2024_13 {
type Input = Vec<Machine>;
type Output1 = u64;
type Output2 = u64;

aoc::puzzle_year_day!(2024, 13);

fn parse_input(&self, lines: Vec<String>) -> Self::Input {
aoc::to_blocks(&lines)
.iter()
.map(|block| Machine::from_input(block))
.collect()
}

fn part_1(&self, machines: &Self::Input) -> Self::Output1 {
self.solve(machines, 0)
}

fn part_2(&self, machines: &Self::Input) -> Self::Output2 {
self.solve(machines, 10_000_000_000_000)
}

fn samples(&self) {
aoc::puzzle_samples! {
self, part_1, TEST, 480
};
}
}

fn main() {
AoC2024_13 {}.run(std::env::args());
}

const TEST: &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
";

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn samples() {
AoC2024_13 {}.samples();
}
}
7 changes: 7 additions & 0 deletions src/main/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5121ab2

Please sign in to comment.