Day 5 input parsing works

day_5_part_2_broken
Joey Pollack 2 years ago
parent c603f08c1b
commit 80280f6765

@ -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"
}
]
}

7
day_5/Cargo.lock generated

@ -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"

@ -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

@ -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<i32, i32>
}
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<SubMap>,
}
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<i32>, HashMap<String, Map>) // String is the map name
{
let mut seeds: Vec<i32> = Vec::new();
let mut maps: HashMap<String, Map> = 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::<i32>().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(&current_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::<i32>().expect(&format!("Failed to parse dest value: {}", vals[0]));
sub_map.source_start = vals[1].parse::<i32>().expect(&format!("Failed to parse source value: {}", vals[1]));
sub_map.length = vals[2].parse::<i32>().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)
}

Loading…
Cancel
Save