Skip to content

Commit

Permalink
[LIB] Move aoc_2022 to global aoc_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 29, 2023
1 parent de3432b commit 7fac085
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 321 deletions.
11 changes: 5 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion aoc_2022/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ common = { path = "../common" }
derive_more = "0.99.17"
hashbrown = "0.13.1"
indoc = "2.0.4"
nd_vec = "0.3.0"
# nd_vec = "0.4.0"
nd_vec = { git = "https://github.com/Basicprogrammer10/nd-vec" }
num-traits = "0.2.15"
pathfinding = "4.3.3"
petgraph = "0.6.2"
Expand Down
59 changes: 0 additions & 59 deletions aoc_2022/src/aoc_lib/matrix.rs

This file was deleted.

7 changes: 0 additions & 7 deletions aoc_2022/src/aoc_lib/mod.rs

This file was deleted.

55 changes: 0 additions & 55 deletions aoc_2022/src/aoc_lib/point.rs

This file was deleted.

33 changes: 0 additions & 33 deletions aoc_2022/src/aoc_lib/point3.rs

This file was deleted.

18 changes: 9 additions & 9 deletions aoc_2022/src/day_09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::vec;

use hashbrown::HashSet;

use crate::aoc_lib;
use common::{Answer, Solution};
use nd_vec::vector;

type Point = aoc_lib::Point<i32>;
type Point = nd_vec::Vec2<i32>;

pub struct Day09;

Expand All @@ -26,7 +26,7 @@ impl Solution for Day09 {
fn process(raw: &str, count: usize) -> usize {
let orders = raw.lines().map(from_line).collect::<Vec<_>>();
let mut tail_pios = HashSet::new();
let mut knots = vec![Point::new(0, 0); count + 1];
let mut knots = vec![vector!(0, 0); count + 1];

tail_pios.insert(*knots.last().unwrap());
for (dir, count) in orders {
Expand All @@ -35,11 +35,11 @@ fn process(raw: &str, count: usize) -> usize {

for i in 1..knots.len() {
let diff = knots[i - 1] - knots[i];
if diff.abs().max_value() <= 1 {
if diff.abs().max_component() <= 1 {
continue;
}

knots[i] += diff.normalize();
knots[i] += diff.signum();
}
tail_pios.insert(*knots.last().unwrap());
}
Expand All @@ -54,10 +54,10 @@ fn from_line(imp: &str) -> (Point, u32) {
let count = count.parse::<i32>().unwrap();

let out = match direction {
"R" => Point::new(1, 0),
"L" => Point::new(-1, 0),
"U" => Point::new(0, -1),
"D" => Point::new(0, 1),
"R" => vector!(1, 0),
"L" => vector!(-1, 0),
"U" => vector!(0, -1),
"D" => vector!(0, 1),
_ => panic!("Invalid direction"),
};

Expand Down
29 changes: 15 additions & 14 deletions aoc_2022/src/day_12.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::VecDeque;

use crate::aoc_lib;
use common::{Answer, Solution};
use nd_vec::vector;

type Point = aoc_lib::Point<usize>;
type Point = nd_vec::Vec2<usize>;

pub struct Day12;

Expand All @@ -25,7 +25,7 @@ impl Solution for Day12 {
map.start = map.end;
map.current = map.start;

run_path(&map, |a, b| b <= a + 1, |c| map.data[c.y][c.x] == 0)
run_path(&map, |a, b| b <= a + 1, |c| map.data[c.y()][c.x()] == 0)
.expect("No path found!?")
.into()
}
Expand All @@ -46,8 +46,8 @@ fn run_path(
return Some(history.len());
}

let current_height = map.data[current.y][current.x];
let mut check_neighbour = |x: usize, y: usize| {
let current_height = map.data[current.y()][current.x()];
let mut check_neighbor = |x: usize, y: usize| {
if x >= map.data[0].len()
|| y >= map.data.len()
|| visited[y][x]
Expand All @@ -59,13 +59,14 @@ fn run_path(
visited[y][x] = true;
let mut new_history = history.clone();
new_history.push(current);
queue.push_back((Point::new(x, y), new_history));
queue.push_back((vector!(x, y), new_history));
};

check_neighbour(current.x + 1, current.y);
check_neighbour(current.x, current.y + 1);
check_neighbour(current.x.wrapping_sub(1), current.y);
check_neighbour(current.x, current.y.wrapping_sub(1));
let (cx, cy) = (current.x(), current.y());
check_neighbor(cx + 1, cy);
check_neighbor(cx, cy + 1);
check_neighbor(cx.wrapping_sub(1), cy);
check_neighbor(cx, cy.wrapping_sub(1));
}

None
Expand All @@ -82,8 +83,8 @@ struct HeightMap {

fn parse(raw: &str) -> HeightMap {
let mut out = Vec::new();
let mut start = Point::new(0, 0);
let mut end = Point::new(0, 0);
let mut start = vector!(0, 0);
let mut end = vector!(0, 0);

for i in raw.lines() {
let mut row = Vec::new();
Expand All @@ -92,11 +93,11 @@ fn parse(raw: &str) -> HeightMap {
match j {
'S' => {
row.push(0);
start = Point::new(row.len() - 1, out.len());
start = vector!(row.len() - 1, out.len());
}
'E' => {
row.push(25);
end = Point::new(row.len() - 1, out.len());
end = vector!(row.len() - 1, out.len());
}
_ => row.push(j as u8 - 97),
}
Expand Down
Loading

0 comments on commit 7fac085

Please sign in to comment.