Solve 12
This commit is contained in:
parent
180bdf9ce2
commit
69ea94bec0
1 changed files with 82 additions and 0 deletions
82
src/bin/12.rs
Normal file
82
src/bin/12.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
use aoc::*;
|
||||||
|
use glam::USizeVec2;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = include_str!("../../input/12");
|
||||||
|
println!("Part 1: {}", part1(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> usize {
|
||||||
|
let (presents, regions) = parse_input(input);
|
||||||
|
regions.into_iter().filter(|r| r.fits(&presents)).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> (Vec<Present>, Vec<Region>) {
|
||||||
|
let chunks = input.split("\n\n").collect_vec();
|
||||||
|
let (presents, regions) = chunks.split_at(chunks.len() - 1);
|
||||||
|
let presents = presents.iter().map(|p| Present::parse(p)).collect();
|
||||||
|
let regions = regions
|
||||||
|
.iter()
|
||||||
|
.flat_map(|chunk| chunk.lines())
|
||||||
|
.map(Region::parse)
|
||||||
|
.collect();
|
||||||
|
(presents, regions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct Present {
|
||||||
|
area: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Present {
|
||||||
|
fn parse(input: &str) -> Self {
|
||||||
|
let area = input
|
||||||
|
.lines()
|
||||||
|
.skip(1)
|
||||||
|
.flat_map(|line| line.chars())
|
||||||
|
.filter(|&c| c == '#')
|
||||||
|
.count();
|
||||||
|
Self { area }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Region {
|
||||||
|
size: USizeVec2,
|
||||||
|
presents: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Region {
|
||||||
|
fn fits(&self, presents: &[Present]) -> bool {
|
||||||
|
let total_present_area: usize = self
|
||||||
|
.pick_presents(presents)
|
||||||
|
.into_iter()
|
||||||
|
.map(|(n, present)| n * present.area)
|
||||||
|
.sum();
|
||||||
|
total_present_area <= self.area()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pick_presents(&self, presents: &[Present]) -> Vec<(usize, Present)> {
|
||||||
|
self.presents
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, n)| (n, presents[i]))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn area(&self) -> usize {
|
||||||
|
self.size.x * self.size.y
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(line: &str) -> Self {
|
||||||
|
let (size, presents) = line.split_once(": ").unwrap();
|
||||||
|
|
||||||
|
let (x, y) = size.split_once('x').unwrap();
|
||||||
|
let size = USizeVec2::new(x.parse().unwrap(), y.parse().unwrap());
|
||||||
|
|
||||||
|
let presents = parse_ws_separated(presents).collect();
|
||||||
|
|
||||||
|
Self { size, presents }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue