begins work on parsing day 5 input

master
Joey Pollack 1 year ago
parent d10cd7da69
commit f67c240888

File diff suppressed because it is too large Load Diff

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

@ -17,6 +17,8 @@ pub struct Day5
run_mode: RunMode,
do_debug_prints: bool,
pub final_result: i32,
rules: Vec<Rule>,
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@ -26,7 +28,7 @@ impl Day5
{
pub fn new() -> Day5
{
Day5 { data_set: DataSet::Test, run_mode: RunMode::FirstCase, do_debug_prints: false, final_result: 0 }
Day5 { data_set: DataSet::Test, run_mode: RunMode::FirstCase, do_debug_prints: false, final_result: 0, rules: vec![] }
}
fn solve_first_case(self: &mut Self) -> String
@ -68,9 +70,31 @@ impl Solver for Day5
DataSet::Full => format!("{}/data/day5_input", dir),
};
let _data = utils::load_data(&data_filename);
let data = utils::load_data(&data_filename);
let mut split_pattern = "\n";
if data.contains("\r\n")
{
split_pattern = "\r\n";
}
let data_split = data.split(split_pattern);
for row in data_split
{
if row.contains("|")
{
self.rules.push(Rule::parse(row).expect(&format!("Failed to create rule from: {}", row) ));
}
}
// TODO: Day5::init
if self.do_debug_prints
{
println!("Rules:");
for rule in &self.rules
{
println!("\tRule: {}|{}", rule.first, rule.second);
}
}
}
@ -90,4 +114,40 @@ impl Solver for Day5
}
}
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// EXTRA STUFF
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, Debug)]
struct Rule
{
first: i32,
second: i32,
}
impl Rule
{
fn new(first: i32, second: i32) -> Rule
{
Rule { first, second }
}
fn parse(input: &str) -> Result<Rule, &str>
{
if !input.contains("|")
{
return Err("Failed to parse rule: string does not contain \'|\'");
}
let mut split = input.split("|");
let first = split.next().expect("Failed to parse rule: Could not properly split the input string");
let second = split.next().expect("Failed to parse rule: Could not properly split the input string");
let first = first.parse::<i32>().expect("Failed to parse rule: Could not convert value to i32");
let second = second.parse::<i32>().expect("Failed to parse rule: Could not convert value to i32");
Ok(Rule { first, second})
}
}

@ -71,14 +71,14 @@ fn main()
// DAY 5
let mut day_5 = Day5::new();
day_5.init(DataSet::Test, RunMode::FirstCase, false);
day_5.init(DataSet::Test, RunMode::FirstCase, true);
let day5_result = day_5.solve();
println!("Day 5 Part 1 Final Result: {}", day5_result);
let mut day_5 = Day5::new();
day_5.init(DataSet::Test, RunMode::SecondCase, false);
let day5_result = day_5.solve();
println!("Day 5 Part 2 Final Result: {}", day5_result);
// let mut day_5 = Day5::new();
// day_5.init(DataSet::Test, RunMode::SecondCase, false);
// let day5_result = day_5.solve();
// println!("Day 5 Part 2 Final Result: {}", day5_result);
println!("-------------------------");

Loading…
Cancel
Save