day 5 part 1

This commit is contained in:
Niklas Kapelle 2023-12-11 18:52:34 +01:00
parent 398227a555
commit f0c1f6aa9f
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -1,17 +1,107 @@
use regex::Regex;
use crate::common::load_input; use crate::common::load_input;
struct TranslationRange{ #[derive(Debug)]
source_start: u32, struct TranslationRange {
destination_start: u32, source_start: u64,
range: u32, destination_start: u64,
range: u64,
} }
#[derive(Debug)]
struct TranslationMap { struct TranslationMap {
ranges: Vec<TranslationRange> ranges: Vec<TranslationRange>,
}
impl TranslationRange {
fn in_range(&self, num: u64) -> bool {
return (num >= self.source_start) && (num < (self.source_start + self.range));
}
fn translate(&self, num: u64) -> u64 {
return (num - self.source_start) + self.destination_start;
}
}
impl TranslationMap {
fn translate(&self, num: u64) -> u64 {
let t: Vec<&TranslationRange> = self.ranges.iter().filter(|e| e.in_range(num)).collect();
if t.len() > 1 {
panic!("Fucked up");
}
if t.len() == 1 {
return t.get(0).unwrap().translate(num);
}
return num;
}
}
fn parse_input(lines: &[String]) -> Vec<TranslationMap> {
let mut translation_map: Vec<TranslationMap> = vec![];
let regex = Regex::new(r"(?m)(\d+) (\d+) (\d+)").unwrap();
for line in lines.iter() {
if line.is_empty() {
continue;
}
if line.contains("map") {
translation_map.push(TranslationMap { ranges: vec![] });
continue;
}
let result = regex.captures(line).unwrap();
let dest: u64 = result.get(1).unwrap().as_str().parse().unwrap();
let source: u64 = result.get(2).unwrap().as_str().parse().unwrap();
let range: u64 = result.get(3).unwrap().as_str().parse().unwrap();
translation_map
.last_mut()
.unwrap()
.ranges
.push(TranslationRange {
source_start: source,
destination_start: dest,
range: range,
})
}
return translation_map;
}
fn get_seeds(line: &str) -> Vec<u64> {
let regex = Regex::new(r"(?m)(\d+)").unwrap();
let result = regex.captures_iter(line);
return result
.map(|e| e.get(0).unwrap().as_str().parse::<u64>().unwrap())
.collect();
}
fn seed_to_location(maps: &Vec<TranslationMap>, num: u64) -> u64 {
let mut result = num;
for map in maps.iter() {
result = map.translate(result);
}
return result;
} }
pub fn run() -> Result<i32, Box<dyn std::error::Error>> { pub fn run() -> Result<i32, Box<dyn std::error::Error>> {
let lines = load_input("./input/5.txt")?; let lines = load_input("./input/5.txt")?;
let seeds = get_seeds(lines.get(0).unwrap().as_str());
let maps = parse_input(&lines[1..]);
Ok(999) let min = seeds
.iter()
.map(|e| seed_to_location(&maps, *e))
.min()
.unwrap();
Ok(min.try_into().unwrap())
} }