-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
165 lines (147 loc) · 5.69 KB
/
main.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
use std::fs;
use std::cmp;
use std::ops::Range;
#[derive(Debug)]
struct Map {
dst: u64,
src: u64,
len: u64,
}
impl Map {
fn from_line(line: &str) -> Map {
let numbers: Vec<u64> = line.split(' ').map(|s| s.parse::<u64>().unwrap()).collect();
if numbers.len() != 3 {
panic!("Expected three numbers in a line!");
}
Map {dst: numbers[0], src: numbers[1], len: numbers[2]}
}
}
#[derive(Debug)]
struct Almanac {
seed_to_soil: Vec<Map>,
soil_to_fertilizer: Vec<Map>,
fertilizer_to_water: Vec<Map>,
water_to_light: Vec<Map>,
light_to_temperature: Vec<Map>,
temperature_to_humidity: Vec<Map>,
humidity_to_location: Vec<Map>,
}
impl Almanac {
fn parse_map(text: &str, name: &str) -> Vec<Map> {
let start_index = text.find(name).unwrap();
let end_index = if let Some(length) = text[start_index..].find("\n\n") {
start_index + length
} else {
text.len()
};
let mapping_str = &text[start_index..end_index];
mapping_str.lines().skip(1).map(|line| Map::from_line(line)).collect()
}
fn parse_almanac(text: &str) -> Almanac {
Almanac {
seed_to_soil: Almanac::parse_map(text, "seed-to-soil"),
soil_to_fertilizer: Almanac::parse_map(text, "soil-to-fertilizer"),
fertilizer_to_water: Almanac::parse_map(text, "fertilizer-to-water"),
water_to_light: Almanac::parse_map(text, "water-to-light"),
light_to_temperature: Almanac::parse_map(text, "light-to-temperature"),
temperature_to_humidity: Almanac::parse_map(text, "temperature-to-humidity"),
humidity_to_location: Almanac::parse_map(text, "humidity-to-location"),
}
}
fn map_seeds_single_step(mappings: &Vec<Map>, seeds: Range<u64>) -> (Range<u64>, Option<Range<u64>>) {
for mapping in mappings {
if seeds.start >= mapping.src && seeds.start < mapping.src + mapping.len {
let end: u64 = cmp::min(seeds.end, mapping.src + mapping.len - 1);
let range = Range {
start: (seeds.start - mapping.src) + mapping.dst,
end: (end - mapping.src) + mapping.dst
};
let remainder = if end != seeds.end {
Some(Range {
start: end + 1,
end: seeds.end
})
} else {
None
};
return (range, remainder);
}
}
return (seeds, None);
}
fn map_seeds_vector_single_step(mappings: &Vec<Map>, list_of_seeds: Vec<Range<u64>>) -> Vec<Range<u64>> {
let mut output: Vec<Range<u64>> = Vec::new();
for seeds in list_of_seeds {
let mut next: Option<Range<u64>> = Some(seeds);
while let Some(current) = next {
let out = Almanac::map_seeds_single_step(mappings, current);
output.push(out.0);
next = out.1;
}
}
output
}
fn map_seeds(&self, seeds: Vec<Range<u64>>) -> Vec<Range<u64>> {
let soil = Almanac::map_seeds_vector_single_step(&self.seed_to_soil, seeds);
let fertilizer = Almanac::map_seeds_vector_single_step(&self.soil_to_fertilizer, soil);
let water = Almanac::map_seeds_vector_single_step(&self.fertilizer_to_water, fertilizer);
let light = Almanac::map_seeds_vector_single_step(&self.water_to_light, water);
let temperature = Almanac::map_seeds_vector_single_step(&self.light_to_temperature, light);
let humidity = Almanac::map_seeds_vector_single_step(&self.temperature_to_humidity, temperature);
let location = Almanac::map_seeds_vector_single_step(&self.humidity_to_location, humidity);
location
}
}
fn parse_seeds(text: &str) -> Vec<Range<u64>> {
let first_line: &str = text.lines().next().unwrap();
let seeds_as_str: &str = &first_line["seeds: ".len()..].trim();
seeds_as_str.split(' ').map(|s| s.parse::<u64>().unwrap())
.map(|value| Range { start: value, end: value }).collect()
}
fn part1(filepath: &str) -> u64 {
let input = fs::read_to_string(filepath)
.expect(&format!("Couldn't read the file '{}'", filepath));
let seeds = parse_seeds(&input);
let almanac = Almanac::parse_almanac(&input);
almanac.map_seeds(seeds).iter().map(|r| r.start).min().unwrap()
}
fn parse_seeds_as_ranges(text: &str) -> Vec<Range<u64>> {
let first_line: &str = text.lines().next().unwrap();
let seeds_as_str: &str = &first_line["seeds: ".len()..].trim();
let numbers: Vec<u64> = seeds_as_str.split(' ').map(|s| s.parse::<u64>().unwrap()).collect();
if numbers.len() % 2 != 0 {
panic!("Expected seed numbers to come in pairs!");
}
numbers.chunks(2).map(|chunk| Range { start: chunk[0], end: chunk[0] + chunk[1]}).collect()
}
fn part2(filepath: &str) -> u64 {
let input = fs::read_to_string(filepath)
.expect(&format!("Couldn't read the file '{}'", filepath));
let seeds = parse_seeds_as_ranges(&input);
let almanac = Almanac::parse_almanac(&input);
almanac.map_seeds(seeds).iter().map(|r| r.start).min().unwrap()
}
fn main() {
println!("Part 01: {}", part1("input.txt"));
println!("Part 02: {}", part2("input.txt"));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
assert_eq!(part1("sample.txt"), 35);
}
#[test]
fn result_part1() {
assert_eq!(part1("input.txt"), 323142486);
}
#[test]
fn test_part2() {
assert_eq!(part2("sample.txt"), 46);
}
#[test]
fn result_part2() {
assert_eq!(part2("input.txt"), 79874951);
}
}