Added day 7 both parts
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/target
|
||||
**/test.txt
|
||||
**/input.txt
|
||||
**/input.txt
|
||||
**/*.txt
|
||||
160
src/day7/mod.rs
Normal file
160
src/day7/mod.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
use std::{collections::HashMap, fs::File, io::Read};
|
||||
|
||||
pub fn part_1(file:&str) -> u64 {
|
||||
let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day7/".to_owned();
|
||||
directory.push_str(file);
|
||||
let mut handle = File::open(directory).unwrap();
|
||||
let mut file = String::new();
|
||||
handle.read_to_string(&mut file).unwrap();
|
||||
|
||||
let mut tachyon_map: Vec<Vec<ManifoldPoint>> = Vec::new();
|
||||
let mut start_col = 0;
|
||||
|
||||
for row in file.lines() {
|
||||
let mut rowvec = Vec::new();
|
||||
for (pos, col) in row.char_indices() {
|
||||
let point = match col {
|
||||
'.' => ManifoldPoint::Empty,
|
||||
'^' => ManifoldPoint::Splitter(false),
|
||||
'S' => {
|
||||
start_col = pos;
|
||||
ManifoldPoint::Start
|
||||
},
|
||||
_ => panic!("Invalid Char"),
|
||||
};
|
||||
rowvec.push(point);
|
||||
}
|
||||
tachyon_map.push(rowvec);
|
||||
}
|
||||
|
||||
first_part_recursion(&mut tachyon_map, 0, start_col);
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
for row in tachyon_map {
|
||||
for col in row {
|
||||
match col {
|
||||
ManifoldPoint::Splitter(visited) => {
|
||||
if visited {
|
||||
count += 1
|
||||
}
|
||||
},
|
||||
_ => continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
fn first_part_recursion(map: &mut Vec<Vec<ManifoldPoint>>, row: usize, col: usize) {
|
||||
if row + 1 == map.len() {
|
||||
return;
|
||||
}
|
||||
let current_point = &mut map[row][col];
|
||||
|
||||
match current_point {
|
||||
ManifoldPoint::Splitter(visited) => {
|
||||
if !*visited {
|
||||
map[row][col] = ManifoldPoint::Splitter(true);
|
||||
first_part_recursion(map, row + 1, col - 1);
|
||||
first_part_recursion(map, row + 1, col + 1);
|
||||
}
|
||||
},
|
||||
_ => first_part_recursion(map, row + 1, col)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn part_2(file:&str) -> u64 {
|
||||
let mut directory = "/home/jason/Programming/Rust/aoc2025/src/day7/".to_owned();
|
||||
directory.push_str(file);
|
||||
let mut handle = File::open(directory).unwrap();
|
||||
let mut file = String::new();
|
||||
handle.read_to_string(&mut file).unwrap();
|
||||
|
||||
let mut tachyon_map: Vec<Vec<ManifoldPoint>> = Vec::new();
|
||||
let mut start_col = 0;
|
||||
|
||||
for row in file.lines() {
|
||||
let mut rowvec = Vec::new();
|
||||
for (pos, col) in row.char_indices() {
|
||||
let point = match col {
|
||||
'.' => ManifoldPoint::Empty,
|
||||
'^' => ManifoldPoint::Splitter(false),
|
||||
'S' => {
|
||||
start_col = pos;
|
||||
ManifoldPoint::Start
|
||||
},
|
||||
_ => panic!("Invalid Char"),
|
||||
};
|
||||
rowvec.push(point);
|
||||
}
|
||||
tachyon_map.push(rowvec);
|
||||
}
|
||||
let mut memo = HashMap::new();
|
||||
|
||||
second_part_recursion(&mut tachyon_map, 0, start_col, &mut memo)
|
||||
}
|
||||
|
||||
fn second_part_recursion(map: &Vec<Vec<ManifoldPoint>>, row: usize, col: usize, memo: &mut HashMap<(usize,usize),u64>) -> u64{
|
||||
if row + 1 >= map.len() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
let timelines = match memo.get(&(row, col)) {
|
||||
Some(stored_num) => *stored_num,
|
||||
None => {
|
||||
let current_point = &map[row][col];
|
||||
|
||||
let point_value = match current_point {
|
||||
ManifoldPoint::Splitter(_) => {
|
||||
let splitl = second_part_recursion(map, row + 1, col - 1, memo);
|
||||
let splitr = second_part_recursion(map, row + 1, col + 1, memo);
|
||||
splitl + splitr
|
||||
},
|
||||
_ => second_part_recursion(map, row + 1, col, memo)
|
||||
};
|
||||
memo.insert((row, col), point_value);
|
||||
point_value
|
||||
}
|
||||
};
|
||||
timelines
|
||||
}
|
||||
|
||||
//#[derive(Debug)]
|
||||
enum ManifoldPoint {
|
||||
Empty,
|
||||
Splitter(bool),
|
||||
Start,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ManifoldPoint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Empty => write!(f, "."),
|
||||
Self::Splitter(_) => write!(f, "^"),
|
||||
Self::Start => write!(f, "S"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(part_1("test.txt"), 21);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn longer_test() {
|
||||
part_1("test2.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(part_2("test.txt"), 40);
|
||||
}
|
||||
|
||||
}
|
||||
10
src/main.rs
10
src/main.rs
@@ -1,8 +1,14 @@
|
||||
mod day6;
|
||||
mod day7;
|
||||
|
||||
fn main() {
|
||||
let d6p1 = day6::part_1("input.txt");
|
||||
/*let d6p1 = day6::part_1("input.txt");
|
||||
println!("Day 6 Part 1 Solution: {}", d6p1);
|
||||
let d6p2 = day6::part_2("input.txt");
|
||||
println!("Day 6 Part 2 Solution: {}", d6p2);
|
||||
println!("Day 6 Part 2 Solution: {}", d6p2);*/
|
||||
|
||||
let d7p1 = day7::part_1("input.txt");
|
||||
println!("Day 7 Part 1 Solution: {}", d7p1);
|
||||
let d7p2 = day7::part_2("input.txt");
|
||||
println!("Day 7 Part 2 Solution: {}", d7p2);
|
||||
}
|
||||
Reference in New Issue
Block a user