Skip to content

Commit

Permalink
partial day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Dec 14, 2023
1 parent cfbf7c3 commit fa8e2e3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 11 deletions.
63 changes: 63 additions & 0 deletions BLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
# What Am I Learning Each Day?

### Day 13

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

**Time: ~- hrs**

**Run Time: ~-ms**

I really liked my part one implementation but I realize it's a pain for part two.

I transposed a grid today:

```rust
let rows = grid.len();
let cols = grid[0].len();

let mut transposed: Vec<Vec<Item>> = vec![vec![Item::Ash; rows]; cols];

for r in 0..rows {
for c in 0..cols {
// Transpose the elements
transposed[c][r] = grid[r][c];
}
}
```

I also had a good use of `zip`:

```rust
// pass grid or transposed to check rows or cols
fn is_reflection_at(grid: &Vec<Vec<Item>>, i: usize) -> bool {
// expand outwards from index
let prev = grid.iter().rev().skip(grid.len() - i);
let next = grid.iter().skip(i + 2);

for (p, n) in prev.zip(next) {
if p != n {
return false
}
}

true
}
```

And `windows`:

```rust
// size 2 means we look for side-by-side matches first
for (i, row) in self.grid.windows(2).enumerate() {
if row[0] == row[1] {
// we have one pair; extend the search to pair everything
if Self::is_reflection_at(&self.grid, i) {
return Reflection::Horizontal(i + 1);
}
}
}
```

For part two I realize it would be easier if I were comparing bits and checking if exactly 1 bit is different.

### Day 11

**Difficulty: -/10 ★★★★★★☆☆☆☆**
Expand Down Expand Up @@ -184,6 +245,8 @@ impl Add for Point {

A lot of this was copied directly from the documentation; though they use generics, and I'd like to avoid public module generics as much as possible, since my frustration with using it with **go**.

Learned Shoelace Formula and Pick's Theorem.

### Day 9

**Difficulty: 1/10 ★☆☆☆☆☆☆☆☆☆**
Expand Down
90 changes: 79 additions & 11 deletions day-13/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ use std::{time::Instant, fs};
use lib::get_part;

/** identify the direction of the reflection, with the col/row */
#[derive(PartialEq)]
enum Reflection {
Horizontal(usize),
Vertical(usize),
None
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone, Copy)]
enum Item {
Ash,
Rock
}

// TODO: this should be a binary, and we can check for
// XOR, then count ones to see if there's any diff
struct Pattern {
// uh oh, not going with hashmap
grid: Vec<Vec<Item>>
grid: Vec<Vec<Item>>,
// lazy way to check column equality, just flip it sideways
transposed: Vec<Vec<Item>>
}

impl Pattern {
Expand All @@ -38,16 +43,57 @@ impl Pattern {
grid.push(row);
}

Self { grid }

let rows = grid.len();
let cols = grid[0].len();

let mut transposed: Vec<Vec<Item>> = vec![vec![Item::Ash; rows]; cols];

for r in 0..rows {
for c in 0..cols {
// Transpose the elements
transposed[c][r] = grid[r][c];
}
}

Self { grid, transposed }
}

// pass grid or transposed to check rows or cols
fn is_reflection_at(grid: &Vec<Vec<Item>>, i: usize) -> bool {
// expand outwards from index
let prev = grid.iter().rev().skip(grid.len() - i);
let next = grid.iter().skip(i + 2);

for (p, n) in prev.zip(next) {
if p != n {
return false
}
}

true
}

fn find_reflection_point(&self) -> Reflection {
// size 2 means we look for side-by-side matches first
dbg!(&self.grid);
for row in self.grid.windows(2) {
for (i, row) in self.grid.windows(2).enumerate() {
if row[0] == row[1] {
dbg!(row);
}
// we have one pair; extend the search to pair everything
if Self::is_reflection_at(&self.grid, i) {
return Reflection::Horizontal(i + 1);
}
}
}

// vertical is harder, because we have to make new vectors?
// let's be lazy and use the transposed
for (i, col) in self.transposed.windows(2).enumerate() {
if col[0] == col[1] {
// we have one pair; extend the search to pair everything
if Self::is_reflection_at(&self.transposed, i) {
return Reflection::Vertical(i + 1);
}
}
}

// we might not have any reflection
Expand All @@ -56,8 +102,13 @@ impl Pattern {
}

fn part_one(patterns: &Vec<Pattern>) -> usize {
patterns[0].find_reflection_point();
1
patterns.iter().map(|p| {
match p.find_reflection_point() {
Reflection::Horizontal(n) => n * 100,
Reflection::Vertical(n) => n,
Reflection::None => 0
}
}).sum()
}

fn part_two() -> usize {
Expand All @@ -69,7 +120,7 @@ fn main() {
let start = Instant::now();
let contents = fs::read_to_string("./src/input.txt").unwrap();

let patterns = contents.lines().map(Pattern::new).collect();
let patterns = contents.split("\n\n").map(Pattern::new).collect();

if one {
let now = Instant::now();
Expand All @@ -92,12 +143,29 @@ mod tests {

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

#[test]
fn test_reflection_point() {
let patterns: Vec<Pattern> = EXAMPLE.split("\n\n").map(Pattern::new).collect();

if let Reflection::Horizontal(n) = patterns[1].find_reflection_point() {
assert_eq!(n, 4, "n isn't 4!");
} else {
assert!(false, "HEY! n is not horizontal");
};

if let Reflection::Vertical(n) = patterns[0].find_reflection_point() {
assert_eq!(n, 5, "n isn't 5!");
} else {
assert!(false, "HEY! n is not vertical");
};
}

#[test]
fn test_part_one() {
let patterns = EXAMPLE.split("\n\n").map(Pattern::new).collect();
let ans = part_one(&patterns);

assert_eq!(ans, 0);
assert_eq!(ans, 405);
}

#[test]
Expand Down

0 comments on commit fa8e2e3

Please sign in to comment.