Skip to content

Commit

Permalink
Day 11! ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Jul 19, 2024
1 parent e5da307 commit ec6ea35
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 39 deletions.
24 changes: 22 additions & 2 deletions BLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,35 @@ For part two I realize it would be easier if I were comparing bits and checking

### Day 11

**Difficulty: -/10 ★★★★★★☆☆☆☆**
**Difficulty: 5/10 ★★★★★★☆☆☆☆**

**Time: ~- hrs**
**Time: 3 hrs**

**Run Time: ~-ms**

Already, I'm upset to see the grid with stars.

Came back in July and found a solution to the problem which worked; also, started using Codeium to try out AI IDE plugins, and it seems to work pretty well.

The idea is that you can just get the manhattan distance as is, and apply the missing columns and rows in between:

```rust
let empty_x = self.empty_cols
.iter()
.filter(|x| **x >= min_x && **x <= max_x)
.count();
let empty_y = self.empty_rows
.iter()
.filter(|y| **y >= min_y && **y <= max_y)
.count();

let gap = gap_distance - 1;
let dist = dx + empty_x * gap + dy + empty_y * gap;
```

And that's all.

I still find it so frustrating to iterate a mutable hashmap; I have no idea how to do it.

### Day 10

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ New: `./createDay.sh 02`
- [Reddit](https://reddit.com/r/adventofcode)
- [StackOverflow](https://stackoverflow.com/questions/tagged/rust)
- [Discord](https://discord.com/invite/rust-lang-community)
- [Codeium](https://codeium.com/)
120 changes: 83 additions & 37 deletions day-11/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,103 @@
use std::{ time::Instant, fs, collections::HashSet };
use lib::{ get_part, Point };
use std::{ time::Instant, fs };
use lib::get_part;

type Point = (usize, usize);

struct Universe {
grid: HashSet<Point>,
grid: Vec<Point>,
empty_cols: Vec<usize>,
empty_rows: Vec<usize>,
}

impl Universe {
fn new(contents: &str) -> Self {
let lines = contents.lines();
let mut grid = HashSet::new();
let mut grid = Vec::new();
let mut empty_cols = Vec::new();
let mut empty_rows = Vec::new();
let mut height = 0;
let mut width = 0;
let mut x_set = HashSet::new();
let mut y_set = HashSet::new();

for (y, line) in lines.enumerate() {
height += 1;
width = line.len();
for (x, char) in line.chars().enumerate() {
if char == '#' {
dbg!(x, y);
grid.insert(Point { x: x as isize, y: y as isize });
x_set.insert(x);
y_set.insert(y);
}
if y == 0 && x > width {
width = x;
grid.push((x, y));
}
}
if y > height {
height = y;
}
}

// find empty col/rows and expand
for y in 0..=height {
if !y_set.contains(&y) {
// empty row!
println!("empty row! {}", y);
for x in 0..width {
if grid.iter().any(|(px, _)| *px == x) {
continue;
}
empty_cols.push(x);
}

for x in 0..=width {
if !x_set.contains(&x) {
// empty col!
println!("empty col: {}", x);
for y in 0..height {
if grid.iter().any(|(_, py)| *py == y) {
continue;
}
empty_rows.push(y);
}

Self { grid }
Self { grid, empty_cols, empty_rows }
}

fn get_manhattan_distance(&self, gap_distance: usize) -> usize {
(0..self.grid.len() - 1)
.flat_map(|i| {
(i + 1..self.grid.len()).map(move |j| {
let dx = self.grid[i].0.abs_diff(self.grid[j].0);
let dy = self.grid[i].1.abs_diff(self.grid[j].1);
let min_x = self.grid[i].0.min(self.grid[j].0);
let min_y = self.grid[i].1.min(self.grid[j].1);
let max_x = min_x + dx;
let max_y = min_y + dy;

let empty_x = self.empty_cols
.iter()
.filter(|x| **x >= min_x && **x <= max_x)
.count();
let empty_y = self.empty_rows
.iter()
.filter(|y| **y >= min_y && **y <= max_y)
.count();

let gap = gap_distance - 1;
let dist = dx + empty_x * gap + dy + empty_y * gap;

return dist;
})
})
.sum()
}
}

fn part_one() -> usize {
0
fn part_one(uni: &Universe) -> usize {
uni.get_manhattan_distance(2)
}

fn part_two() -> usize {
0
fn part_two(uni: &Universe) -> usize {
uni.get_manhattan_distance(1000000)
}

fn main() {
let (one, two) = get_part();
let start = Instant::now();
let contents = fs::read_to_string("./src/input.txt").unwrap();
let universe = Universe::new(&contents);

if one {
let now = Instant::now();
let ans = part_one();
let ans = part_one(&universe);
println!("Part one: {:?} {:?}", ans, now.elapsed());
}

if two {
let now = Instant::now();
let ans = part_two();
let ans = part_two(&universe);
println!("Part two: {:?} {:?}", ans, now.elapsed());
}

Expand All @@ -84,19 +110,39 @@ mod tests {

const EXAMPLE: &str = include_str!("./example.txt");

const EXAMPLE_2: &str = "#..
...
..#";

#[test]
fn test_simple() {
let grid = Universe::new(EXAMPLE_2);

assert_eq!(grid.empty_cols, vec![1]);
assert_eq!(grid.empty_rows, vec![1]);

let ans = grid.get_manhattan_distance(2);

assert_eq!(ans, 6);
}

#[test]
#[ignore]
fn test_part_one() {
let grid = Universe::new(EXAMPLE);
let ans = part_one();

assert_eq!(ans, 10);
assert_eq!(grid.empty_cols, vec![2, 5, 8]);
assert_eq!(grid.empty_rows, vec![3, 7]);

let ans = grid.get_manhattan_distance(2);

assert_eq!(ans, 374);
}

#[test]
fn test_part_two() {
let ans = part_two();
let grid = Universe::new(EXAMPLE);

assert_eq!(ans, 0);
assert_eq!(grid.get_manhattan_distance(10), 1030);
assert_eq!(grid.get_manhattan_distance(100), 8410);
}
}

0 comments on commit ec6ea35

Please sign in to comment.