not the prettiest but i got it done. i figured out the right approach by part 2

This commit is contained in:
Shoofle 2023-12-11 01:22:47 -05:00
parent daedef27ac
commit e4e8e41dfa
137 changed files with 214 additions and 0 deletions

7
day11/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day11"
version = "0.1.0"

8
day11/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[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]

183
day11/src/main.rs Normal file
View File

@ -0,0 +1,183 @@
use std::iter::*;
use std::fs;
use std::env;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Coord {x: i64, y: i64}
fn pt(x:i64, y:i64) -> Coord { Coord { x, y } }
struct Grid<T: Copy> { g: Vec<Vec<T>> }
impl<T: Copy> Grid<T> {
fn get(&self, r: Coord) -> Option<T> {
if r.y < 0 || r.y >= self.g.len() as i64 {
return None;
}
if r.x < 0 || r.x >= self.g[0].len() as i64 {
return None;
}
Some(self.g[r.y as usize][r.x as usize])
}
fn set(&mut self, r: Coord, value: T) {
if r.y < 0 || r.y >= self.g.len() as i64 {
return ();
}
if r.x < 0 || r.x >= self.g[0].len() as i64 {
return ();
}
self.g[r.y as usize][r.x as usize] = value;
}
}
fn main() {
println!("Hello, AoC day 11!");
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("wrong number of arguments!");
std::process::exit(1);
}
let file_path = &args[1];
let contents = fs::read_to_string(file_path)
.expect("Should have been able to read the file");
let mut grid = Grid::<char> { g: Vec::new() };
let mut rows_to_expand: Vec<bool> = Vec::new();
let mut columns_to_expand: Vec<bool> = Vec::new();
for _y in contents.lines() {
rows_to_expand.push(true);
}
for _x in contents.lines().nth(0).expect("there's gotta be a first line").chars() {
columns_to_expand.push(true);
}
// let's build that grid
for (y, line) in contents.lines().enumerate() {
let mut row = Vec::new();
for (x, c) in line.chars().enumerate() {
if c != '.' {
rows_to_expand[y] = false;
columns_to_expand[x] = false;
}
row.push(c);
}
grid.g.push(row);
}
println!("starting grid:");
print_map(&grid);
println!("expanding these:");
println!("rows: {rows_to_expand:?}");
println!("columns: {columns_to_expand:?}");
let mut offset=0;
for (y, expanding) in rows_to_expand.iter().enumerate() {
if !expanding { continue; }
let mut newline: Vec<char> = Vec::new();
for c in grid.g[y+offset].iter() { newline.push(*c); }
grid.g.insert(y+offset, newline);
offset += 1;
}
offset=0;
for (x, expanding) in columns_to_expand.iter().enumerate() {
if !expanding { continue; }
for (_y, row) in grid.g.iter_mut().enumerate() {
row.insert(x + offset, '.');
}
offset += 1;
}
println!("map:");
print_map(&grid);
let mut galaxies: Vec<Coord> = Vec::new();
for (y, row) in grid.g.iter().enumerate() {
for (x, character) in row.iter().enumerate() {
if *character == '#' { galaxies.push(pt(x as i64,y as i64)); }
}
}
println!("galaxy list: {galaxies:?}");
let mut sum = 0;
for a in galaxies.iter() {
for b in galaxies.iter() {
sum += (b.x-a.x).abs() + (b.y-a.y).abs();
}
}
sum = sum / 2;
println!("behold, this is the sum of sums!: {sum}");
println!("okay let's do that again for part 2, but differently.");
let mut grid2 = Grid::<char> { g: Vec::new() };
let mut galaxies = Vec::new();
let dilation_factor = 999_999;
let mut rows_to_expand = Vec::new();
let mut columns_to_expand = Vec::new();
for _y in contents.lines() {
rows_to_expand.push(true);
}
for _x in contents.lines().nth(0).expect("there's gotta be a first line").chars() {
columns_to_expand.push(true);
}
// let's start this over.
for (y, line) in contents.lines().enumerate() {
let mut row = Vec::new();
for (x, c) in line.chars().enumerate() {
if c == '#' {
rows_to_expand[y] = false;
columns_to_expand[x] = false;
galaxies.push(pt(x as i64,y as i64));
}
row.push(c);
}
grid2.g.push(row);
}
for g in galaxies.iter_mut() {
let mut expansions = 0;
for (y, expanding) in rows_to_expand.iter().enumerate() {
if !expanding { continue; }
if g.y > y as i64 { expansions += dilation_factor; }
}
g.y += expansions;
expansions = 0;
for (x, expanding) in columns_to_expand.iter().enumerate() {
if !expanding { continue; }
if g.x > x as i64 { expansions += dilation_factor; }
}
g.x += expansions;
}
let mut sum: i128 = 0;
for a in galaxies.iter() {
for b in galaxies.iter() {
sum += ((b.x-a.x).abs() + (b.y-a.y).abs()) as i128;
}
}
sum = sum / 2;
println!("behold, this is the sum of sums!: {sum}");
}
fn print_map(grid: &Grid<char>) {
for row in grid.g.iter() {
let line: Vec<String> = row.iter().map(|x| x.to_string()).collect();
let l = line.join("");
println!("{l}");
}
}

