diff --git a/Cargo.lock b/Cargo.lock index d7d338f..d502585 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -281,9 +281,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.150" +version = "0.2.168" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" [[package]] name = "lock_api" @@ -513,18 +513,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", diff --git a/README.md b/README.md index 0577e01..1c99a57 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/data/examples/13.txt b/data/examples/13.txt new file mode 100644 index 0000000..912f482 --- /dev/null +++ b/data/examples/13.txt @@ -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 diff --git a/src/bin/13.rs b/src/bin/13.rs new file mode 100644 index 0000000..bca45f1 --- /dev/null +++ b/src/bin/13.rs @@ -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 { + let re = Regex::new(r"\d+").unwrap(); + Some( + input + .split("\n\n") + .map(|machine| { + let numbers: Vec> = machine + .lines() + .map(|line| { + re.captures_iter(line) + .map(|cap| cap[0].parse::().unwrap()) + .collect::>() + }) + .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::() as u64, + ) +} + +pub fn part_one(input: &str) -> Option { + solve(input, 0) +} + +pub fn part_two(input: &str) -> Option { + 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)); + } +}