]> aoc.elinar.fr Git - aoc_2024/commitdiff
Day05 - part 1
authoralex <>
Thu, 5 Dec 2024 08:25:08 +0000 (09:25 +0100)
committeralex <>
Thu, 5 Dec 2024 08:25:08 +0000 (09:25 +0100)
src/day05.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day05.rs b/src/day05.rs
new file mode 100644 (file)
index 0000000..d7fd970
--- /dev/null
@@ -0,0 +1,127 @@
+use std::error::Error;
+use std::path::Path;
+use std::collections::{HashMap, HashSet};
+
+struct Puzzle {
+    order: HashMap<u32, HashSet<u32>>,
+    lines: Vec<Vec<u32>>,
+}
+
+impl Puzzle {
+    pub fn new(input: &str) -> Self {
+        let (o, l) = input.split_once("\n\n").unwrap();
+
+        let mut order: HashMap<u32, HashSet<u32>> = HashMap::new();
+        o.lines().for_each(|l| {
+            let o = l.split("|")
+                .map(|v| v.parse::<u32>().unwrap())
+                .collect::<Vec<u32>>();
+            let (before, after) = (o[0], o[1]);
+            order.entry(before).and_modify(|s| {s.insert(after);}).or_insert({
+                let mut s = HashSet::new();
+                s.insert(after);
+                s
+            });
+        });
+
+        let lines = l.lines().map(|v| {
+            v.split(",").map(|x| x.parse::<u32>().unwrap()).collect::<Vec<u32>>()
+        }).collect::<Vec<Vec<u32>>>();
+
+        Self {
+            order,
+            lines,
+        }
+    }
+
+    pub fn is_ordered(self: &Self, line: &[u32]) -> bool {
+        for i in 1..line.len() {
+            let v = &line[i];
+            for before in &line[0..i] {
+                if self.order.get(v).is_some() && self.order.get(v).unwrap().contains(before) {
+                    return false
+                }
+            }
+        }
+        true
+    }
+}
+
+fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
+    println!("Running {} - part 1", get_day());
+
+    let puzzle = Puzzle::new(input);
+
+    let res = puzzle.lines.iter()
+        .filter(|l| puzzle.is_ordered(l))
+        .map(|l| l[l.len()/2])
+        .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 = "\
+47|53
+97|13
+97|61
+97|47
+75|29
+61|13
+75|53
+29|13
+97|29
+53|29
+61|53
+97|53
+61|29
+47|13
+75|47
+97|75
+47|61
+75|61
+47|29
+75|13
+53|13
+
+75,47,61,53,29
+97,61,53,29,13
+75,29,13
+75,97,47,61,53
+61,13,29
+97,13,75,29,47";
+
+    #[test]
+    fn test_part1() {
+        assert_eq!(143, run_part1(TEXT_INPUT).unwrap());
+    }
+
+    #[test]
+    fn test_part2() {
+        assert_eq!(0, run_part2(TEXT_INPUT).unwrap());
+    }
+}
index 0a828f075d0ca1c716e35d928eca33dc47df45aa..9760d8febafe3f4d867f06fca785bb3ec4de5f02 100644 (file)
@@ -8,6 +8,7 @@ pub mod day01;
 pub mod day02;
 pub mod day03;
 pub mod day04;
+pub mod day05;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -34,6 +35,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box<dyn Error>> {
         "day02" => day02::run(&input)?,
         "day03" => day03::run(&input)?,
         "day04" => day04::run(&input)?,
+        "day05" => day05::run(&input)?,
         _ => return Err(format!("unknown or unimplemented day \"{day}\"").into()),
     }
     Ok(())