-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd12.rs
More file actions
105 lines (86 loc) · 2.46 KB
/
d12.rs
File metadata and controls
105 lines (86 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::Day;
pub struct Day12 {}
impl Day for Day12 {
fn year(&self) -> u16 {
2025
}
fn day(&self) -> u8 {
12
}
fn part_one(&self) -> String {
let input = self.read_default_input();
let (regions, presents) = parse_input(&input);
let mut result = 0;
for region in regions {
let region_area = region.width * region.height;
let required_area = region
.presents_count
.iter()
.enumerate()
.map(|(present_idx, count)| presents[present_idx].area * count)
.sum::<usize>();
if required_area <= region_area {
result += 1;
}
}
result.to_string()
}
fn part_two(&self) -> String {
"-".to_string()
}
}
#[derive(Debug)]
struct Region {
width: usize,
height: usize,
presents_count: [usize; 6],
}
struct Present {
_shape: Vec<bool>,
area: usize,
}
fn parse_input(input: &str) -> (Vec<Region>, Vec<Present>) {
let sections = input.split("\n\n").collect::<Vec<_>>();
let (regions_input, presents_input) = sections.split_last().unwrap();
let presents = presents_input
.iter()
.map(|present_raw| {
let shape = present_raw
.lines()
.skip(1)
.flat_map(|line| line.chars().map(|ch| ch == '#').collect::<Vec<_>>())
.collect::<Vec<bool>>();
let area = shape.iter().filter(|a| **a).count();
Present {
_shape: shape,
area,
}
})
.collect::<Vec<Present>>();
let mut regions = vec![];
for region_raw in regions_input.lines() {
let (dimensions, counts) = region_raw.split_once(':').unwrap();
let (width, height) = dimensions
.split_once('x')
.map(|(width, height)| {
(
width.parse::<usize>().unwrap(),
height.parse::<usize>().unwrap(),
)
})
.unwrap();
let presents_count: [usize; 6] = counts
.trim()
.split(" ")
.map(|count| count.parse::<usize>().unwrap())
.collect::<Vec<_>>()
.try_into()
.unwrap();
regions.push(Region {
width,
height,
presents_count,
});
}
(regions, presents)
}