View File

@ -0,0 +1 @@
{"rustc_fingerprint":11672801278650259402,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/shoofle/.rustup/toolchains/stable-x86_64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.69.0 (84c898d65 2023-04-16)\nbinary: rustc\ncommit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\ncommit-date: 2023-04-16\nhost: x86_64-apple-darwin\nrelease: 1.69.0\nLLVM version: 15.0.7\n","stderr":""}},"successes":{}}

View File

@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/

View File

View File

@ -0,0 +1 @@
f609fbbae361eb07

View File

@ -0,0 +1 @@
{"rustc":3659767333214291318,"features":"[]","target":3176960092119891533,"profile":11736316127369858332,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/day11-e3eea56213de9d74/dep-bin-day11"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}

View File

@ -0,0 +1 @@
This file has an mtime of when this was started.

View File

@ -0,0 +1,3 @@
{"message":"method `get` is never used","code":{"code":"dead_code","explanation":null},"level":"warning","spans":[{"file_name":"src/main.rs","byte_start":238,"byte_end":241,"line_start":12,"line_end":12,"column_start":8,"column_end":11,"is_primary":true,"text":[{"text":" fn get(&self, r: Coord) -> Option<T> {","highlight_start":8,"highlight_end":11}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`#[warn(dead_code)]` on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: method `get` is never used\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:12:8\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m12\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn get(&self, r: Coord) -> Option<T> {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: `#[warn(dead_code)]` on by default\u001b[0m\n\n"}
{"message":"method `set` is never used","code":{"code":"dead_code","explanation":null},"level":"warning","spans":[{"file_name":"src/main.rs","byte_start":511,"byte_end":514,"line_start":21,"line_end":21,"column_start":8,"column_end":11,"is_primary":true,"text":[{"text":" fn set(&mut self, r: Coord, value: T) {","highlight_start":8,"highlight_end":11}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: method `set` is never used\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/main.rs:21:8\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m21\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m fn set(&mut self, r: Coord, value: T) {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^\u001b[0m\n\n"}
{"message":"2 warnings emitted","code":null,"level":"warning","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: 2 warnings emitted\u001b[0m\n\n"}

BIN
day11/target/debug/day11 Executable file

Binary file not shown.

View File

@ -0,0 +1 @@
/Users/shoofle/Projects/aoc_2023/day11/target/debug/day11: /Users/shoofle/Projects/aoc_2023/day11/src/main.rs

Binary file not shown.

View File

@ -0,0 +1,5 @@
/Users/shoofle/Projects/aoc_2023/day11/target/debug/deps/day11-e3eea56213de9d74: src/main.rs
/Users/shoofle/Projects/aoc_2023/day11/target/debug/deps/day11-e3eea56213de9d74.d: src/main.rs
src/main.rs:

Some files were not shown because too many files have changed in this diff Show More