]> aoc.elinar.fr Git - aoc_2024/commitdiff
Day13 - part 1
authoralex <>
Fri, 13 Dec 2024 10:26:20 +0000 (11:26 +0100)
committeralex <>
Fri, 13 Dec 2024 10:26:20 +0000 (11:26 +0100)
src/day13.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day13.rs b/src/day13.rs
new file mode 100644 (file)
index 0000000..0c3aa22
--- /dev/null
@@ -0,0 +1,95 @@
+use std::error::Error;
+use std::path::Path;
+
+fn extract_values(line: &str) -> (i32, i32) {
+    let (_, xy) = line.split_once(":").unwrap();
+    let (x, y) = xy.split_once(",").unwrap();
+    let x = x[3..x.len()].parse::<i32>().unwrap();
+    let y = y[3..y.len()].parse::<i32>().unwrap();
+    (x, y)
+}
+
+fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
+    println!("Running {} - part 1", get_day());
+
+    let res: u32 = input.split("\n\n")
+        .map(|list| {
+            let list: Vec<_> = list.lines().collect();
+            let (xa, ya) = extract_values(list[0]);
+            let (xb, yb) = extract_values(list[1]);
+            let (xp, yp) = extract_values(list[2]);
+            let det = xa*yb - ya*xb;
+            match det {
+                0 => 0,
+                _ => {
+                    let an = yb*xp - xb*yp;
+                    let bn = xa*yp - ya*xp;
+                    if an % det !=0 || bn % det !=0 {
+                        0
+                    }
+                    else {
+                        let a = an / det;
+                        let b = bn / det;
+                        (a*3 + b) as u32
+                    }
+                }
+            }
+        })
+        .sum();
+
+    Ok(res)
+}
+
+fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
+    println!("Running {} - part 2", get_day());
+
+    Ok(0)
+}
+
+pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
+    let res = run_part1(input)?;
+    println!("{res}");
+
+    let res = run_part2(input)?;
+    println!("{res}");
+
+    Ok(())
+}
+
+fn get_day() -> String {
+    let filename = file!();
+    Path::new(filename).file_stem().unwrap().to_str().unwrap().to_string()
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    static TEXT_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 test_part1() {
+        assert_eq!(480, run_part1(TEXT_INPUT).unwrap());
+    }
+
+    #[test]
+    fn test_part2() {
+        assert_eq!(0, run_part2(TEXT_INPUT).unwrap());
+    }
+}
index 0e0f455cd6fd0cee5410852ee1fc6eaec349db5f..9f925bf18c1c54a51488770e4564964fc6d1bc20 100644 (file)
@@ -16,6 +16,7 @@ pub mod day09;
 pub mod day10;
 pub mod day11;
 pub mod day12;
+pub mod day13;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -50,6 +51,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box<dyn Error>> {
         "day10" => day10::run(&input)?,
         "day11" => day11::run(&input)?,
         "day12" => day12::run(&input)?,
+        "day13" => day13::run(&input)?,
         _ => return Err(format!("unknown or unimplemented day \"{day}\"").into()),
     }
     Ok(())