Implement day23 part 1.

This commit is contained in:
2018-12-23 07:07:02 +01:00
parent a052d3a4fe
commit 85538d2fc6
5 changed files with 1084 additions and 4 deletions

1000
2018/inputs/23.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -31,6 +31,18 @@ impl<T> Point for (T, T)
} }
} }
impl<T> Point for (T, T, T)
where T: Add<Output=T> + Sub<Output=T> + Copy + Ord
{
type CoordType = T;
fn manhattan(self, other: Self) -> T {
let (xa, ya, za) = self;
let (xb, yb, zb) = other;
xa.max(xb) + ya.max(yb) + za.max(zb) - xa.min(xb) - ya.min(yb) - za.min(zb)
}
}
/// Apply Erathostenes's sieve to the supplied array /// Apply Erathostenes's sieve to the supplied array
/// ///

View File

@@ -1,19 +1,61 @@
use std::io::Read; use std::io::Read;
use common::Solution; use common::Solution;
use regex::Regex;
use std::io::BufReader;
use std::io::BufRead;
use common::Point;
use std::collections::VecDeque;
use std::collections::HashSet;
use std::collections::HashMap;
type Coordinate = (i64, i64, i64);
#[derive(Default)] #[derive(Default)]
pub struct Day23 {} pub struct Day23 {
bots: Vec<(i64, Coordinate)>
}
impl Day23 { impl Day23 {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
fn read_input(&mut self, input: &mut Read) {
let matcher = Regex::new(r"-?\d+").unwrap();
let reader = BufReader::new(input);
for line in reader.lines() {
let line = line.unwrap();
let mut ints = [0i64;4];
for (c, i) in matcher.find_iter(&line).zip(ints.iter_mut()) {
*i = c.as_str().parse().unwrap();
}
let pos = (ints[0], ints[1], ints[2]);
self.bots.push((ints[3], pos));
}
}
fn in_range(&self, pos: Coordinate) -> usize {
self.bots.iter()
.filter(|&&(range, other)| other.manhattan(pos) <= range)
.count()
}
} }
impl Solution for Day23 { impl Solution for Day23 {
fn part1(&mut self, _input: &mut Read) -> String { fn part1(&mut self, input: &mut Read) -> String {
unimplemented!() self.read_input(input);
self.bots.sort_unstable();
let (best_range, best_pos) = *self.bots.last().unwrap();
let result = self.bots.iter().filter(|(_, pos)| pos.manhattan(best_pos) <= best_range)
.count();
result.to_string()
} }
fn part2(&mut self, _input: &mut Read) -> String { fn part2(&mut self, _input: &mut Read) -> String {
@@ -22,4 +64,15 @@ impl Solution for Day23 {
} }
#[cfg(test)] #[cfg(test)]
mod tests {} mod tests {
use day23::Day23;
use common::Solution;
const SAMPLE1_INPUT: &[u8] = include_bytes!("samples/23.1.txt");
#[test]
fn sample_part1() {
let mut instance = Day23::new();
assert_eq!("7", instance.part1(&mut SAMPLE1_INPUT));
}
}

View File

@@ -0,0 +1,9 @@
pos=<0,0,0>, r=4
pos=<1,0,0>, r=1
pos=<4,0,0>, r=3
pos=<0,2,0>, r=1
pos=<0,5,0>, r=3
pos=<0,0,3>, r=1
pos=<1,1,1>, r=1
pos=<1,1,2>, r=1
pos=<1,3,1>, r=1

View File

@@ -0,0 +1,6 @@
pos=<10,12,12>, r=2
pos=<12,14,12>, r=2
pos=<16,12,12>, r=4
pos=<14,14,14>, r=6
pos=<50,50,50>, r=200
pos=<10,10,10>, r=5