Solve 04
This commit is contained in:
parent
90eeb5dc03
commit
57b0474d09
1 changed files with 75 additions and 0 deletions
75
src/bin/04.rs
Normal file
75
src/bin/04.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
use aoc::*;
|
||||||
|
use glam::IVec2;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../../input/04");
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Part 1: {}", part1(INPUT));
|
||||||
|
println!("Part 2: {}", part2(INPUT));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example() {
|
||||||
|
assert_example!(part1, "04-test", 13);
|
||||||
|
assert_example!(part2, "04-test", 43);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> usize {
|
||||||
|
let mut rolls = parse_input(input);
|
||||||
|
remove_accessible(&mut rolls)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: &str) -> usize {
|
||||||
|
let mut rolls = parse_input(input);
|
||||||
|
let mut sum = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let removed = remove_accessible(&mut rolls);
|
||||||
|
sum += removed;
|
||||||
|
if removed == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_accessible(rolls: &mut HashSet<IVec2>) -> usize {
|
||||||
|
let mut accessible = Vec::new();
|
||||||
|
|
||||||
|
for roll in rolls.iter().copied() {
|
||||||
|
let mut neighbors = 0;
|
||||||
|
for dir in DIRECTIONS8 {
|
||||||
|
let neighbor = roll + dir;
|
||||||
|
if rolls.contains(&neighbor) {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if neighbors < 4 {
|
||||||
|
accessible.push(roll);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = accessible.len();
|
||||||
|
|
||||||
|
for accessible in accessible {
|
||||||
|
rolls.remove(&accessible);
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> HashSet<IVec2> {
|
||||||
|
let mut result = HashSet::new();
|
||||||
|
|
||||||
|
for (y, line) in input.lines().enumerate() {
|
||||||
|
for (x, c) in line.chars().enumerate() {
|
||||||
|
if c == '@' {
|
||||||
|
result.insert(IVec2::new(x as i32, y as i32));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue