This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.rs
162 lines (129 loc) · 4.29 KB
/
05.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
use std::{ops::Range, str::Lines};
use rayon::prelude::*;
advent_of_code::solution!(5);
#[derive(Debug)]
struct Map {
source_range: Range<usize>,
offset: i64,
}
#[derive(Debug)]
struct Almanac {
seeds: Vec<usize>,
seed_soil: Vec<Map>,
soil_fertilizer: Vec<Map>,
fertilizer_water: Vec<Map>,
water_light: Vec<Map>,
light_temperature: Vec<Map>,
temperature_humidity: Vec<Map>,
humidity_location: Vec<Map>,
}
impl Map {
fn send(&self, x: usize) -> usize {
if self.source_range.contains(&x) {
(x as i64 + self.offset) as usize
} else {
x
}
}
fn contains(&self, x: usize) -> bool {
self.source_range.contains(&x)
}
}
impl Almanac {
fn take_map(&self, x: usize, mappings: &Vec<Map>) -> usize {
for map in mappings {
if map.contains(x) {
return map.send(x);
}
}
x
}
fn get_location(&self, seed: usize) -> usize {
let soil = self.take_map(seed, &self.seed_soil);
let fertilizer = self.take_map(soil, &self.soil_fertilizer);
let water = self.take_map(fertilizer, &self.fertilizer_water);
let light = self.take_map(water, &self.water_light);
let temperature = self.take_map(light, &self.light_temperature);
let humidity = self.take_map(temperature, &self.temperature_humidity);
let location = self.take_map(humidity, &self.humidity_location);
location
}
}
fn parse_ranges(input: &mut Lines) -> Vec<Map> {
input.next();
let mut ranges: Vec<Map> = vec![];
while let Some(s) = input.next() {
if s.is_empty() {
break;
}
let parts : Vec<usize> = s.split(" ").map(|n| n.parse().unwrap()).collect();
let offset: i64 = (parts[0] as i64) - (parts[1] as i64);
let source_range: Range<usize> = parts[1]..(parts[1]+parts[2]);
ranges.push(Map { source_range, offset });
}
ranges.sort_by_key(|m| m.source_range.start);
ranges
}
fn parse(input: &str) -> Almanac {
let mut lines = input.lines();
let first_line = lines.next().unwrap();
lines.next();
let seeds : Vec<usize> = first_line.split(": ")
.collect::<Vec<&str>>()[1]
.split(" ")
.map(|s| s.parse().unwrap())
.collect();
let seed_soil = parse_ranges(&mut lines);
let soil_fertilizer = parse_ranges(&mut lines);
let fertilizer_water = parse_ranges(&mut lines);
let water_light = parse_ranges(&mut lines);
let light_temperature = parse_ranges(&mut lines);
let temperature_humidity = parse_ranges(&mut lines);
let humidity_location = parse_ranges(&mut lines);
Almanac { seeds,
seed_soil,
soil_fertilizer,
fertilizer_water,
water_light,
light_temperature,
temperature_humidity,
humidity_location
}
}
pub fn part_one(input: &str) -> Option<u32> {
let almanac = parse(input);
let result = almanac.seeds
.iter()
.map(|&s| almanac.get_location(s))
.min()
.unwrap();
Some(result as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
let almanac = parse(input);
let seed_ranges : Vec<_> = almanac.seeds
.chunks(2)
.into_iter()
.map(|pair| pair[0]..(pair[0]+pair[1]))
.collect();
let result = seed_ranges.par_iter()
.flat_map(|r| r.clone().into_iter())
.map(|s| almanac.get_location(s))
.min()
.unwrap();
Some(result as u32)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(35));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(46));
}
}