-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
176 lines (151 loc) · 4.26 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
use std::fs::File;
use std::io::prelude::*;
use std::error::Error;
fn split_numbers(line: String) -> Vec<i64>
{
return line
.split_whitespace()
.map(|s| s.parse().unwrap())
.collect();
}
pub fn part1() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day5/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lines: Vec<_> = contents.lines().collect();
let mut maps: Vec<Vec<Vec<i64>>> = Vec::new();
let mut m: i64 = -1;
for l in 2..lines.len()
{
let line = lines[l];
if line.trim().is_empty()
{
continue;
}
let chars: Vec<_> = line.chars().collect();
if chars[0].is_digit(10)
{
maps[m as usize].push(split_numbers(line.to_string()));
}
else
{
maps.push(Vec::new());
m += 1;
}
}
let seeds = split_numbers(lines[0].replace("seeds: ", ""));
let mut min_location = std::i64::MAX;
for seed in seeds
{
let mut source = seed;
for map in &maps
{
for range in map
{
if source >= range[1] && source < range[1] + range[2]
{
source = range[0] + source - range[1];
break;
}
}
}
min_location = std::cmp::min(min_location, source);
}
return Ok(min_location);
}
pub fn part2() -> Result<i64, Box<dyn Error>>
{
let mut file = File::open("src/day5/input.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lines: Vec<&str> = contents.lines().collect();
// Maps of ranges in the form ((src_start, src_end), (dest_start, dest_end))
let mut maps: Vec<Vec<((i64, i64), (i64, i64))>> = Vec::new();
let mut m: i64 = -1;
for l in 2..lines.len()
{
let line = lines[l];
if line.trim().is_empty()
{
continue;
}
let chars: Vec<char> = line.chars().collect();
if chars[0].is_digit(10)
{
let split = split_numbers(line.to_string());
// ingest ranges as ((src_start, src_end), (dest_start, dest_end))
maps[m as usize].push((
(split[1], split[1] + split[2] - 1),
(split[0], split[0] + split[2] - 1)
));
}
else
{
maps.push(Vec::new());
m += 1;
}
}
let split = split_numbers(lines[0].replace("seeds: ", ""));
// (start, end)
let mut seed_ranges: Vec<_> = split.chunks(2).map(|c| (c[0], c[0] + c[1] - 1)).collect();
let mut min_location = std::i64::MAX;
for map in maps
{
let mut new_range: Vec<(i64, i64)> = Vec::new();
for range in &seed_ranges
{
new_range.extend(&map_range(&map, range));
}
seed_ranges = new_range;
}
for range in seed_ranges
{
min_location = std::cmp::min(min_location, range.0);
}
return Ok(min_location);
}
fn map_range(map: &Vec<((i64, i64), (i64, i64))>, range: &(i64, i64)) -> Vec<(i64, i64)>
{
let mut destinations: Vec<(i64, i64)> = Vec::new();
for (src, dest) in map
{
// [ { } ]
if range.0 >= src.0 && range.1 <= src.1
{
destinations.push((dest.0 + range.0 - src.0, dest.1 + range.1 - src.1));
}
// [ { ] }
else if range.0 >= src.0 && range.0 <= src.1
{
let inner_range = (dest.0 + range.0 - src.0, dest.1);
destinations.push(inner_range);
destinations.extend(map_range(map, &(src.1 + 1, range.1)));
}
// { [ } ]
else if range.1 <= src.1 && range.1 >= src.0
{
let inner_range = (dest.0, dest.0 + range.1 - src.0);
destinations.push(inner_range);
destinations.extend(map_range(map, &(range.0, src.0 - 1)));
}
// { } [ ]
else {}
}
if destinations.len() == 0
{
destinations.push(*range);
}
return destinations;
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_parts()
{
assert_eq!(part1().ok(), Some(35));
assert_eq!(part2().ok(), Some(46));
}
}