Add day13

This commit is contained in:
Adrian Groh 2024-12-13 09:37:24 +01:00
parent b1771c0104
commit e0d3ca6c59
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771
3 changed files with 124 additions and 0 deletions

54
day13/Cargo.lock generated Normal file
View File

@ -0,0 +1,54 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "day13"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"

7
day13/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "day13"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1.11.1"

63
day13/src/main.rs Normal file
View File

@ -0,0 +1,63 @@
use regex::Regex;
#[derive(Debug, Clone)]
struct Machine {
x1: isize,
y1: isize,
x2: isize,
y2: isize,
x3: isize,
y3: isize,
}
#[rustfmt::skip]
fn parse(input: &str) -> Vec<Machine> {
let re = Regex::new(r"\d+").unwrap();
input
.split("\n\n")
.map(|m| {
let mut captures = re.captures_iter(m);
Machine {
x1: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
y1: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
x2: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
y2: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
x3: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
y3: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(),
}
})
.collect()
}
fn solve(m: &Machine) -> isize {
let d = m.x1 * m.y2 - m.y1 * m.x2;
let a = (m.x3 * m.y2 - m.x2 * m.y3) / d;
let b = (m.x1 * m.y3 - m.x3 * m.y1) / d;
if a * m.x1 + b * m.x2 == m.x3 && a * m.y1 + b * m.y2 == m.y3 {
3 * a + b
} else {
0
}
}
fn part1(machines: &[Machine]) -> isize {
machines.iter().map(solve).sum()
}
fn part2(machines: &mut [Machine]) -> isize {
machines
.iter_mut()
.map(|m| {
m.x3 += 10000000000000;
m.y3 += 10000000000000;
m
})
.map(|m| solve(m))
.sum()
}
fn main() {
let mut input = parse(include_str!("../input.txt"));
println!("{}", part1(&input));
println!("{}", part2(&mut input));
}