Parse the input for 2023 day5 part 1

This commit is contained in:
Timothy Warren 2023-12-11 14:55:46 -05:00
parent 05f4a22e69
commit 36320e90bc
3 changed files with 86 additions and 10 deletions

View File

@ -1,10 +1,13 @@
use std::collections::{HashMap, VecDeque};
use std::ops::Range; use std::ops::Range;
use std::collections::HashMap; use crate::DataType::Soil;
const FILE_STR: &'static str = include_str!("input.txt");
#[derive(Debug)] 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 { enum DataType {
Seeds, Seed,
Soil, Soil,
Fertilizer, Fertilizer,
Water, Water,
@ -14,6 +17,22 @@ enum DataType {
Location, 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)] #[derive(Debug, Default)]
struct DataMap { struct DataMap {
from_range: Range<u64>, from_range: Range<u64>,
@ -27,7 +46,7 @@ impl DataMap {
DataMap { DataMap {
from_range, from_range,
to_range to_range,
} }
} }
@ -41,7 +60,7 @@ impl DataMap {
match dest_index { match dest_index {
Some(n) => n, Some(n) => n,
None => src_value None => src_value,
} }
} }
@ -55,28 +74,85 @@ impl DataMap {
match src_index { match src_index {
Some(n) => n, Some(n) => n,
None => dest_value None => dest_value,
} }
} }
} }
type MapMap = HashMap<(DataType, DataType), Vec<DataMap>>;
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct Almanac { struct Almanac {
seeds: Vec<u64>, seeds: Vec<u64>,
maps: HashMap<(DataType, DataType), Vec<DataMap>>, maps: MapMap,
} }
impl Almanac { impl Almanac {
fn parse(input: &str) -> Self { 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!(); todo!();
} }
fn soil_from_seed(&self, seed: u64) -> u64 {
use DataType::*;
self.x_from_y(Soil, Seed, seed)
}
} }
fn main() { fn main() {
println!("Hello, world!"); let almanac = Almanac::parse(EXAMPLE_FILE_STR);
let soil = almanac.soil_from_seed(79);
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
const EXAMPLE_FILE_STR: &'static str = include_str!("example-input.txt"); const EXAMPLE_FILE_STR: &'static str = include_str!("example-input.txt");
}
#[test]
fn test_parse() {}
}