mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implement 2016 day 22 part 1. Again.
This commit is contained in:
@@ -6,5 +6,5 @@ authors = ["Bert Peters <bert@bertptrs.nl>"]
|
||||
[dependencies]
|
||||
clap = "2.32"
|
||||
regex = "1.0"
|
||||
itertools = "0.7.8"
|
||||
itertools = "0.8"
|
||||
permutohedron = "0.2.4"
|
||||
|
||||
1022
2016/inputs/22.txt
Normal file
1022
2016/inputs/22.txt
Normal file
File diff suppressed because it is too large
Load Diff
69
2016/src/day22.rs
Normal file
69
2016/src/day22.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use std::io::Read;
|
||||
|
||||
use common::Solution;
|
||||
use std::io::BufReader;
|
||||
use regex::Regex;
|
||||
use std::io::BufRead;
|
||||
|
||||
type Coordinate = (usize, usize);
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
|
||||
struct Node {
|
||||
pos: Coordinate,
|
||||
capacity: usize,
|
||||
used: usize,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
fn fits(self, other: Node) -> bool {
|
||||
self.capacity - self.used >= other.used
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Day22 {
|
||||
nodes: Vec<Node>,
|
||||
}
|
||||
|
||||
impl Day22 {
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn read_input(&mut self, input: &mut Read) {
|
||||
let reader = BufReader::new(input);
|
||||
let matcher = Regex::new(r"/dev/grid/node-x(\d+)-y(\d+)(?:\s+)(\d+)T(?:\s+)+(\d+)T").unwrap();
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line.unwrap();
|
||||
let node = match matcher.captures(&line) {
|
||||
Some(captures) => Node {
|
||||
pos: (captures[1].parse().unwrap(), captures[2].parse().unwrap()),
|
||||
capacity: captures[3].parse().unwrap(),
|
||||
used: captures[4].parse().unwrap(),
|
||||
},
|
||||
|
||||
None => continue
|
||||
};
|
||||
|
||||
self.nodes.push(node)
|
||||
}
|
||||
|
||||
assert_ne!(0, self.nodes.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution for Day22 {
|
||||
fn part1(&mut self, input: &mut Read) -> String {
|
||||
self.read_input(input);
|
||||
|
||||
let fitting = iproduct!(&self.nodes, &self.nodes)
|
||||
.filter(|&(_, b)| b.used > 0)
|
||||
.filter(|&(a, b)| a != b)
|
||||
.filter(|&(a, b)| a.fits(*b))
|
||||
.count();
|
||||
|
||||
|
||||
fitting.to_string()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
extern crate clap;
|
||||
extern crate regex;
|
||||
extern crate itertools;
|
||||
#[macro_use] extern crate itertools;
|
||||
extern crate permutohedron;
|
||||
use clap::{Arg, App};
|
||||
use std::fs;
|
||||
@@ -14,6 +14,7 @@ pub mod day12;
|
||||
pub mod day15;
|
||||
pub mod day16;
|
||||
pub mod day21;
|
||||
pub mod day22;
|
||||
pub mod day23;
|
||||
pub mod day24;
|
||||
pub mod day25;
|
||||
@@ -27,6 +28,7 @@ fn get_impl(day: &str) -> Box<common::Solution> {
|
||||
Ok(15) => { Box::new(day15::Day15::new()) }
|
||||
Ok(16) => { Box::new(day16::Day16::new()) }
|
||||
Ok(21) => { Box::new(day21::Day21::new()) }
|
||||
Ok(22) => { Box::new(day22::Day22::new()) }
|
||||
Ok(23) => { Box::new(day23::Day23::new()) }
|
||||
Ok(24) => { Box::new(day24::Day24::new()) }
|
||||
Ok(25) => { Box::new(day25::Day25::new()) }
|
||||
|
||||
Reference in New Issue
Block a user