From 097cfac5169a530892c29ccb8a799b7194fa28b5 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 26 Dec 2018 23:22:45 +0100 Subject: [PATCH] day24 part1 input parsing --- input/day24.txt | 0 src/main.rs | 4 +-- src/tasks/day24.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++ src/tasks/mod.rs | 1 + 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 input/day24.txt create mode 100644 src/tasks/day24.rs diff --git a/input/day24.txt b/input/day24.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index fc32bc4..20d5f46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ fn main() { - // aoc_2018::tasks::day23::task1(); - aoc_2018::tasks::day23::task2(); + aoc_2018::tasks::day24::task1(); + // aoc_2018::tasks::day23::task2(); } diff --git a/src/tasks/day24.rs b/src/tasks/day24.rs new file mode 100644 index 0000000..637998d --- /dev/null +++ b/src/tasks/day24.rs @@ -0,0 +1,84 @@ +pub fn task1() { + let input = "Immune System: +17 units each with 5390 hit points (weak to radiation, bludgeoning) with an attack that does 4507 fire damage at initiative 2 +989 units each with 1274 hit points (immune to fire; weak to bludgeoning, slashing) with an attack that does 25 slashing damage at initiative 3 + +Infection: +801 units each with 4706 hit points (weak to radiation) with an attack that does 116 bludgeoning damage at initiative 1 +4485 units each with 2961 hit points (immune to radiation; weak to fire, cold) with an attack that does 12 slashing damage at initiative 4"; + let mut groups_immune: Vec = Vec::new(); + let mut groups_infection: Vec = Vec::new(); + + let mut to_immune = false; + for line in input.lines() { + match line { + "Immune System:" => { + to_immune = true; + } + "Infection:" => { + to_immune = false; + } + "" => (), + group => { + if let Some(group) = Group::from_str(group) { + if to_immune { + groups_immune.push(group); + } else { + groups_infection.push(group); + } + } + } + } + } + + println!("Immune System:\n{:?}", groups_immune); + println!("Infection:\n{:?}", groups_infection); +} + +#[derive(Debug)] +struct Group { + units: i64, + hp_each: i64, + weaknesses: Vec, + immunities: Vec, + attack_damage: i64, + attack_type: String, + initiative: u64, +} + +impl Group { + fn from_str(input: &str) -> Option { + // 801 units each with 4706 hit points (weak to radiation) with an attack that does 116 bludgeoning damage at initiative 1 + use regex::Regex; + let regex = Regex::new(r"(\d+) units each with (\d+) hit points \((.+)\) with an attack that does (\d+) (\w+) damage at initiative (\d+)").unwrap(); + if let Some(m) = regex.captures(input) { + let units: i64 = m[1].parse().unwrap(); + let hp_each: i64 = m[2].parse().unwrap(); + let attack_damage: i64 = m[4].parse().unwrap(); + let attack_type = m[5].to_string(); + let initiative: u64 = m[6].parse().unwrap(); + let mut weaknesses: Vec = Vec::new(); + let mut immunities: Vec = Vec::new(); + for part in m[3].split("; ") { + if part.starts_with("weak to ") { + weaknesses = part[8..].split(", ").map(|it| it.to_string()).collect(); + } else if part.starts_with("immune to ") { + immunities = part[10..].split(", ").map(|it| it.to_string()).collect(); + } + } + + let group = Group { + units, + hp_each, + weaknesses, + immunities, + attack_damage, + attack_type, + initiative, + }; + Some(group) + } else { + None + } + } +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 01676ab..f4df0bd 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -16,3 +16,4 @@ pub mod day15; pub mod day20; pub mod day22; pub mod day23; +pub mod day24;