Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 17, 2024
2 parents 7c39b37 + 9f36913 commit 83de3b2
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 240 deletions.
6 changes: 3 additions & 3 deletions aoc_2021/src/day_15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use std::collections::BinaryHeap;
use hashbrown::HashMap;
use nd_vec::{vector, Vector};

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};

solution!("Chiton", 15);

type Point = Vector<usize, 2>;

fn part_a(input: &str) -> Answer {
let matrix = Matrix::new_chars(input, |chr| chr.to_digit(10).unwrap() as u8);
let matrix = Grid::new(input, |chr| chr.to_digit(10).unwrap() as u8);
solve(matrix.size, |pos| matrix.get(pos).copied()).into()
}

fn part_b(input: &str) -> Answer {
let matrix = Matrix::new_chars(input, |chr| chr.to_digit(10).unwrap() as u8);
let matrix = Grid::new(input, |chr| chr.to_digit(10).unwrap() as u8);
solve(matrix.size * 5, |pos| {
let (cx, cy) = (pos.x() / matrix.size.x(), pos.y() / matrix.size.y());
if cx > 4 || cy > 4 {
Expand Down
6 changes: 3 additions & 3 deletions aoc_2023/src/day_14.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, convert::identity, hash::Hash};

use aoc_lib::matrix::Matrix;
use aoc_lib::matrix::Grid;
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand Down Expand Up @@ -35,12 +35,12 @@ fn part_b(input: &str) -> Answer {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Dish {
tiles: Matrix<char>,
tiles: Grid<char>,
}

fn parse(input: &str) -> Dish {
Dish {
tiles: Matrix::new_chars(input, identity),
tiles: Grid::new(input, identity),
}
}

Expand Down
10 changes: 5 additions & 5 deletions aoc_2023/src/day_16.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand Down Expand Up @@ -30,8 +30,8 @@ fn part_b(input: &str) -> Answer {
max.into()
}

fn parse(input: &str) -> Matrix<Tile> {
Matrix::new_chars(input, Tile::from_char)
fn parse(input: &str) -> Grid<Tile> {
Grid::new(input, Tile::from_char)
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand All @@ -43,8 +43,8 @@ enum Tile {
SlantRight, // /
}

fn lazer(cavern: &Matrix<Tile>, start: Pos, direction: Direction) -> usize {
fn _lazer(cavern: &Matrix<Tile>, visited: &mut HashSet<Pos>, mut pos: Pos, mut dir: Direction) {
fn lazer(cavern: &Grid<Tile>, start: Pos, direction: Direction) -> usize {
fn _lazer(cavern: &Grid<Tile>, visited: &mut HashSet<Pos>, mut pos: Pos, mut dir: Direction) {
loop {
pos = dir.advance(pos);
if !cavern.contains(pos) {
Expand Down
8 changes: 4 additions & 4 deletions aoc_2023/src/day_17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::{BinaryHeap, HashMap},
};

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand All @@ -19,11 +19,11 @@ fn part_b(input: &str) -> Answer {
pathfind(parse(input), 4, 10).into()
}

fn parse(input: &str) -> Matrix<u8> {
Matrix::new_chars(input, |c| c as u8 - b'0')
fn parse(input: &str) -> Grid<u8> {
Grid::new(input, |c| c as u8 - b'0')
}

fn pathfind(board: Matrix<u8>, min_dist: u8, max_dist: u8) -> u32 {
fn pathfind(board: Grid<u8>, min_dist: u8, max_dist: u8) -> u32 {
let mut queue = BinaryHeap::new();
let mut visited = HashMap::new();
let mut res = u32::MAX;
Expand Down
6 changes: 3 additions & 3 deletions aoc_2023/src/day_21.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand Down Expand Up @@ -86,8 +86,8 @@ enum Tile {
Start,
}

fn parse(input: &str) -> Matrix<Tile> {
Matrix::new_chars(input, |x| match x {
fn parse(input: &str) -> Grid<Tile> {
Grid::new(input, |x| match x {
'#' => Tile::Wall,
'.' => Tile::Garden,
'S' => Tile::Start,
Expand Down
10 changes: 5 additions & 5 deletions aoc_2023/src/day_23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
convert::identity,
};

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand All @@ -20,7 +20,7 @@ fn part_b(input: &str) -> Answer {
}

// Simple DFS to find the longest path for part A
fn solve_a(map: &Matrix<char>, visited: &mut HashSet<Pos>, pos: Pos, idx: u32) -> u32 {
fn solve_a(map: &Grid<char>, visited: &mut HashSet<Pos>, pos: Pos, idx: u32) -> u32 {
if pos == map.size() - vector!(2, 1) {
return idx;
}
Expand All @@ -44,7 +44,7 @@ fn solve_a(map: &Matrix<char>, visited: &mut HashSet<Pos>, pos: Pos, idx: u32) -
}

// Convert the map into a graph and collapse it to find the longest path for part B
fn solve_b(map: &Matrix<char>) -> u32 {
fn solve_b(map: &Grid<char>) -> u32 {
// == Build graph ==
let mut graph = HashMap::new();
let mut add_edge = |a: Pos, b: Pos| {
Expand Down Expand Up @@ -124,8 +124,8 @@ fn solve_b(map: &Matrix<char>) -> u32 {
max
}

fn parse(input: &str) -> Matrix<char> {
Matrix::new_chars(input, identity)
fn parse(input: &str) -> Grid<char> {
Grid::new(input, identity)
}

fn dir_matches(dir: Direction, chr: char) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions aoc_2024/src/day_04.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::convert::identity;

use aoc_lib::{direction::ordinal::Direction, matrix::Matrix};
use aoc_lib::{direction::ordinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::vector;

solution!("Ceres Search", 4);

fn part_a(input: &str) -> Answer {
let matrix = Matrix::new_chars(input, identity);
let matrix = Grid::new(input, identity);
let mut count = 0;

for y in 0..matrix.size.y() {
Expand Down Expand Up @@ -44,7 +44,7 @@ const MAS_DIRECTIONS: [[Direction; 2]; 2] = [
];

fn part_b(input: &str) -> Answer {
let matrix = Matrix::new_chars(input, identity);
let matrix = Grid::new(input, identity);
let mut count = 0;

for y in 0..matrix.size.y() {
Expand Down
6 changes: 3 additions & 3 deletions aoc_2024/src/day_06.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::Vec2;
use rayon::iter::{ParallelBridge, ParallelIterator};
Expand Down Expand Up @@ -30,13 +30,13 @@ enum Tile {
}

struct Map {
map: Matrix<Tile>,
map: Grid<Tile>,
start: Vec2<usize>,
}

impl Map {
fn new(input: &str) -> Self {
let map = Matrix::new_chars(input, |x| match x {
let map = Grid::new(input, |x| match x {
'#' => Tile::Obstacle,
'^' => Tile::Start,
_ => Tile::None,
Expand Down
6 changes: 3 additions & 3 deletions aoc_2024/src/day_08.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};

use aoc_lib::matrix::Matrix;
use aoc_lib::matrix::Grid;
use common::{solution, Answer};
use nd_vec::Vec2;

Expand Down Expand Up @@ -52,7 +52,7 @@ fn part_b(input: &str) -> Answer {
}

struct AntennaMap {
world: Matrix<Tile>,
world: Grid<Tile>,
freqs: HashMap<char, Vec<Vec2<i32>>>,
}

Expand All @@ -63,7 +63,7 @@ enum Tile {

impl AntennaMap {
fn parse(input: &str) -> Self {
let world = Matrix::new_chars(input, |x| match x {
let world = Grid::new(input, |x| match x {
'a'..='z' | 'A'..='Z' | '0'..='9' => Tile::Emitter(x),
_ => Tile::Empty,
});
Expand Down
6 changes: 3 additions & 3 deletions aoc_2024/src/day_10.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashSet, VecDeque};

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::Vec2;

Expand All @@ -22,12 +22,12 @@ fn solve(input: &str, part_b: bool) -> usize {
}

struct Map {
board: Matrix<u32>,
board: Grid<u32>,
}

impl Map {
fn parse(input: &str) -> Self {
let board = Matrix::new_chars(input, |x| x.to_digit(10).unwrap());
let board = Grid::new(input, |x| x.to_digit(10).unwrap());
Self { board }
}

Expand Down
2 changes: 1 addition & 1 deletion aoc_2024/src/day_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn solve(nums: Vec<u64>, iters: usize) -> u64 {
counts = next;
}

counts.values().sum::<u64>().into()
counts.values().sum::<u64>()
}

fn parse(input: &str) -> Vec<u64> {
Expand Down
6 changes: 3 additions & 3 deletions aoc_2024/src/day_12.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashSet, convert::identity};

use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use itertools::Itertools;
use nd_vec::{vector, Vec2};
Expand Down Expand Up @@ -51,14 +51,14 @@ fn part_b(input: &str) -> Answer {
}

struct Garden {
matrix: Matrix<char>,
matrix: Grid<char>,

seen: HashSet<Vec2<usize>>,
}

impl Garden {
fn parse(input: &str) -> Self {
let matrix = Matrix::new_chars(input, identity);
let matrix = Grid::new(input, identity);
Self {
matrix,
seen: HashSet::new(),
Expand Down
2 changes: 1 addition & 1 deletion aoc_2024/src/day_13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Problem {
}

fn solve(&self) -> u64 {
self.cases.iter().map(|x| x.cheapest()).sum::<u64>().into()
self.cases.iter().map(|x| x.cheapest()).sum::<u64>()
}

fn part_b(mut self) -> Self {
Expand Down
16 changes: 7 additions & 9 deletions aoc_2024/src/day_15.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};

Expand All @@ -20,7 +20,7 @@ struct Problem {
pos: Vec2<usize>,
idx: usize,

board: Matrix<Tile>,
board: Grid<Tile>,
instructions: Vec<Direction>,
}

Expand All @@ -36,7 +36,7 @@ enum Tile {
impl Problem {
fn parse(input: &str, part_b: bool) -> Self {
let (board, instructions) = input.split_once("\n\n").unwrap();
let mut board = Matrix::new_chars(board, |chr| match chr {
let mut board = Grid::new(board, |chr| match chr {
'@' => Tile::Robot,
'#' => Tile::Wall,
'O' => Tile::Box,
Expand Down Expand Up @@ -96,12 +96,10 @@ impl Problem {
self.idx += 1;

let new = dir.advance(self.pos);
if {
if part_b {
self.push_b(new, dir)
} else {
self.push(new, dir)
}
if if part_b {
self.push_b(new, dir)
} else {
self.push(new, dir)
} {
self.pos = new;
}
Expand Down
Loading

0 comments on commit 83de3b2

Please sign in to comment.