-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from ephemient/rs/day8
- Loading branch information
Showing
5 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::collections::BTreeSet; | ||
use std::iter; | ||
|
||
use itertools::Itertools; | ||
|
||
fn solve<I>(data: &str, multiples: I) -> usize | ||
where | ||
I: Clone + Iterator<Item = isize>, | ||
{ | ||
let (mut height, mut width) = (0, 0); | ||
data.lines() | ||
.enumerate() | ||
.flat_map(|(y, line)| { | ||
height = y + 1; | ||
width = width.max(line.len()); | ||
line.bytes().enumerate().filter_map(move |(x, b)| match b { | ||
b'.' => None, | ||
_ => Some((b, (y, x))), | ||
}) | ||
}) | ||
.into_grouping_map_by(|(b, _)| *b) | ||
.fold_with( | ||
|_, _| vec![], | ||
|mut acc, _, (_, point)| { | ||
acc.push(point); | ||
acc | ||
}, | ||
) | ||
.values() | ||
.flat_map(|points| { | ||
points.iter().flat_map(|point0 @ (y0, x0)| { | ||
points | ||
.iter() | ||
.filter(move |point1| point0 != *point1) | ||
.flat_map(|(y1, x1)| { | ||
let (dy, dx) = (*y1 as isize - *y0 as isize, *x1 as isize - *x0 as isize); | ||
multiples | ||
.clone() | ||
.map(move |i| { | ||
y1.checked_add_signed(i * dy) | ||
.filter(|y| *y < height) | ||
.zip(x1.checked_add_signed(i * dx).filter(|x| *x < width)) | ||
}) | ||
.while_some() | ||
}) | ||
}) | ||
}) | ||
.collect::<BTreeSet<_>>() | ||
.len() | ||
} | ||
|
||
pub fn part1(data: &str) -> usize { | ||
solve(data, iter::once(1)) | ||
} | ||
|
||
pub fn part2(data: &str) -> usize { | ||
solve(data, iter::successors(Some(0), |i| Some(i + 1))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use indoc::indoc; | ||
use pretty_assertions::assert_eq; | ||
|
||
static EXAMPLE: &str = indoc! {" | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ | ||
"}; | ||
|
||
#[test] | ||
fn part1_examples() { | ||
assert_eq!(14, part1(EXAMPLE)); | ||
} | ||
|
||
#[test] | ||
fn part2_examples() { | ||
assert_eq!(34, part2(EXAMPLE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod day4; | |
pub mod day5; | ||
pub mod day6; | ||
pub mod day7; | ||
pub mod day8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters