]> aoc.elinar.fr Git - aoc_2023/commitdiff
Day14 - part 1
authoralex <null>
Thu, 14 Dec 2023 12:33:54 +0000 (13:33 +0100)
committeralex <null>
Thu, 14 Dec 2023 12:33:54 +0000 (13:33 +0100)
src/day14.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day14.rs b/src/day14.rs
new file mode 100644 (file)
index 0000000..20ad187
--- /dev/null
@@ -0,0 +1,112 @@
+use std::io::Read;
+use std::error::Error;
+use std::fs::File;
+
+pub fn run(input_file: &str) -> Result<(), Box<dyn Error>> {
+    let mut f = File::open(input_file)?;
+    let mut input = String::new();
+    f.read_to_string(&mut input)?;
+
+    let res = run_part1(&input)?;
+    println!("{res}");
+
+    let res = run_part2(&input)?;
+    println!("{res}");
+
+    Ok(())
+}
+
+fn run_part1(input: &str) -> Result<u64, Box<dyn Error>> {
+    println!("Running day14 - part 1");
+
+    let mut rows: Vec<Vec<char>> = input.lines()
+        .filter_map(|l| {
+            match l.is_empty() {
+                true => None,
+                false => {
+                    Some(l.chars().collect::<Vec<char>>())
+                },
+            }
+        })
+        .collect();
+    //rows.iter().for_each(|r| { println!("{:?}", r); });
+
+    let height = rows.len();
+    let width = rows[0].len();
+    for i in 0..width {
+        let mut j = 0;
+        while j < height {
+            while j < height && rows[j][i] != 'O' {
+                j += 1;
+            }
+            if j >= height {
+                continue;
+            }
+            if rows[j][i] == 'O' {
+                if j == 0 {
+                    j += 1;
+                    continue;
+                }
+                let mut k = j;
+                while k > 0 && rows[k - 1][i] == '.' {
+                    k -= 1;
+                }
+                rows[j][i] = '.';
+                rows[k][i] = 'O';
+                //println!("{:2} swap: {} -> {}", i, j, k);
+                //rows.iter().for_each(|r| { println!("{:?}", r); });
+            }
+            j += 1;
+        }
+    }
+
+    let res = rows.iter().enumerate()
+        .map(|(i, r)| {
+            let s: u64 = r.iter()
+                .filter_map(|c| {
+                    match c {
+                        'O' => Some(1),
+                        _ => None,
+                    }
+                })
+                .sum();
+            s * (height - i) as u64
+        })
+        .sum();
+    Ok(res)
+}
+
+fn run_part2(input: &str) -> Result<u64, Box<dyn Error>> {
+    println!("Running day14 - part 2");
+    let res = 0;
+    Ok(res)
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    static TEXT_INPUT: &str = "\
+O....#....
+O.OO#....#
+.....##...
+OO.#O....O
+.O.....O#.
+O.#..O.#.#
+..O..#O..O
+.......O..
+#....###..
+#OO..#....";
+
+    #[test]
+    fn day14_part1() {
+        let res = run_part1(TEXT_INPUT);
+        assert_eq!(136, res.unwrap());
+    }
+
+    #[test]
+    fn day14_part2() {
+        let res = run_part2(TEXT_INPUT);
+        assert_eq!(0, res.unwrap());
+    }
+}
index 88d4d8e7190da3967d4c37d999735bb413d4e748..6c63e781f6269713f3fe028a9c92babe11dc74e7 100644 (file)
@@ -14,6 +14,7 @@ pub mod day08;
 pub mod day09;
 pub mod day10;
 pub mod day11;
+pub mod day14;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -43,6 +44,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box<dyn Error>> {
         "day09" => day09::run(input_file)?,
         "day10" => day10::run(input_file)?,
         "day11" => day11::run(input_file)?,
+        "day14" => day14::run(input_file)?,
         _ => return Err(format!("unknown day \"{day}\"").into()),
     }
     Ok(())