Skip to content

Commit

Permalink
day 13 part 2 optimization courtesy of @catface
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 13, 2023
1 parent e0b243f commit be1f69d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `28.7µs` | `36.3µs` |
| [Day 2](./src/bin/02.rs) | `42.5µs` | `42.0µs` |
| [Day 3](./src/bin/03.rs) | `84.3µs` | `99.5µs` |
| [Day 4](./src/bin/04.rs) | `48.1µs` | `50.9µs` |
| [Day 5](./src/bin/05.rs) | `20.7µs` | `24.4µs` |
| [Day 6](./src/bin/06.rs) | `205.0ns` | `102.0ns` |
| [Day 7](./src/bin/07.rs) | `93.9µs` | `92.7µs` |
| [Day 8](./src/bin/08.rs) | `75.1µs` | `159.3µs` |
| [Day 9](./src/bin/09.rs) | `58.9µs` | `58.4µs` |
| [Day 10](./src/bin/10.rs) | `257.9µs` | `275.7µs` |
| [Day 11](./src/bin/11.rs) | `16.5µs` | `15.8µs` |
| [Day 12](./src/bin/12.rs) | `135.9µs` | `647.1µs` |
| [Day 13](./src/bin/13.rs) | `12.4µs` | `40.5µs` |

**Total: 2.42ms**
| [Day 1](./src/bin/01.rs) | `34.4µs` | `34.8µs` |
| [Day 2](./src/bin/02.rs) | `42.5µs` | `41.1µs` |
| [Day 3](./src/bin/03.rs) | `85.2µs` | `99.8µs` |
| [Day 4](./src/bin/04.rs) | `48.4µs` | `50.0µs` |
| [Day 5](./src/bin/05.rs) | `20.6µs` | `24.3µs` |
| [Day 6](./src/bin/06.rs) | `203.0ns` | `102.0ns` |
| [Day 7](./src/bin/07.rs) | `95.0µs` | `94.7µs` |
| [Day 8](./src/bin/08.rs) | `73.2µs` | `177.3µs` |
| [Day 9](./src/bin/09.rs) | `57.7µs` | `58.1µs` |
| [Day 10](./src/bin/10.rs) | `262.6µs` | `280.0µs` |
| [Day 11](./src/bin/11.rs) | `16.3µs` | `16.0µs` |
| [Day 12](./src/bin/12.rs) | `138.5µs` | `661.3µs` |
| [Day 13](./src/bin/13.rs) | `12.3µs` | `16.4µs` |

**Total: 2.44ms**
<!--- benchmarking table --->

---
Expand Down
32 changes: 14 additions & 18 deletions src/bin/13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ pub fn part_two(input: &str) -> Option<u32> {

let col_mirror = (1..cols).find(|&col_split| {
(0..col_split.min(cols - col_split))
.map(|offset| {
(0..rows)
.map(|r| -> u32 {
(pattern[r * (cols + 1) + col_split - offset - 1]
!= pattern[r * (cols + 1) + col_split + offset])
.into()
})
.sum::<u32>()
.flat_map(|offset| {
(0..rows).filter(move |&r| {
pattern[r * (cols + 1) + col_split - offset - 1]
!= pattern[r * (cols + 1) + col_split + offset]
})
})
.sum::<u32>()
.take(2)
.count()
== 1
});
if let Some(col_mirror) = col_mirror {
Expand All @@ -76,16 +74,14 @@ pub fn part_two(input: &str) -> Option<u32> {

let row_mirror = (1..rows).find(|&row_split| {
(0..row_split.min(rows - row_split))
.map(|offset| {
(0..cols)
.map(|c| -> u32 {
(pattern[(row_split - offset - 1) * (cols + 1) + c]
!= pattern[(row_split + offset) * (cols + 1) + c])
.into()
})
.sum::<u32>()
.flat_map(|offset| {
(0..cols).filter(move |c| {
pattern[(row_split - offset - 1) * (cols + 1) + c]
!= pattern[(row_split + offset) * (cols + 1) + c]
})
})
.sum::<u32>()
.take(2)
.count()
== 1
});
if let Some(row_mirror) = row_mirror {
Expand Down

0 comments on commit be1f69d

Please sign in to comment.