1
0

Compare commits

..

3 Commits

Author SHA1 Message Date
dce8b1d2d5 clippy and cleanup 2024-12-12 18:47:22 +01:00
6621a5a71a day 12 part 2 2024-12-12 18:43:15 +01:00
0d0bcb2b33 day 12 part 1 2024-12-12 08:47:08 +01:00
4 changed files with 188 additions and 1 deletions

View File

@@ -23,7 +23,7 @@ fn solve(input: &str, blinks: i32) -> usize {
next.entry(1).and_modify(|count| *count += v).or_insert(v); next.entry(1).and_modify(|count| *count += v).or_insert(v);
} else if (k.ilog10() + 1) % 2 == 0 { } else if (k.ilog10() + 1) % 2 == 0 {
let l = (k.ilog10() + 1) / 2; let l = (k.ilog10() + 1) / 2;
let z: usize = 10usize.pow(l as u32); let z: usize = 10usize.pow(l);
let a = k / z; let a = k / z;
let b = k % z; let b = k % z;
next.entry(a).and_modify(|count| *count += v).or_insert(v); next.entry(a).and_modify(|count| *count += v).or_insert(v);

185
src/day12.rs Normal file
View File

@@ -0,0 +1,185 @@
use std::{collections::HashSet, fs::read_to_string};
use itertools::Itertools;
use crate::utils::grid::{Coord, Grid};
pub fn day_main() {
let input = read_to_string("input/day12.txt").unwrap();
let input = input.trim();
println!(" part1: {}", part1(input));
println!(" part2: {}", part2(input));
}
type RiddleResult = u64;
const NEIGHBORS: &[Coord] = &[(1, 0), (0, 1), (-1, 0), (0, -1)];
fn part1(input: &str) -> RiddleResult {
let garden = Grid::parse(input);
let mut processed: HashSet<Coord> = HashSet::new();
let mut result = 0;
for (p, c) in garden.entries() {
if processed.contains(&p) {
continue;
}
let mut area = 0;
let mut fence = 0;
let mut further = vec![p];
while let Some(current) = further.pop() {
if processed.contains(&current) {
continue;
}
area += 1;
for next in NEIGHBORS.iter().map(|d| (current.0 + d.0, current.1 + d.1)) {
let next_c = garden.get(next);
if next_c.is_none() || next_c.unwrap() != c {
fence += 1;
} else {
further.push(next);
}
}
processed.insert(current);
}
result += area * fence;
}
result
}
fn part2(input: &str) -> RiddleResult {
let garden = Grid::parse(input);
let mut processed: HashSet<Coord> = HashSet::new();
let mut result = 0;
for (p, c) in garden.entries() {
if processed.contains(&p) {
continue;
}
let mut area = 0;
let mut further = vec![p];
let mut group = HashSet::new();
while let Some(current) = further.pop() {
if processed.contains(&current) {
continue;
}
group.insert(current);
area += 1;
for next in NEIGHBORS.iter().map(|d| (current.0 + d.0, current.1 + d.1)) {
let next_c = garden.get(next);
if next_c.is_none() || next_c.unwrap() != c {
} else {
further.push(next);
}
}
processed.insert(current);
}
result += area * sides(group);
}
result
}
fn sides(group: HashSet<(i64, i64)>) -> RiddleResult {
let x_range = group.iter().min_by_key(|it| it.0).unwrap().0
..=group.iter().max_by_key(|it| it.0).unwrap().0;
let y_range = group.iter().min_by_key(|it| it.1).unwrap().1
..=group.iter().max_by_key(|it| it.1).unwrap().1;
let mut result = 0;
for x in x_range {
// 1 for the ending of the last group, +1 for each time a line "stops", e.g. between the current point and next there is a gap
let points = group
.iter()
.filter(|p| p.0 == x)
.filter(|p| !group.contains(&(p.0 - 1, p.1)))
.sorted_by_key(|p| p.1)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.1 + 1 != b.1)
.count();
result += 1 + count;
}
let points = group
.iter()
.filter(|p| p.0 == x)
.filter(|p| !group.contains(&(p.0 + 1, p.1)))
.sorted_by_key(|p| p.1)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.1 + 1 != b.1)
.count();
result += 1 + count;
}
}
for y in y_range {
let points = group
.iter()
.filter(|p| p.1 == y)
.filter(|p| !group.contains(&(p.0, p.1 - 1)))
.sorted_by_key(|p| p.0)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.0 + 1 != b.0)
.count();
result += 1 + count;
}
let points = group
.iter()
.filter(|p| p.1 == y)
.filter(|p| !group.contains(&(p.0, p.1 + 1)))
.sorted_by_key(|p| p.0)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.0 + 1 != b.0)
.count();
result += 1 + count;
}
}
result as RiddleResult
}
#[cfg(test)]
mod test {
use std::collections::HashSet;
use crate::day12::sides;
use super::{part1, part2};
const TEST_INPUT: &str = r"AAAA
BBCD
BBCC
EEEC
";
#[test]
fn test1() {
assert_eq!(part1(TEST_INPUT), 140);
}
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 80);
}
#[test]
fn sides_one() {
let group = HashSet::from_iter([(3, 5)]);
assert_eq!(sides(group), 4);
}
#[test]
fn sides_plus() {
let group = HashSet::from_iter([(3, 3), (3, 4), (3, 2), (2, 3), (4, 3)]);
assert_eq!(sides(group), 12);
}
}

View File

@@ -9,5 +9,6 @@ pub mod day08;
pub mod day09; pub mod day09;
pub mod day10; pub mod day10;
pub mod day11; pub mod day11;
pub mod day12;
// PLACEHOLDER // PLACEHOLDER
pub mod utils; pub mod utils;

View File

@@ -16,6 +16,7 @@ fn main() {
(9, day09::day_main), (9, day09::day_main),
(10, day10::day_main), (10, day10::day_main),
(11, day11::day_main), (11, day11::day_main),
(12, day12::day_main),
// PLACEHOLDER // PLACEHOLDER
]); ]);
let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok()); let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok());