From 80280f67654998a9ae8bd91ea1c9ea1df4c49291 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 14 Dec 2023 14:36:16 -0500 Subject: [PATCH] Day 5 input parsing works --- .vscode/launch.json | 10 ++++ day_5/Cargo.lock | 7 +++ day_5/data/test_input | 33 +++++++++++ day_5/src/main.rs | 124 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 day_5/Cargo.lock create mode 100644 day_5/data/test_input diff --git a/.vscode/launch.json b/.vscode/launch.json index 7fbc907..5afeab4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,7 @@ "configurations": [ + { "type": "lldb", "request": "launch", @@ -31,6 +32,15 @@ "program": "${workspaceFolder}/day_4/target/debug/day_4", "args": [], "cwd": "${workspaceFolder}/day_4/target/debug" + }, + + { + "type": "lldb", + "request": "launch", + "name": "Debug Day 5", + "program": "${workspaceFolder}/day_5/target/debug/day_5", + "args": [], + "cwd": "${workspaceFolder}/day_5/target/debug" } ] } \ No newline at end of file diff --git a/day_5/Cargo.lock b/day_5/Cargo.lock new file mode 100644 index 0000000..e899086 --- /dev/null +++ b/day_5/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day_5" +version = "0.1.0" diff --git a/day_5/data/test_input b/day_5/data/test_input new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/day_5/data/test_input @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file diff --git a/day_5/src/main.rs b/day_5/src/main.rs index 3416e1e..b9e7336 100644 --- a/day_5/src/main.rs +++ b/day_5/src/main.rs @@ -1,9 +1,131 @@ -use std::{io::prelude::*, fs::File, path::Path, io }; +use std::{io::prelude::*, fs::File, path::Path, io, collections::HashMap }; + + +#[derive(Clone, Debug)] +struct SubMap +{ + destination_start: i32, + source_start: i32, + length: i32, + + map: HashMap +} + +impl SubMap +{ + fn new() -> SubMap + { + SubMap { destination_start: 0, source_start: 0, length: 0, map: HashMap::new() } + } + + fn map_values(&mut self) + { + for i in 0..self.length + { + self.map.insert(self.source_start + i, self.destination_start + i); + } + } +} + +#[derive(Clone, Debug)] +struct Map +{ + name: String, + sub_maps: Vec, +} + +impl Map +{ + fn new(name: &str) -> Map + { + Map { name: name.to_string(), sub_maps: Vec::new() } + } + + fn map_input(&self, input: i32) -> i32 + { + for sub_map in &self.sub_maps + { + if sub_map.map.contains_key(&input) + { + return sub_map.map[&input]; + } + } + + return input; + } +} fn main() { + let input = load_data("data/test_input"); + let (seeds, maps) = parse_input(&input); + + println!("SEEDS: {:#?}, MAPS: {:#?}", seeds, maps); +} + +fn parse_input(input: &str) -> (Vec, HashMap) // String is the map name +{ + let mut seeds: Vec = Vec::new(); + + let mut maps: HashMap = HashMap::new(); + let mut current_map = ""; + for (idx, line) in input.to_ascii_lowercase().split("\n").into_iter().enumerate() + { + // stupid windows dumb line ending non-sense + if line.starts_with("\r") + { + continue; + } + + // SEEDS + if line.contains("seeds:") + { + for val in line.trim_start_matches("seeds:").trim().split(" ") + { + let num = val.parse::().expect(&format!("Failed to parse seed value: {}", val)); + seeds.push(num); + } + + continue; + } + + + // NEW MAP + if line.contains("map:") + { + let name = line.trim_end_matches("map:"); + current_map = name.clone(); + let map = Map::new(¤t_map); + maps.insert(current_map.to_string(), map.clone()); + continue; + } + + // NEW SUB MAP + let mut sub_map = SubMap::new(); + let vals: Vec<&str> = line.trim().split(" ").collect(); + + if vals.len() < 3 + { + println!("========INVALID SUBMAP FOR {}: {:#?}========", current_map, vals); + continue; + } + + sub_map.destination_start = vals[0].parse::().expect(&format!("Failed to parse dest value: {}", vals[0])); + sub_map.source_start = vals[1].parse::().expect(&format!("Failed to parse source value: {}", vals[1])); + sub_map.length = vals[2].parse::().expect(&format!("Failed to parse length value: {}", vals[2])); + sub_map.map_values(); + if let Some(map) = maps.get_mut(current_map) + { + map.sub_maps.push(sub_map.clone()); + } + else + { + println!("========CURRENT MAP NOT FOUND IN MAP: {}========", current_map); + } + } + (seeds, maps) }