diff --git a/.gitea/workflows/build_and_run.yml b/.gitea/workflows/build_and_run.yml index 0cf847d..a76732d 100644 --- a/.gitea/workflows/build_and_run.yml +++ b/.gitea/workflows/build_and_run.yml @@ -11,7 +11,7 @@ jobs: name: Challenge for day strategy: matrix: - day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] + day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] runs-on: rust-bookworm steps: - name: Check out repository code diff --git a/day25/Cargo.toml b/day25/Cargo.toml new file mode 100644 index 0000000..e5f4cc3 --- /dev/null +++ b/day25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day25" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day25/src/main.rs b/day25/src/main.rs new file mode 100644 index 0000000..6577835 --- /dev/null +++ b/day25/src/main.rs @@ -0,0 +1,68 @@ +use std::{env, fs}; + +fn read_input(path: &str) -> String { + fs::read_to_string(path).expect("Cannot read file.") +} + +fn parse_input(input: &str) -> (Vec>, Vec>) { + let mut keys = Vec::new(); + let mut locks = Vec::new(); + + let mut height = 0; + let mut x = vec![5; 5]; + let mut is_key = false; + for line in input.lines() { + if line.is_empty() { + height = 0; + continue; + } + + if height == 0 { + is_key = line.starts_with("."); + } else if height == 6 { + if is_key { + keys.push(x.iter().map(|i| 5 - i).collect()); + } else { + locks.push(x); + } + x = vec![5; 5]; + } else { + for (i, c) in line.chars().enumerate() { + if x[i] == 5 && ((is_key && c == '#') || (!is_key && c == '.')) { + x[i] = height - 1; + } + } + } + height += 1; + } + + (keys, locks) +} + +fn part_1(keys: &Vec>, locks: &Vec>) -> u32 { + let mut combinations = 0; + for key in keys { + for lock in locks { + for i in 0..5 { + if key[i] + lock[i] > 5 { + break; + } + if i == 4 { + combinations += 1; + } + } + } + } + + combinations +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let input = read_input(&arg); + let (keys, locks) = parse_input(&input); + println!("[{}]", &arg); + println!("\t[Part 1] => Answer is '{}'.", part_1(&keys, &locks)); + } +}