diff --git a/old/day11/Cargo.lock b/old/day11/Cargo.lock deleted file mode 100644 index 4ab9be8..0000000 --- a/old/day11/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "day11" -version = "0.1.0" diff --git a/old/day11/Cargo.toml b/old/day11/Cargo.toml deleted file mode 100644 index 8f5b9a5..0000000 --- a/old/day11/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "day11" -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/old/day11/src/main.rs b/old/day11/src/main.rs deleted file mode 100644 index 2c1423b..0000000 --- a/old/day11/src/main.rs +++ /dev/null @@ -1,121 +0,0 @@ -use std::fs; - -fn main() { - let input = fs::read_to_string("input.txt").unwrap(); - let input: Vec<_> = input.split('\n') - .map(|x| x.chars().collect::>()) - .collect(); - - let mut y_expand: Vec = vec![]; - for line in &input { - if line.iter().all(|char| char == &'.') { - y_expand.push(1); - } else { - y_expand.push(0); - } - } - - let mut x_expand: Vec = vec![]; - for j in 0..input[0].len() { - let mut is_empty = true; - for i in 0..input.len() { - if input[i][j] != '.' { - is_empty = false; - } - } - if is_empty { - x_expand.push(1); - } else { - x_expand.push(0); - } - } - - // println!("{:?}", x_expand); - - let mut galaxies: Vec<(i64, i64)> = vec![]; - - let mut y_offset: i64 = 0; - - for (i, line) in input.iter().enumerate() { - if y_expand[i] == 1 { - y_offset += 1000000 - 1; - } - let mut x_offset: i64 = 0; - - for (j, char) in line.iter().enumerate() { - if x_expand[j] == 1 { - x_offset += 1000000 - 1; - } - if char == &'#' { - galaxies.push((i as i64 + y_offset, j as i64 + x_offset)); - } - } - } - - let dist_total = galaxies.clone().into_iter().enumerate() - .fold(0, |mut acc, (i, gal)| { - for next in &galaxies[i + 1..] { - acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); - } - acc - }); - - println!("{:?}", dist_total); - -} - -// use std::fs; - -// fn main() { -// let input = fs::read_to_string("input.txt").unwrap(); -// let input: Vec<_> = input.split('\n') -// .map(|x| x.chars().collect::>()) -// .collect(); - -// let mut rotate: Vec> = vec![]; -// for j in 0..input[0].len() { -// let mut tmp: Vec = vec![]; -// for i in 0..input.len() { -// tmp.push(input[i][j]); -// } - -// if tmp.iter().all(|x| x == &'.') { -// rotate.push(tmp.clone()); -// } -// rotate.push(tmp); -// } - - -// let mut expanded: Vec> = vec![]; -// for j in 0..rotate[0].len() { -// let mut tmp: Vec = vec![]; -// for i in 0..rotate.len() { -// tmp.push(rotate[i][j]); -// } -// if tmp.iter().all(|x| x == &'.') { -// expanded.push(tmp.clone()); -// } -// expanded.push(tmp); -// } - -// let mut galaxies: Vec<(i32, i32)> = vec![]; - -// for (i, line) in expanded.iter().enumerate() { -// for (j, char) in line.iter().enumerate() { -// if char == &'#' { -// galaxies.push((i as i32,j as i32)); -// } -// } -// } - -// let dist_total = galaxies.clone().into_iter().enumerate() -// .fold(0, |mut acc, (i, gal)| { -// for next in &galaxies[i + 1..] { -// acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); -// } -// acc -// }); - -// println!("{:?}", dist_total); - -// } \ No newline at end of file diff --git a/old/day12/Cargo.lock b/old/day12/Cargo.lock deleted file mode 100644 index 5f2a393..0000000 --- a/old/day12/Cargo.lock +++ /dev/null @@ -1,25 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "day12" -version = "0.1.0" -dependencies = [ - "itertools", -] - -[[package]] -name = "either" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" - -[[package]] -name = "itertools" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" -dependencies = [ - "either", -] diff --git a/old/day12/Cargo.toml b/old/day12/Cargo.toml deleted file mode 100644 index caf7178..0000000 --- a/old/day12/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "day12" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -itertools = "0.12.0" diff --git a/old/day12/src/main.rs b/old/day12/src/main.rs deleted file mode 100644 index 84d7474..0000000 --- a/old/day12/src/main.rs +++ /dev/null @@ -1,54 +0,0 @@ -use std::fs; - -use itertools::Itertools; - -fn main() { - let input = parse_input(); - - for (springs, groups) in input { - println!("{:?} {:?}", springs, groups); - } -} - -fn parse_input() -> Vec<(Vec, Vec)> { - let input = fs::read_to_string("input.txt").unwrap(); - let input: Vec<(Vec, Vec)> = input.split('\n') - .map(|line| { - line.split(' ') - .collect_tuple() - .unwrap() - }).map(|(springs, groups)| { - let springs: Vec = springs.chars() - .map(|char| { - char.to_condition() - }).collect(); - - let groups: Vec<_> = groups.split(',') - .map(|num| num.parse::().expect("Failed to parse group len")) - .collect(); - (springs, groups) - }).collect(); - input -} - -#[derive(Debug)] -enum Condition { - Good, - Bad, - WhoKnows -} - -trait ConditionConvertable { - fn to_condition(self) -> Condition; -} - -impl ConditionConvertable for char { - fn to_condition(self) -> Condition { - match self { - '.' => Condition::Good, - '#' => Condition::Bad, - '?' => Condition::WhoKnows, - _ => panic!("Invalid spring char AHHH") - } - } -} diff --git a/src/day11.rs b/src/day11.rs new file mode 100644 index 0000000..7e4bb57 --- /dev/null +++ b/src/day11.rs @@ -0,0 +1,144 @@ +use aoc_runner_derive::{aoc, aoc_generator}; +#[aoc_generator(day11)] +fn parse(input: &str) -> Vec> { + let input: Vec<_> = input.split('\n') + .map(|x| x.chars().collect::>()) + .collect(); + + input +} + +#[aoc(day11, part1)] +fn part1(input: &Vec>) -> u32 { + let mut rotate: Vec> = vec![]; + for j in 0..input[0].len() { + let mut tmp: Vec = vec![]; + for i in 0..input.len() { + tmp.push(input[i][j]); + } + + if tmp.iter().all(|x| x == &'.') { + rotate.push(tmp.clone()); + } + rotate.push(tmp); + } + + + let mut expanded: Vec> = vec![]; + for j in 0..rotate[0].len() { + let mut tmp: Vec = vec![]; + for i in 0..rotate.len() { + tmp.push(rotate[i][j]); + } + if tmp.iter().all(|x| x == &'.') { + expanded.push(tmp.clone()); + } + expanded.push(tmp); + } + + let mut galaxies: Vec<(i32, i32)> = vec![]; + + for (i, line) in expanded.iter().enumerate() { + for (j, char) in line.iter().enumerate() { + if char == &'#' { + galaxies.push((i as i32,j as i32)); + } + } + } + + let dist_total = galaxies.clone().into_iter().enumerate() + .fold(0, |mut acc, (i, gal)| { + for next in &galaxies[i + 1..] { + acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); + } + acc + }); + + dist_total +} + +#[aoc(day11, part2)] +fn part2(input: &Vec>) -> u64 { + let mut y_expand: Vec = vec![]; + for line in input { + if line.iter().all(|char| char == &'.') { + y_expand.push(1); + } else { + y_expand.push(0); + } + } + + let mut x_expand: Vec = vec![]; + for j in 0..input[0].len() { + let mut is_empty = true; + for i in 0..input.len() { + if input[i][j] != '.' { + is_empty = false; + } + } + if is_empty { + x_expand.push(1); + } else { + x_expand.push(0); + } + } + + // println!("{:?}", x_expand); + + let mut galaxies: Vec<(i64, i64)> = vec![]; + + let mut y_offset: i64 = 0; + + for (i, line) in input.iter().enumerate() { + if y_expand[i] == 1 { + y_offset += 1000000 - 1; + } + let mut x_offset: i64 = 0; + + for (j, char) in line.iter().enumerate() { + if x_expand[j] == 1 { + x_offset += 1000000 - 1; + } + if char == &'#' { + galaxies.push((i as i64 + y_offset, j as i64 + x_offset)); + } + } + } + + let dist_total = galaxies.clone().into_iter().enumerate() + .fold(0, |mut acc, (i, gal)| { + for next in &galaxies[i + 1..] { + acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); + } + acc + }); + + dist_total +} + + +#[cfg(test)] +mod tests { + use super::*; + + const EX: &str = r"...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#....."; + + #[test] + fn part1_example() { + assert_eq!(part1(&parse(EX)), 374); + } + + // #[test] + // fn part2_example() { + // assert_eq!(part2(&parse(EX)), ""); + // } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 1a36258..ece7c82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,15 @@ +mod day14; +mod day13; +mod day12; +mod day11; mod day10; mod day9; mod day8; mod day7; mod day6; mod day5; -mod day3; -mod day14; -mod day13; -mod day12; mod day4; +mod day3; mod day2; mod day1;