diff --git a/Cargo.lock b/Cargo.lock index 373583b..42f652b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,3 +17,11 @@ version = "0.1.0" [[package]] name = "d02t2" version = "0.1.0" + +[[package]] +name = "d03t1" +version = "0.1.0" + +[[package]] +name = "d03t2" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index f32aa6a..564cf56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ members = [ "d01t2", "d02t1", "d02t2", + "d03t1", + "d03t2", ] diff --git a/d03t1/Cargo.toml b/d03t1/Cargo.toml new file mode 100644 index 0000000..c4c07fa --- /dev/null +++ b/d03t1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d03t1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d03t1/src/main.rs b/d03t1/src/main.rs new file mode 100644 index 0000000..45a4461 --- /dev/null +++ b/d03t1/src/main.rs @@ -0,0 +1,73 @@ +use std::collections::{HashSet, VecDeque}; + +fn dec_if_pos(input: usize) -> usize { + if input > 0 { + return input - 1; + } else { + return input; + } +} + +fn inc_if_lt(input: usize, max: usize) -> usize { + if input < max { + return input + 1; + } else { + return input; + } +} + +fn main() { + let input = include_str!("../input.txt"); + let mut result = 0; + let lines = input.lines().collect::>(); + let total_lines = lines.len(); + let mut checked_fields: HashSet<(usize, usize)> = HashSet::new(); + for (line_idx, line) in input.lines().enumerate() { + for (char_idx, _char) in line + .chars() + .enumerate() + .filter(|(_, char)| !char.is_ascii_digit() && *char != '.') + { + for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) { + for y in dec_if_pos(line_idx)..=inc_if_lt(line_idx, total_lines) { + let cur_line = lines[y]; + let line_chars = cur_line.chars().collect::>(); + let mut cur_num_str = VecDeque::::new(); + if line_chars[x].is_ascii_digit() { + if checked_fields.contains(&(x, y)) { + continue; + } + checked_fields.insert((x, y)); + cur_num_str.push_back(line_chars[x]); + if x > 0 { + for i in (0..=(x - 1)).rev() { + if line_chars[i].is_ascii_digit() { + cur_num_str.push_front(line_chars[i]); + checked_fields.insert((i, y)); + } else { + break; + } + } + } + if x < line.len() { + for i in (x + 1)..=(line.len() - 1) { + if line_chars[i].is_ascii_digit() { + cur_num_str.push_back(line_chars[i]); + checked_fields.insert((i, y)); + } else { + break; + } + } + } + result += cur_num_str + .into_iter() + .collect::() + .parse::() + .unwrap(); + } + } + } + } + } + println!("d03t1: {result}"); +} diff --git a/d03t2/Cargo.toml b/d03t2/Cargo.toml new file mode 100644 index 0000000..1675264 --- /dev/null +++ b/d03t2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d03t2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d03t2/src/main.rs b/d03t2/src/main.rs new file mode 100644 index 0000000..ecd7a84 --- /dev/null +++ b/d03t2/src/main.rs @@ -0,0 +1,75 @@ +use std::collections::{HashSet, VecDeque}; + +fn dec_if_pos(input: usize) -> usize { + if input > 0 { + return input - 1; + } else { + return input; + } +} + +fn inc_if_lt(input: usize, max: usize) -> usize { + if input < max { + return input + 1; + } else { + return input; + } +} + +fn main() { + let input = include_str!("../input.txt"); + let mut result = 0; + let lines = input.lines().collect::>(); + let total_lines = lines.len(); + let mut checked_fields: HashSet<(usize, usize)> = HashSet::new(); + for (line_idx, line) in input.lines().enumerate() { + for (char_idx, _char) in line.chars().enumerate().filter(|(_, char)| *char == '*') { + let mut nums = Vec::::new(); + for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) { + for y in dec_if_pos(line_idx)..=inc_if_lt(line_idx, total_lines) { + let cur_line = lines[y]; + let line_chars = cur_line.chars().collect::>(); + let mut cur_num_str = VecDeque::::new(); + if line_chars[x].is_ascii_digit() { + if checked_fields.contains(&(x, y)) { + continue; + } + checked_fields.insert((x, y)); + cur_num_str.push_back(line_chars[x]); + if x > 0 { + for i in (0..=(x - 1)).rev() { + if line_chars[i].is_ascii_digit() { + cur_num_str.push_front(line_chars[i]); + checked_fields.insert((i, y)); + } else { + break; + } + } + } + if x < line.len() { + for i in (x + 1)..=(line.len() - 1) { + if line_chars[i].is_ascii_digit() { + cur_num_str.push_back(line_chars[i]); + checked_fields.insert((i, y)); + } else { + break; + } + } + } + nums.push( + cur_num_str + .into_iter() + .collect::() + .parse::() + .unwrap(), + ); + } + } + } + if nums.len() == 2 { + result += nums[0] * nums[1]; + } + } + } + println!("d03t2: {result}"); +}