-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd05.rs
More file actions
156 lines (131 loc) Β· 4.44 KB
/
d05.rs
File metadata and controls
156 lines (131 loc) Β· 4.44 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
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
use crate::Day;
pub struct Day05 {}
impl Day for Day05 {
fn year(&self) -> u16 {
2023
}
fn day(&self) -> u8 {
5
}
fn part_one(&self) -> String {
let almanac = Almanac::from(self.read_default_input().as_str());
let mut closest_location = i64::MAX;
for seed in &almanac.seeds {
let location = almanac.traverse_categories(Direction::Normal, *seed);
if location < closest_location {
closest_location = location;
}
}
closest_location.to_string()
}
fn part_two(&self) -> String {
let almanac = Almanac::from(self.read_default_input().as_str());
let mut closest_location = 0;
let seed_ranges: Vec<(i64, i64)> = almanac.seeds_as_ranges();
'outer: loop {
let seed = almanac.traverse_categories(Direction::Reverse, closest_location);
for (range_start, range_end) in &seed_ranges {
if seed >= *range_start && seed < *range_end {
break 'outer;
}
}
closest_location += 1;
}
closest_location.to_string()
}
}
enum Direction {
Normal,
Reverse,
}
struct Almanac {
seeds: Vec<i64>,
steps: Vec<Vec<(i64, i64, i64)>>,
}
impl Almanac {
fn seeds_as_ranges(&self) -> Vec<(i64, i64)> {
self.seeds
.chunks(2)
.map(|chunk| (chunk[0], chunk[0] + chunk[1]))
.collect()
}
fn traverse_categories(&self, direction: Direction, input: i64) -> i64 {
match direction {
Direction::Normal => self.traverse_categories_recursion(direction, -1, input),
Direction::Reverse => self.traverse_categories_recursion(direction, 7, input),
}
}
fn traverse_categories_recursion(
&self,
direction: Direction,
current_step: i8,
input: i64,
) -> i64 {
let next_step_idx = match direction {
Direction::Normal => current_step + 1,
Direction::Reverse => current_step - 1,
};
if !(0..=6).contains(&next_step_idx) {
return input;
}
let next_step_idx = next_step_idx as usize;
let next_step = &self.steps[next_step_idx];
let mut output = input;
for (destination_start, source_start, length) in next_step {
let (left, right) = match direction {
Direction::Normal => (source_start, destination_start),
Direction::Reverse => (destination_start, source_start),
};
if input >= *left && input < left + length {
let distance = input - left;
output = right + distance;
}
}
self.traverse_categories_recursion(direction, next_step_idx as i8, output)
}
}
impl From<&str> for Almanac {
fn from(value: &str) -> Self {
let mut seeds: Vec<i64> = vec![];
let mut steps: Vec<Vec<(i64, i64, i64)>> = Vec::new();
for _ in 0..7 {
steps.push(Vec::new());
}
for map in value.split("\n\n") {
if map.starts_with("seeds:") {
seeds = map
.split(':')
.next_back()
.unwrap()
.trim()
.split(' ')
.map(|number| number.parse::<i64>().unwrap())
.collect::<Vec<i64>>();
continue;
}
let map_lines = map.lines().collect::<Vec<&str>>();
let step_idx = match map_lines[0] {
"seed-to-soil map:" => 0,
"soil-to-fertilizer map:" => 1,
"fertilizer-to-water map:" => 2,
"water-to-light map:" => 3,
"light-to-temperature map:" => 4,
"temperature-to-humidity map:" => 5,
"humidity-to-location map:" => 6,
_ => panic!("invalid map"),
};
let ranges = map_lines[1..]
.iter()
.map(|item| {
item.trim()
.split(' ')
.map(|number| number.parse::<i64>().unwrap())
.collect::<Vec<i64>>()
})
.map(|range| (range[0], range[1], range[2]))
.collect::<Vec<(i64, i64, i64)>>();
steps[step_idx] = ranges;
}
Self { seeds, steps }
}
}