update day11

This commit is contained in:
Andrew Glaze 2023-12-18 12:16:50 -05:00
parent 4e494e5596
commit 7e082e6558
8 changed files with 149 additions and 228 deletions

7
old/day11/Cargo.lock generated
View File

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

View File

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

View File

@ -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::<Vec<char>>())
.collect();
let mut y_expand: Vec<i32> = 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<i32> = 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::<Vec<char>>())
// .collect();
// let mut rotate: Vec<Vec<char>> = vec![];
// for j in 0..input[0].len() {
// let mut tmp: Vec<char> = 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<char>> = vec![];
// for j in 0..rotate[0].len() {
// let mut tmp: Vec<char> = 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);
// }

25
old/day12/Cargo.lock generated
View File

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

View File

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

View File

@ -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<Condition>, Vec<i32>)> {
let input = fs::read_to_string("input.txt").unwrap();
let input: Vec<(Vec<Condition>, Vec<i32>)> = input.split('\n')
.map(|line| {
line.split(' ')
.collect_tuple()
.unwrap()
}).map(|(springs, groups)| {
let springs: Vec<Condition> = springs.chars()
.map(|char| {
char.to_condition()
}).collect();
let groups: Vec<_> = groups.split(',')
.map(|num| num.parse::<i32>().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")
}
}
}

144
src/day11.rs Normal file
View File

@ -0,0 +1,144 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day11)]
fn parse(input: &str) -> Vec<Vec<char>> {
let input: Vec<_> = input.split('\n')
.map(|x| x.chars().collect::<Vec<char>>())
.collect();
input
}
#[aoc(day11, part1)]
fn part1(input: &Vec<Vec<char>>) -> u32 {
let mut rotate: Vec<Vec<char>> = vec![];
for j in 0..input[0].len() {
let mut tmp: Vec<char> = 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<char>> = vec![];
for j in 0..rotate[0].len() {
let mut tmp: Vec<char> = 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<Vec<char>>) -> u64 {
let mut y_expand: Vec<i32> = 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<i32> = 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)), "<RESULT>");
// }
}

View File

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