This commit is contained in:
BlackDemonFire 2023-12-03 22:21:32 +01:00
parent 8d235ecbc4
commit ac7a1925a5
Signed by: lucy
SSH key fingerprint: SHA256:LOoXGgXjn6B8CcfqWHkd2Y5gFaffr0b2tAzBKlEG6Xc
6 changed files with 174 additions and 0 deletions

8
Cargo.lock generated
View file

@ -17,3 +17,11 @@ version = "0.1.0"
[[package]] [[package]]
name = "d02t2" name = "d02t2"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "d03t1"
version = "0.1.0"
[[package]]
name = "d03t2"
version = "0.1.0"

View file

@ -6,4 +6,6 @@ members = [
"d01t2", "d01t2",
"d02t1", "d02t1",
"d02t2", "d02t2",
"d03t1",
"d03t2",
] ]

8
d03t1/Cargo.toml Normal file
View file

@ -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]

73
d03t1/src/main.rs Normal file
View file

@ -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::<Vec<_>>();
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::<Vec<_>>();
let mut cur_num_str = VecDeque::<char>::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::<String>()
.parse::<usize>()
.unwrap();
}
}
}
}
}
println!("d03t1: {result}");
}

8
d03t2/Cargo.toml Normal file
View file

@ -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]

75
d03t2/src/main.rs Normal file
View file

@ -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::<Vec<_>>();
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::<usize>::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::<Vec<_>>();
let mut cur_num_str = VecDeque::<char>::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::<String>()
.parse::<usize>()
.unwrap(),
);
}
}
}
if nums.len() == 2 {
result += nums[0] * nums[1];
}
}
}
println!("d03t2: {result}");
}