Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn part_one(input: &str) -> Option<u32> {
- fn offsets(i: i32, j: i32, d: i32) -> Vec<(u32, i32, i32)> {
- vec![
- (0, i + d, j),
- (1, i + d, j + d),
- (2, i, j + d),
- (3, i - d, j + d),
- (4, i - d, j),
- (5, i - d, j - d),
- (6, i, j - d),
- (7, i + d, j - d),
- ]
- }
- let mut counts = HashMap::<(u32, i32, i32), u32>::new();
- input
- .split("\n")
- .enumerate()
- .map(|(i, l)| {
- l.chars()
- .enumerate()
- .map(move |(j, c)| match c {
- 'X' => offsets(i as i32, j as i32, 0),
- 'M' => offsets(i as i32, j as i32, 1),
- 'A' => offsets(i as i32, j as i32, 2),
- 'S' => offsets(i as i32, j as i32, 3),
- _ => vec![],
- })
- .flatten()
- })
- .flatten()
- .for_each(|(d, i, j)| *counts.entry((d, i, j)).or_default() += 1);
- Some(counts.values().filter(|v| **v == 4).count() as u32)
- }
- pub fn part_two(input: &str) -> Option<u32> {
- fn offsets(i: i32, j: i32, d: i32) -> Vec<(u32, i32, i32)> {
- vec![
- (0, i + d, j + d),
- (1, i + d, j + d),
- (1, i - d, j + d),
- (2, i - d, j + d),
- (2, i - d, j - d),
- (3, i - d, j - d),
- (3, i + d, j - d),
- (0, i + d, j - d),
- ]
- }
- let mut counts = HashMap::<(u32, i32, i32), u32>::new();
- input
- .split("\n")
- .enumerate()
- .map(|(i, l)| {
- l.chars()
- .enumerate()
- .map(move |(j, c)| match c {
- 'M' => offsets(i as i32, j as i32, -1),
- 'A' => offsets(i as i32, j as i32, 0),
- 'S' => offsets(i as i32, j as i32, 1),
- _ => vec![],
- })
- .flatten()
- })
- .flatten()
- .for_each(|(d, i, j)| *counts.entry((d, i, j)).or_default() += 1);
- // A will feature twice in votes, so need 6
- Some(counts.values().filter(|v| **v == 6).count() as u32)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement