day 10 in under the wire

This commit is contained in:
Shoofle 2023-12-10 23:57:23 -05:00
parent a5b5bcfc3b
commit daedef27ac
124 changed files with 261 additions and 0 deletions

7
day10/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 = "day10"
version = "0.1.0"

8
day10/Cargo.toml Normal file
View File

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

233
day10/src/main.rs Normal file
View File

@ -0,0 +1,233 @@
use std::iter::*;
use std::fs;
use std::env;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Coord {x: i32, y: i32}
#[derive(Debug, Copy, Clone, PartialEq)]
enum Dir {
North,
South,
West,
East
}
#[derive(Debug, Copy, Clone, PartialEq)]
struct Pipe {
a: Dir,
b: Dir
}
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 i32 {
return None;
}
if r.x < 0 || r.x >= self.g[0].len() as i32 {
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 i32 {
return ();
}
if r.x < 0 || r.x >= self.g[0].len() as i32 {
return ();
}
self.g[r.y as usize][r.x as usize] = value;
}
}
fn main() {
println!("Hello, AoC day 9!");
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 directions = vec![Dir::North, Dir::South, Dir::East, Dir::West];
let mut grid = Grid::<char> { g: Vec::new() };
let mut start = Coord { x: -1, y: -1};
// 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 == 'S' {
start.x = x as i32;
start.y = y as i32;
}
row.push(c);
}
grid.g.push(row);
}
let mut starting_directions = Vec::new();
// now let's figure out what pipe the starting position should be
for direction in directions {
//figure out what a return pipe would look like
let ret = opposite(direction);
//step once in that direction
if let Some(next) = grid.get(step(start, direction)) {
println!("we're at {start:?}, looking {direction:?} at {next:?}");
if let Some(candidates) = connects(next) {
if candidates.0 == ret || candidates.1 == ret {
starting_directions.push(direction);
}
}
}
}
let true_s = match (starting_directions[0], starting_directions[1]) {
(Dir::South, Dir::East) => 'F',
(Dir::East, Dir::South) => 'F',
(Dir::North, Dir::West) => 'J',
(Dir::West, Dir::North) => 'J',
(Dir::North, Dir::East) => 'L',
(Dir::East, Dir::North) => 'L',
(Dir::South, Dir::West) => '7',
(Dir::West, Dir::South) => '7',
(Dir::North, Dir::South) => '|',
(Dir::South, Dir::North) => '|',
(Dir::East, Dir::West) => '-',
(Dir::West, Dir::East) => '-',
_ => 'S'
};
grid.set(start, true_s);
let mut summing_field = Grid::<i32> { g: Vec::new() };
for _y in 0..grid.g.len() {
let mut new_row = Vec::new();
for _x in 0..grid.g[0].len() {
new_row.push(0);
}
summing_field.g.push(new_row);
}
// now let's walk around the path and count the steps, then divide by two
let mut r = start;
let mut facing = starting_directions[0];
let mut steps = 0;
while steps == 0 || r != start {
r = step(r, facing);
// we just stepped to the new r, frmo the old r, in the direction facing
let new_facing = turn(facing, grid.get(r).expect("We were pointed in this direction. It should be fine."));
for z in 0..r.x+1 {
summing_field.g[r.y as usize][z as usize] += match facing {
Dir::North => 1,
Dir::South => -1,
Dir::East => 0,
Dir::West => 0
};
summing_field.g[r.y as usize][z as usize] += match new_facing {
Dir::North => 1,
Dir::South => -1,
Dir::East => 0,
Dir::West => 0
};
}
facing = new_facing;
steps += 1;
}
let mut r = start;
let mut facing = starting_directions[0];
let mut steps = 0;
while steps == 0 || r != start {
r = step(r, facing);
// we just stepped to the new r, frmo the old r, in the direction facing
facing = turn(facing, grid.get(r).expect("We were pointed in this direction. It should be fine."));
summing_field.set(r, 0);
steps += 1;
}
println!("map:");
for row in grid.g {
println!("{row:?}");
}
let mut sum = 0;
println!("summing field:");
for row in summing_field.g {
println!("{row:?}");
for x in row {
sum += x;
}
}
sum = sum / 2;
println!("{steps} steps around the loop!");
let halfway = steps / 2;
println!("halfway is {halfway}!");
println!("behold, this is the sum of sums!: {sum}");
}
fn step(r: Coord, direction: Dir) -> Coord {
match direction {
Dir::North => Coord { x: r.x, y: r.y-1 },
Dir::South => Coord { x: r.x, y: r.y+1 },
Dir::West => Coord { x: r.x-1, y: r.y },
Dir::East => Coord { x: r.x+1, y: r.y },
}
}
fn turn(going: Dir, x: char) -> Dir {
match (going, x) {
(Dir::North, 'F') => Dir::East,
(Dir::West, 'F') => Dir::South,
(Dir::South, 'J') => Dir::West,
(Dir::East, 'J') => Dir::North,
(Dir::South, 'L') => Dir::East,
(Dir::West, 'L') => Dir::North,
(Dir::East, '7') => Dir::South,
(Dir::North, '7') => Dir::West,
(Dir::North, '|') => Dir::North,
(Dir::South, '|') => Dir::South,
(Dir::East, '-') => Dir::East,
(Dir::West, '-') => Dir::West,
(_, 'S') => Dir::North,
(_d, _c) => panic!("\"Just don't worry about it. This won't happen.\" -- Shoofle Dot Net")
}
}
fn connects(x: char) -> Option<(Dir, Dir)> {
match x {
'F' => Some((Dir::South, Dir::East)),
'J' => Some((Dir::North, Dir::West)),
'L' => Some((Dir::North, Dir::East)),
'7' => Some((Dir::South, Dir::West)),
'|' => Some((Dir::North, Dir::South)),
'-' => Some((Dir::East, Dir::West)),
_ => None
}
}
fn opposite(x: Dir) -> Dir {
match x {
Dir::North => Dir::South,
Dir::South => Dir::North,
Dir::East => Dir::West,
Dir::West => Dir::East,
}
}

View File

@ -0,0 +1 @@
{"rustc_fingerprint":11672801278650259402,"outputs":{"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":""},"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":""}},"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 @@
dd078a3fb23733e6

View File

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

View File

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

BIN
day10/target/debug/day10 Executable file

Binary file not shown.

View File

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

Binary file not shown.

View File

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

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