]> aoc.elinar.fr Git - aoc_2024/commitdiff
Day07 - part 1
authoralex <>
Sat, 7 Dec 2024 08:23:41 +0000 (09:23 +0100)
committeralex <>
Sat, 7 Dec 2024 08:23:41 +0000 (09:23 +0100)
src/day07.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day07.rs b/src/day07.rs
new file mode 100644 (file)
index 0000000..1b1f41f
--- /dev/null
@@ -0,0 +1,92 @@
+use std::error::Error;
+use std::path::Path;
+
+fn possible_values(operands: &[u64]) -> Vec<u64> {
+    let mut res: Vec<u64> = Vec::new();
+    for o in operands {
+        if res.is_empty() {
+            res.push(*o);
+        }
+        else {
+            let tmp = res.clone();
+            res = Vec::new();
+            for t in tmp {
+                res.push(t + o);
+                res.push(t * o);
+            }
+        }
+    }
+    res
+}
+
+fn run_part1(input: &str) -> Result<u64, Box<dyn Error>> {
+    println!("Running {} - part 1", get_day());
+
+    let res = input.lines()
+        .map(|l| {
+            let (tot, ops) = l.split_once(":").unwrap();
+            let ops = ops.trim().split(" ")
+                .map(|v| v.parse::<u64>().unwrap())
+                .collect::<Vec<u64>>();
+            (tot.parse::<u64>().unwrap(), ops)
+        })
+        .map(|(tot, ops)| {
+            let values = possible_values(&ops);
+            if values.contains(&tot) {
+                tot
+            }
+            else {
+                0
+            }
+        })
+        .sum();
+
+    Ok(res)
+}
+
+fn run_part2(input: &str) -> Result<u64, 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 = "\
+190: 10 19
+3267: 81 40 27
+83: 17 5
+156: 15 6
+7290: 6 8 6 15
+161011: 16 10 13
+192: 17 8 14
+21037: 9 7 18 13
+292: 11 6 16 20";
+
+    #[test]
+    fn test_part1() {
+        assert_eq!(3749, run_part1(TEXT_INPUT).unwrap());
+    }
+
+    #[test]
+    fn test_part2() {
+        assert_eq!(0, run_part2(TEXT_INPUT).unwrap());
+    }
+}
index 9d5ac55503bd8a5a182a9679710147c69fed1c57..a3058a07cd5e9b9b32c0b55302097ae76962d58c 100644 (file)
@@ -10,6 +10,7 @@ pub mod day03;
 pub mod day04;
 pub mod day05;
 pub mod day06;
+pub mod day07;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -38,6 +39,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box<dyn Error>> {
         "day04" => day04::run(&input)?,
         "day05" => day05::run(&input)?,
         "day06" => day06::run(&input)?,
+        "day07" => day07::run(&input)?,
         _ => return Err(format!("unknown or unimplemented day \"{day}\"").into()),
     }
     Ok(())