159 lines
4.3 KiB
Rust
159 lines
4.3 KiB
Rust
use std::collections::{HashMap, VecDeque};
|
|
use std::ops::Range;
|
|
use crate::DataType::Soil;
|
|
|
|
const FILE_STR: &'static str = include_str!("input.txt");
|
|
const EXAMPLE_FILE_STR: &'static str = include_str!("example-input.txt");
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
|
enum DataType {
|
|
Seed,
|
|
Soil,
|
|
Fertilizer,
|
|
Water,
|
|
Light,
|
|
Temperature,
|
|
Humidity,
|
|
Location,
|
|
}
|
|
|
|
impl DataType {
|
|
fn try_from(s: &str) -> Option<Self> {
|
|
match s {
|
|
"seed" => Some(Self::Seed),
|
|
"soil" => Some(Self::Soil),
|
|
"fertilizer" => Some(Self::Fertilizer),
|
|
"water" => Some(Self::Water),
|
|
"light" => Some(Self::Light),
|
|
"temperature" => Some(Self::Temperature),
|
|
"humidity" => Some(Self::Humidity),
|
|
"location" => Some(Self::Location),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
struct DataMap {
|
|
from_range: Range<u64>,
|
|
to_range: Range<u64>,
|
|
}
|
|
|
|
impl DataMap {
|
|
fn new(from_start: u64, to_start: u64, map_length: u64) -> Self {
|
|
let to_range = to_start..to_start + map_length;
|
|
let from_range = from_start..from_start + map_length;
|
|
|
|
DataMap {
|
|
from_range,
|
|
to_range,
|
|
}
|
|
}
|
|
|
|
fn get_dest_idx(&self, src_value: u64) -> u64 {
|
|
let src_index = src_value.checked_sub(self.to_range.start);
|
|
let dest_index = if let Some(idx) = src_index {
|
|
self.from_range.start.checked_add(idx)
|
|
} else {
|
|
Some(src_value)
|
|
};
|
|
|
|
match dest_index {
|
|
Some(n) => n,
|
|
None => src_value,
|
|
}
|
|
}
|
|
|
|
fn get_src_idx(&self, dest_value: u64) -> u64 {
|
|
let dest_index = dest_value.checked_sub(self.from_range.start);
|
|
let src_index = if let Some(idx) = dest_index {
|
|
self.to_range.start.checked_add(idx)
|
|
} else {
|
|
Some(dest_value)
|
|
};
|
|
|
|
match src_index {
|
|
Some(n) => n,
|
|
None => dest_value,
|
|
}
|
|
}
|
|
}
|
|
|
|
type MapMap = HashMap<(DataType, DataType), Vec<DataMap>>;
|
|
|
|
#[derive(Debug, Default)]
|
|
struct Almanac {
|
|
seeds: Vec<u64>,
|
|
maps: MapMap,
|
|
}
|
|
|
|
impl Almanac {
|
|
fn parse(input: &str) -> Self {
|
|
let mut data_chunks: VecDeque<_> = input.split("\n\n").collect();
|
|
let seeds: Vec<u64> = data_chunks
|
|
.pop_front()
|
|
.unwrap()
|
|
.split(':')
|
|
.collect::<Vec<&str>>()
|
|
.pop()
|
|
.unwrap()
|
|
.split_whitespace()
|
|
.map(|s| s.trim())
|
|
.map(|s| s.parse::<u64>().unwrap())
|
|
.collect();
|
|
|
|
let maps = data_chunks
|
|
.into_iter()
|
|
.map(|chunk| {
|
|
let mut lines: VecDeque<_> = chunk.split("\n").collect();
|
|
let type_line_parts: Vec<_> =
|
|
lines.pop_front().unwrap().split_whitespace().collect();
|
|
let types: Vec<_> = type_line_parts[0]
|
|
.split("-to-")
|
|
.map(|s| DataType::try_from(s.trim()).unwrap())
|
|
.collect();
|
|
let (from, to) = (types[0], types[1]);
|
|
let mappings: Vec<DataMap> = lines
|
|
.into_iter()
|
|
.map(|line| {
|
|
line.split_whitespace()
|
|
.map(|s| s.trim())
|
|
.filter(|s| s.len() > 0)
|
|
.map(|s| s.parse::<u64>().unwrap())
|
|
.collect::<Vec<u64>>()
|
|
})
|
|
.map(|m| DataMap::new(m[0], m[1], m[2]))
|
|
.collect();
|
|
|
|
((from, to), mappings)
|
|
})
|
|
.collect();
|
|
|
|
Almanac { seeds, maps }
|
|
}
|
|
|
|
fn x_from_y(&self, x: DataType, y: DataType, search: u64) -> u64 {
|
|
let maps = self.maps.get(&(y, x)).expect(&format!("Missing mapping from {:?} to {:?}", x, y));
|
|
todo!();
|
|
}
|
|
|
|
fn soil_from_seed(&self, seed: u64) -> u64 {
|
|
use DataType::*;
|
|
|
|
self.x_from_y(Soil, Seed, seed)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let almanac = Almanac::parse(EXAMPLE_FILE_STR);
|
|
let soil = almanac.soil_from_seed(79);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
const EXAMPLE_FILE_STR: &'static str = include_str!("example-input.txt");
|
|
|
|
#[test]
|
|
fn test_parse() {}
|
|
}
|