Skip to content

Commit

Permalink
feat: day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 13, 2024
1 parent b263d6f commit afc4d64
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 10](./src/bin/10.rs) | `530.9µs` | `284.2µs` |
| [Day 11](./src/bin/11.rs) | `238.7µs` | `12.7ms` |
| [Day 12](./src/bin/12.rs) | `7.5ms` | `8.2ms` |
| [Day 13](./src/bin/13.rs) | `363.7µs` | `344.0µs` |

**Total: 464.87ms**
**Total: 465.58ms**
<!--- benchmarking table --->

---
Expand Down
15 changes: 15 additions & 0 deletions data/examples/13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
85 changes: 85 additions & 0 deletions src/bin/13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use regex::Regex;
advent_of_code::solution!(13);

#[derive(Debug)]
struct Machine {
a: (isize, isize),
b: (isize, isize),
prize: (isize, isize),
}

pub fn solve(input: &str, offset: isize) -> Option<u64> {
let re = Regex::new(r"\d+").unwrap();
Some(
input
.split("\n\n")
.map(|machine| {
let numbers: Vec<Vec<isize>> = machine
.lines()
.map(|line| {
re.captures_iter(line)
.map(|cap| cap[0].parse::<isize>().unwrap())
.collect::<Vec<_>>()
})
.collect();
Machine {
a: (numbers[0][0], numbers[0][1]),
b: (numbers[1][0], numbers[1][1]),
prize: (numbers[2][0] + offset, numbers[2][1] + offset),
}
})
.map(|machine| {
let (a1, b1, c1) = (machine.a.0, machine.b.0, machine.prize.0);
let (a2, b2, c2) = (machine.a.1, machine.b.1, machine.prize.1);
let (b3, c3) = (a2 * b1, a2 * c1);
let (b4, c4) = (a1 * b2, a1 * c2);
let (mut b5, mut c5) = (b4 - b3, c4 - c3);
if b5 < 0 && c5 < 0 {
b5 = -b5;
c5 = -c5;
}
if b5 < 0 || c5 < 0 {
return 0;
}
if c5 % b5 != 0 {
return 0;
}
let b6 = c5 / b5;
let b7 = c1 - b1 * b6;
if b7 < 0 {
return 0;
}
if b7 % a1 != 0 {
return 0;
}
let a6 = b7 / a1;
a6 * 3 + b6
})
.sum::<isize>() as u64,
)
}

pub fn part_one(input: &str) -> Option<u64> {
solve(input, 0)
}

pub fn part_two(input: &str) -> Option<u64> {
solve(input, 10000000000000)
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(480));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(875318608908));
}
}

0 comments on commit afc4d64

Please sign in to comment.