Skip to content

Commit

Permalink
chore(fmt): lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BRAVO68WEB committed Dec 25, 2023
1 parent 65f7353 commit b8c2593
Show file tree
Hide file tree
Showing 11 changed files with 4 additions and 48 deletions.
1 change: 0 additions & 1 deletion code/rs/src/d15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fn part2(input: &str) -> usize {
acc += (i + 1) * (j + 1) * f.1 as usize;
}
}
// return as integer
acc
}

Expand Down
4 changes: 0 additions & 4 deletions code/rs/src/d16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,14 @@ pub fn part1(data: &str) -> Option<usize> {
}

pub fn part2(data: &str) -> Option<usize> {
// Split the input string into lines and collect into a vector
let grid = data.lines().collect::<Vec<_>>();

// Generate starting points for traversal in all four directions
let right_starts = (0..grid.len()).map(|y| (y, 0, Direction::R));
let down_starts = (0..grid.first()?.len()).map(|x| (0, x, Direction::D));
let left_starts =
(0..grid.len()).filter_map(|y| Some((y, grid[y].len().checked_sub(1)?, Direction::L)));
let up_starts = (0..grid.last()?.len()).map(|x| (grid.len() - 1, x, Direction::U));

// Combine all starting points and process in parallel
let result = right_starts
.chain(down_starts)
.chain(left_starts)
Expand All @@ -95,7 +92,6 @@ pub fn part2(data: &str) -> Option<usize> {
.filter_map(|(y, x, direction)| fill(&grid, y, x, direction))
.max();

// Return the result
result
}

Expand Down
2 changes: 1 addition & 1 deletion code/rs/src/d21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn generate_neighbors(position: (isize, isize)) -> Vec<(isize, isize)> {
}

fn solve_steps(garden: Garden, num_steps: isize) -> usize {
let mut queue = vec![((0, 0), 0)]; // position, number of steps so far
let mut queue = vec![((0, 0), 0)];
let mut visited = HashSet::new();

while let Some(state) = queue.pop() {
Expand Down
15 changes: 0 additions & 15 deletions code/rs/src/d22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ impl Brick {
fn move_down(&mut self) {
self.min.z -= 1;
self.max.z -= 1;
// this is probably inefficient?
self.cubes = Self::cubes(self.min, self.max);
}

// generate all the cubes for this brick
fn cubes(min: Triple, max: Triple) -> BTreeSet<Triple> {
(min.z..=max.z)
.flat_map(|z| {
Expand Down Expand Up @@ -129,25 +127,19 @@ impl Ord for Brick {
}

fn settle(input: &[Brick]) -> Input {
// sort the bricks by z index
let mut bricks = input.to_vec();
bricks.sort();

// The 3D tower, with brick id being the value
let mut settled: BTreeMap<Triple, usize> = BTreeMap::new();

// the "graph" indicating which bricks support other bricks
let mut holding_up: SupportMap = BTreeMap::new();
let mut sitting_on: SupportMap = BTreeMap::new();

for brick in bricks.iter_mut() {
// set up our graph from here
holding_up.insert(brick.id, BTreeSet::new());
sitting_on.insert(brick.id, BTreeSet::new());

// move down until we find something below us
let bricks_below = loop {
// if we hit the ground, return nothing
if brick.on_ground() {
break vec![];
}
Expand All @@ -159,18 +151,15 @@ fn settle(input: &[Brick]) -> Input {
.cloned()
.collect();

// if there are bricks below us, return their IDs
if !bricks_below.is_empty() {
break bricks_below;
}

brick.move_down();
};

// now that we dropped all the way, set the id where we landed
settled.extend(brick.cubes.iter().map(|c| (*c, brick.id)));

// set up the graph for each of the bricks
for below in bricks_below {
holding_up.entry(below).or_default().insert(brick.id);
sitting_on.entry(brick.id).or_default().insert(below);
Expand All @@ -194,22 +183,18 @@ fn part2(input: &Input) -> usize {
holding_up
.iter()
.map(|(brick, above)| {
// start considering everything above us
let mut queue: VecDeque<usize> = VecDeque::from_iter(above.iter().cloned());
let mut unsupported: BTreeSet<usize> = BTreeSet::from_iter(vec![*brick]);

while let Some(brick) = queue.pop_front() {
// if all of the bricks that we're sitting on are unsupported
if sitting_on[&brick].is_subset(&unsupported) {
// this one is unsupported, and consider the rest of the ones sitting on us
unsupported.insert(brick);
for b in &holding_up[&brick] {
queue.push_back(*b);
}
}
}

// discount the original brick we put in the falling set to get the rest of it to work
unsupported.len() - 1
})
.sum()
Expand Down
1 change: 0 additions & 1 deletion code/rs/src/d25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ fn part1(input: &str) -> usize {
let graph = parse_graph(input);
let vertices: Vec<&str> = graph.keys().map(|x| *x).collect();
let mut set1: Vec<&str> = Vec::new();
// Add vertex 0 to the set
set1.push(vertices[0]);
let mut j = 0;
while j < set1.len() {
Expand Down
3 changes: 0 additions & 3 deletions code/rs/src/d3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ fn build_part_map(map: &Matrix<char>) -> BTreeMap<(usize, usize), Vec<u64>> {
.take_while(|(_, c)| c.is_ascii_digit())
.collect::<Vec<_>>();

// Check if is part number
if let Some(gear) = number
.iter()
.flat_map(|(j, _)| map.neighbours((i, *j), true))
.find(|(i, j)| map[(*i, *j)] != '.' && !map[(*i, *j)].is_numeric())
{
// Parse number
let n = number
.into_iter()
.map(|(_, c)| c)
.collect::<String>()
.parse::<u64>()
.unwrap();

// Add to part numbers
part_numbers.entry(gear).or_default().push(n);
}
}
Expand Down
7 changes: 0 additions & 7 deletions code/rs/src/d5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,29 @@ fn map_seeds_ranges(seeds: Vec<Range<u64>>, mappings: &Mappings) -> Vec<Range<u6
let final_end = mappings
.iter()

// Get only the ones that affect the current seed range
.filter(|(_, source_range_start, range_length)| {
source_range_start + range_length > seed_range.start && *source_range_start < seed_range.end
})

// Sort them to only have to apply them once and avoid collisions
.sorted_by_key(|(_, source_range_start, _)| *source_range_start)
.fold(
seed_range.start,
|current, (destination_range_start, source_range_start, range_length)| {
// From current to start of the mapping
if current < *source_range_start {
mapped_ranges.push(current..*source_range_start);
}

// From start of the mapping or start of the seed range
// To end of the mapping or end of the seed range
let common_start = current.max(*source_range_start);
let common_end = seed_range.end.min(source_range_start + range_length);
mapped_ranges.push(
common_start + destination_range_start - source_range_start
..common_end + destination_range_start - source_range_start,
);

// Move to common end
common_end
},
);

// From end of the last mapping to end of the seed range
if final_end < seed_range.end {
mapped_ranges.push(final_end..seed_range.end);
}
Expand Down
7 changes: 0 additions & 7 deletions code/rs/src/d7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,33 @@ impl HandType {
.collect::<Vec<_>>()
.as_slice()
{
// Five of a kind
[(5, _)]
| [(4, Card::Joker), (1, _)]
| [(4, _), (1, Card::Joker)]
| [(3, _), (2, Card::Joker)]
| [(3, Card::Joker), (2, _)] => Self::FiveOfAKind,

// Four of a kind
[(4, _), (1, _)]
| [(3, Card::Joker), (1, _), (1, _)]
| [(3, _), (1, Card::Joker), (1, _)]
| [(3, _), (1, _), (1, Card::Joker)]
| [(2, Card::Joker), (2, _), (1, _)]
| [(2, _), (2, Card::Joker), (1, _)] => Self::FourOfAKind,

// Full house
[(3, _), (2, _)] | [(2, _), (2, _), (_, Card::Joker)] => Self::FullHouse,

// Three of a kind
[(3, _), (_, _), (_, _)] | [(2, Card::Joker), (1, _), (1, _), (1, _)] => {
Self::ThreeOfAKind
}
cc @ [(2, _), (_, _), (_, _), (_, _)] if cc.contains(&(&1, &&Card::Joker)) => {
Self::ThreeOfAKind
}

// Two pair
[(2, _), (2, _), (_, _)] => Self::TwoPair,

// One pair
[(2, _), (_, _), (_, _), (_, _)] => Self::OnePair,
cc if cc.contains(&(&1, &&Card::Joker)) => Self::OnePair,

// High card
[(1, _), (_, _), (_, _), (_, _), (_, _)] => Self::HighCard,

hand => panic!("Invalid hand {hand:?}"),
Expand Down
4 changes: 0 additions & 4 deletions code/ts/23/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const crossings = {} as Record<
}
>;

// Find all crossroads
const dots = matrixFindElements(matrix, { value: "." });
for (const [pos] of dots) {
const neigs = xydirections(pos)
Expand All @@ -85,7 +84,6 @@ crossings[xykey(target)] = { pos: target, paths: {} };
const m2 = matrixClone(matrix);
Object.values(crossings).forEach(({ pos }) => matrixSet(m2, pos, "X"));

// Find path lengths between crossroads
for (const crossing of Object.values(crossings)) {
const xpos = crossing.pos;
const neigs = xydirections(xpos);
Expand All @@ -105,7 +103,6 @@ for (const crossing of Object.values(crossings)) {
}
}

// Find longest path through crossroads
const itFinder2 = findPathsFlexi<[string, string[]]>({
startNodes: [[xykey(start), [xykey(start)]]],
endCondition: ([key]) => key == xykey(target),
Expand All @@ -115,7 +112,6 @@ const itFinder2 = findPathsFlexi<[string, string[]]>({
.map(v => [v, [...path, v]]),
costFn: ([to], cost, [from]) => cost + crossings[from].paths[to],
prioritizeHighCost: true,
// Importantly, no cacheKeyFn passed in
});

let max2 = 0;
Expand Down
6 changes: 3 additions & 3 deletions code/ts/24/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const part1 = (input: string): string | number => {
const startY = py + vy * startTime;
const endY = py + vy * endTime;
if (startTime > endTime) {
return false; // X and Y don't fall in zone at the same time
return false;
}
return {
start: xy(startX, startY),
Expand All @@ -88,7 +88,7 @@ export const part1 = (input: string): string | number => {
const [[aPX, aPY], [aVX, aVY]] = hailstones[i];
const aBounds = hailstoneBounds[i] || getBounds(aPX, aPY, aVX, aVY);
hailstoneBounds[i] = aBounds;
if (!aBounds) continue; // Not in zone
if (!aBounds) continue;
for (let j = 1; j < hailstones.length; j++) {
if (i === j) continue;
const pairKey = (i < j ? [i, j] : [j, i]).join(":");
Expand All @@ -97,7 +97,7 @@ export const part1 = (input: string): string | number => {
const [[bPX, bPY], [bVX, bVY]] = hailstones[j];
const bBounds = hailstoneBounds[j] || getBounds(bPX, bPY, bVX, bVY);
hailstoneBounds[j] = bBounds;
if (!bBounds) continue; // Not in zone
if (!bBounds) continue;
if (getSegmentIntersection(aBounds.start, aBounds.end, bBounds.start, bBounds.end)) {
intersections++;
}
Expand Down
2 changes: 0 additions & 2 deletions libs/aoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ const today = new Date();

const day = today.getDate();

// Download input
Bun.spawn(["aoc", "-I", "-i", `../../input/${day}.txt`, "-d", day.toString(), "download"]);

// Create new project
const proc = Bun.spawn(["bash", "../../scripts/init.sh"]);
const output = await new Response(proc.stdout).text();

Expand Down

0 comments on commit b8c2593

Please sign in to comment.