-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
185 lines (151 loc) · 3.89 KB
/
mod.rs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use anyhow::{Context, Error, Result};
use rayon::{slice::ParallelSlice, iter::ParallelIterator};
use std::str::FromStr;
use crate::utils::AocError::*;
#[derive(Debug)]
pub struct Almanac {
seed_map: Vec<SeedMap>,
seeds: Vec<i128>,
}
impl FromStr for Almanac {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let seed_map = s
.split("\n\n")
.filter(|s| !s.is_empty())
.skip(1)
.map(SeedMap::from_str)
.collect::<Result<Vec<_>>>()
.context("Error while parsing input")?;
let seeds = s.lines().next()
.ok_or(GenericError).context("Could not find line with seeds")?
.split(": ")
.nth(1)
.ok_or(GenericError).context("Could not find seeds")?
.split(' ')
.map(|v| Ok(v.trim().parse::<i128>()?))
.collect::<Result<Vec<_>>>()?;
Ok( Almanac { seed_map, seeds })
}
}
impl Almanac {
pub fn map_seed(&self, seed: i128) -> i128 {
let mut seed = seed;
for map in &self.seed_map {
if let Some(v) = map.map_seed(seed) {
seed = v;
}
}
seed
}
}
#[derive(Debug)]
pub struct SeedMap {
ranges: Vec<Range>,
}
impl FromStr for SeedMap {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let ranges = s
.lines()
.skip(1)
.map(Range::from_str)
.collect::<Result<Vec<_>>>()?;
Ok(SeedMap { ranges })
}
}
impl SeedMap {
pub fn map_seed(&self, seed: i128) -> Option<i128> {
self.ranges.iter().filter_map(|r| r.map(seed)).next()
}
}
#[derive(Debug)]
pub struct Range {
destination_start: i128,
source_start: i128,
length: i128,
}
impl FromStr for Range {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let values = s.split(' ')
.map(|v| Ok(v.trim().parse::<i128>()?))
.collect::<Result<Vec<_>>>()?;
if values.len() != 3 {
return Err(GenericError).context("Could not parse range")?
}
Ok(Range { destination_start: values[0], source_start: values[1], length: values[2] })
}
}
impl Range {
pub fn map(&self, seed: i128) -> Option<i128> {
if self.source_start <= seed && seed < self.source_start + self.length {
Some(self.destination_start + seed - self.source_start)
} else {
None
}
}
}
#[aoc_generator(day05)]
pub fn input_generator(input: &str) -> Result<Almanac> {
Almanac::from_str(input)
}
#[aoc(day05, part1)]
pub fn solve_part1(input: &Almanac) -> Result<i128> {
let result = input.seeds.iter().map(|s| input.map_seed(*s)).collect::<Vec<_>>();
Ok(*result.iter().min().ok_or(GenericError).context("Could not find lowest location")?)
}
#[aoc(day05, part2)]
pub fn solve_part2(input: &Almanac) -> Result<i128> {
let min = input.seeds.par_chunks_exact(2).filter_map(|r| {
let start = r[0];
let end = r[0] + r[1];
(start..end).map(|s| input.map_seed(s)).min()
}).min().ok_or(GenericError).context("Foo")?;
Ok(min)
}
#[cfg(test)]
mod test {
use super::*;
fn sample() -> &'static str {
"seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4"
}
fn input() -> Result<Almanac> {
input_generator(sample())
}
#[test]
fn part1_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(35, solve_part1(&data)?))
}
#[test]
fn part2_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(46, solve_part2(&data)?))
}
}