This commit is contained in:
Lea 2023-12-12 22:01:16 +01:00
parent 1054ed64d5
commit cafdfd324c
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
7 changed files with 1154 additions and 0 deletions

14
Cargo.lock generated
View file

@ -38,6 +38,20 @@ version = "0.1.0"
name = "day_11-2" name = "day_11-2"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "day_12-1"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "day_12-2"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]] [[package]]
name = "day_2-1" name = "day_2-1"
version = "0.1.0" version = "0.1.0"

1000
day_12/input copy.txt Normal file

File diff suppressed because it is too large Load diff

6
day_12/input.txt Normal file
View file

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1

9
day_12/part_1/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "day_12-1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.10.2"

40
day_12/part_1/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use regex::Regex;
fn main() {
let input = include_str!("../../input.txt").lines();
let regex = Regex::new("#+").unwrap();
let mut result = 0;
for line in input {
let mut parts = line.split(" ");
let field = parts.next().unwrap().split("").filter(|i| i.len() > 0).collect::<Vec<&str>>();
let instructions = parts.next().unwrap().split(",").map(|i| i.parse::<i32>().unwrap()).collect::<Vec<i32>>();
println!("{:?} {:?}", field, instructions);
let unknown_count = field.iter().filter(|i| i == &&"?").count();
for i in 0..i32::pow(2, unknown_count as u32) {
let mut current_field = field.clone();
let mut pos = 0;
for (j, spring) in current_field.clone().iter().enumerate() {
if spring == &"?" {
current_field[j] = if i & 1 << pos > 0 { "#" } else { "." };
pos += 1;
}
}
let field_str = current_field.join("");
let pairs = regex
.find_iter(field_str.as_str())
.map(|m| m.as_str().len() as i32)
.collect::<Vec<i32>>();
if pairs.len() == instructions.len() && pairs.iter().zip(&instructions).filter(|(a, b)| a != b).count() == 0 {
result += 1;
}
}
}
println!("Result: {result}");
}

9
day_12/part_2/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "day_12-2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.10.2"

76
day_12/part_2/src/main.rs Normal file
View file

@ -0,0 +1,76 @@
use std::{thread::{self, JoinHandle}, time::Instant};
use regex::Regex;
// i could just figure out a proper way but bruteforce is funnier
// but also its still way too slow so ill fix it tomorrow maybe
fn main() {
let start = Instant::now();
let input_orig = include_str!("../../input.txt").lines();
let mut inputs: Vec<Vec<&str>> = vec![];
let threads = input_orig.clone().count();
for i in 0..threads {
let lines = input_orig.clone().skip(i).step_by(threads).collect::<Vec<&str>>();
inputs.push(lines);
}
let mut handles: Vec<JoinHandle<i128>> = vec![];
for input in inputs {
let handle = thread::spawn(|| {
let thread_start = Instant::now();
let mut result = 0;
let regex = Regex::new("#+").unwrap();
for line in input {
let mut parts = line.split(" ");
// i fucking hate rust fuck this ownership garbage what is this
let field_in = parts.next().unwrap();
let field_str = format!("{field_in}?{field_in}?{field_in}?{field_in}?{field_in}");
let instructions_in = parts.next().unwrap();
let instructions_str = format!("{instructions_in},{instructions_in},{instructions_in},{instructions_in},{instructions_in}");
let field = field_str.split("").filter(|i| i.len() > 0).collect::<Vec<&str>>();
let instructions = instructions_str.split(",").map(|i| i.parse::<i128>().unwrap()).collect::<Vec<i128>>();
// println!("{:?} {:?}", field, instructions);
let unknown_count = field.iter().filter(|i| i == &&"?").count();
for i in 0..i128::pow(2, unknown_count as u32) {
let mut current_field = field.clone();
let mut pos = 0;
for (j, spring) in current_field.clone().iter().enumerate() {
if spring == &"?" {
current_field[j] = if i & 1 << pos > 0 { "#" } else { "." };
pos += 1;
}
}
let field_str = current_field.join("");
let pairs = regex
.find_iter(field_str.as_str())
.map(|m| m.as_str().len() as i128)
.collect::<Vec<i128>>();
if pairs.len() == instructions.len() && pairs.iter().zip(&instructions).filter(|(a, b)| a != b).count() == 0 {
result += 1;
}
}
}
println!("Thread result: {result} (Thread finished in {} seconds)", thread_start.elapsed().as_secs());
return result;
});
handles.push(handle);
}
let mut res = 0;
for handle in handles {
res += handle.join().unwrap();
}
println!("Final result: {} (After {} seconds)", res, start.elapsed().as_secs());
}