day23 part1

This commit is contained in:
Johannes
2018-12-25 17:15:51 +01:00
parent 21dfc7f630
commit 6e438127ec
4 changed files with 1048 additions and 2 deletions

1000
input/day23.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
fn main() {
aoc_2018::tasks::day22::both();
aoc_2018::tasks::day23::task1();
// aoc_2018::tasks::day15::task2();
}

45
src/tasks/day23.rs Normal file
View File

@@ -0,0 +1,45 @@
use crate::utils;
extern crate regex;
use regex::Regex;
pub fn task1() {
let input = utils::read_file("input/day23.txt");
let regex =
Regex::new(r"^pos=<(?P<x>-?\d+),(?P<y>-?\d+),(?P<z>-?\d+)>, r=(?P<range>\d+)$").unwrap();
let bots: Vec<Bot> = input
.lines()
.map(|line| {
let m = regex.captures(line).unwrap();
let x = m["x"].parse::<isize>().unwrap();
let y = m["y"].parse::<isize>().unwrap();
let z = m["z"].parse::<isize>().unwrap();
let range = m["range"].parse::<usize>().unwrap();
Bot { x, y, z, range }
})
.collect();
let big_bot = bots.iter().max_by_key(|it| it.range).unwrap();
println!("The bot with largest range is {:?}", big_bot);
let bots_in_range = bots
.iter()
.filter(|other| big_bot.distance(other) <= big_bot.range)
.count();
println!("There are {} bots in range of the big bot.", bots_in_range);
}
#[derive(Debug)]
struct Bot {
x: isize,
y: isize,
z: isize,
range: usize,
}
impl Bot {
fn distance(&self, other: &Self) -> usize {
((other.x - self.x).abs() + (other.y - self.y).abs() + (other.z - self.z).abs()) as usize
}
}

View File

@@ -15,3 +15,4 @@ pub mod day14;
pub mod day15;
pub mod day20;
pub mod day22;
pub mod day23;