-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.rs
168 lines (147 loc) · 3.79 KB
/
day05.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
use std::ops::Range;
use crate::data::load;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum PuzzleErr {
#[error("Integer parsing error: '{}'.", .0)]
IntParseError(String),
}
#[derive(Debug, Clone)]
struct MapRange {
source_start: u32,
dest_start: u32,
range: Range<u32>,
}
impl MapRange {
fn new(source_start: u32, dest_start: u32, len: u32) -> Self {
Self {
source_start,
dest_start,
range: source_start..(source_start + len),
}
}
fn contains(&self, x: &u32) -> bool {
self.range.contains(x)
}
}
#[derive(Debug, Clone)]
struct Map {
ranges: Vec<MapRange>,
}
fn line_to_range(line: &str) -> MapRange {
let vals = line
.split_whitespace()
.map(|s| s.parse::<u32>().unwrap())
.collect::<Vec<_>>();
MapRange::new(vals[1], vals[0], vals[2])
}
impl Map {
fn new(ranges: Vec<MapRange>) -> Self {
Self { ranges }
}
fn translate(&self, source_val: &u32) -> u32 {
for r in self.ranges.iter() {
if r.contains(source_val) {
return source_val - r.source_start + r.dest_start;
}
}
*source_val
}
}
#[derive(Debug, Clone)]
struct Almanac {
seeds: Vec<u32>,
maps: Vec<Map>,
}
impl Almanac {
fn apply_maps(&self, x: &u32) -> u32 {
let mut res = *x;
for map in self.maps.iter() {
res = map.translate(&res);
}
res
}
}
fn get_seeds(input: &str) -> Result<Vec<u32>, PuzzleErr> {
input
.trim()
.to_string()
.lines()
.collect::<Vec<_>>()
.first()
.unwrap()
.trim()
.split("seeds: ")
.collect::<Vec<_>>()[1]
.split_whitespace()
.map(|s| {
s.parse::<u32>()
.or(Err(PuzzleErr::IntParseError(s.to_string())))
})
.collect::<Result<Vec<_>, PuzzleErr>>()
}
fn get_maps(input: &str) -> Result<Vec<Map>, PuzzleErr> {
let mut maps = Vec::new();
let mut map_ranges = Vec::new();
for line in input.trim().lines().skip(2).map(|l| l.trim()) {
if line.contains("map:") {
continue;
} else if line.is_empty() {
maps.push(Map::new(map_ranges.clone()));
map_ranges.clear();
} else {
map_ranges.push(line_to_range(line));
}
}
if !map_ranges.is_empty() {
maps.push(Map::new(map_ranges.clone()))
}
Ok(maps)
}
fn parse_input(input: &str) -> Result<Almanac, PuzzleErr> {
let seeds = get_seeds(input)?; //[1..2].to_vec();
let maps = get_maps(input)?;
Ok(Almanac { seeds, maps })
}
pub fn puzzle_1(input: &str) -> Result<u32, PuzzleErr> {
let almanac = parse_input(input)?;
Ok(almanac
.seeds
.iter()
.map(|x| almanac.apply_maps(x))
.min()
.unwrap())
}
pub fn puzzle_2(input: &str) -> Result<u32, PuzzleErr> {
let almanac = parse_input(input)?;
Ok(almanac
.seeds
.windows(2)
.step_by(2)
.map(|x| {
(x[0]..(x[0] + x[1]))
.map(|y| almanac.apply_maps(&y))
.min()
.unwrap()
})
.min()
.unwrap())
}
pub fn main(data_dir: &str) {
println!("Day 5: If You Give A Seed A Fertilizer");
let data = load(data_dir, 5, None);
// Puzzle 1.
let answer_1 = puzzle_1(&data);
match answer_1 {
Ok(x) => println!(" Puzzle 1: {}", x),
Err(e) => panic!("No solution to puzzle 1: {}.", e),
}
assert_eq!(answer_1, Ok(650599855));
// Puzzle 2.
let answer_2 = puzzle_2(&data);
match answer_2 {
Ok(x) => println!(" Puzzle 2: {}", x),
Err(e) => panic!("No solution to puzzle 2: {}", e),
}
assert_eq!(answer_2, Ok(1240035))
}