Change signature for solution.

This commit is contained in:
2019-08-22 12:28:52 +02:00
parent e949701564
commit 29b5dcdf62
26 changed files with 73 additions and 73 deletions

View File

@@ -112,7 +112,7 @@ pub fn trim_back(input: &mut Vec<u8>) {
/// Read the entire input as one value.
///
/// This function loads the input into a string and then attempts to parse it.
pub fn read_single_input<T>(input: &mut Read) -> T
pub fn read_single_input<T>(input: &mut dyn Read) -> T
where
T: FromStr,
<T as FromStr>::Err: Debug,
@@ -158,10 +158,10 @@ where
/// be easily run from the main program.
pub trait Solution {
/// Solve the first part of the day
fn part1(&mut self, input: &mut io::Read) -> String;
fn part1(&mut self, input: &mut dyn io::Read) -> String;
/// Solve the second part of the day
fn part2(&mut self, input: &mut io::Read) -> String;
fn part2(&mut self, input: &mut dyn io::Read) -> String;
}
#[cfg(test)]

View File

@@ -14,7 +14,7 @@ impl Day01 {
}
impl common::Solution for Day01 {
fn part1(&mut self, input: &mut io::Read) -> String {
fn part1(&mut self, input: &mut dyn io::Read) -> String {
let reader = io::BufReader::new(input);
let sum: i32 = reader
@@ -25,7 +25,7 @@ impl common::Solution for Day01 {
sum.to_string()
}
fn part2(&mut self, input: &mut io::Read) -> String {
fn part2(&mut self, input: &mut dyn io::Read) -> String {
let reader = io::BufReader::new(input);
let mut freqs = HashSet::new();
freqs.insert(0);

View File

@@ -33,7 +33,7 @@ impl Day02 {
}
impl common::Solution for Day02 {
fn part1(&mut self, input: &mut io::Read) -> String {
fn part1(&mut self, input: &mut dyn io::Read) -> String {
let reader = io::BufReader::new(input);
let mut twos = 0;
let mut threes = 0;
@@ -53,7 +53,7 @@ impl common::Solution for Day02 {
(twos * threes).to_string()
}
fn part2(&mut self, input: &mut io::Read) -> String {
fn part2(&mut self, input: &mut dyn io::Read) -> String {
let mut ids: Vec<String> = io::BufReader::new(input)
.lines()
.map(|x| x.unwrap())

View File

@@ -40,7 +40,7 @@ impl Day03 {
Default::default()
}
fn read_claims(&mut self, input: &mut io::Read) {
fn read_claims(&mut self, input: &mut dyn io::Read) {
let reader = io::BufReader::new(input);
self.claims.clear();
@@ -66,7 +66,7 @@ impl Day03 {
}
impl common::Solution for Day03 {
fn part1(&mut self, input: &mut io::Read) -> String {
fn part1(&mut self, input: &mut dyn io::Read) -> String {
self.read_claims(input);
let claim_map = self.get_claims();
@@ -75,7 +75,7 @@ impl common::Solution for Day03 {
multi_claim.to_string()
}
fn part2(&mut self, input: &mut io::Read) -> String {
fn part2(&mut self, input: &mut dyn io::Read) -> String {
self.read_claims(input);
let claims = self.get_claims();

View File

@@ -33,7 +33,7 @@ impl Day04 {
Default::default()
}
fn read_events(&mut self, input: &mut io::Read) {
fn read_events(&mut self, input: &mut dyn io::Read) {
self.events.clear();
let reader = io::BufReader::new(input);
@@ -105,7 +105,7 @@ impl Day04 {
}
impl common::Solution for Day04 {
fn part1(&mut self, input: &mut io::Read) -> String {
fn part1(&mut self, input: &mut dyn io::Read) -> String {
self.read_events(input);
let sleepers = self.get_sleeps();
let scores: HashMap<usize, u32> =
@@ -114,7 +114,7 @@ impl common::Solution for Day04 {
Day04::format_results(&sleepers, &scores)
}
fn part2(&mut self, input: &mut io::Read) -> String {
fn part2(&mut self, input: &mut dyn io::Read) -> String {
self.read_events(input);
let sleepers = self.get_sleeps();
let scores: HashMap<usize, u32> = sleepers

View File

@@ -29,7 +29,7 @@ impl Day05 {
}
impl common::Solution for Day05 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let mut data = Vec::new();
input.read_to_end(&mut data).expect("Can't read input!");
common::trim_back(&mut data);
@@ -37,7 +37,7 @@ impl common::Solution for Day05 {
Day05::reduce(data).to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let mut data = Vec::new();
input.read_to_end(&mut data).expect("Can't read input!");
common::trim_back(&mut data);

View File

@@ -28,7 +28,7 @@ impl Day06 {
Default::default()
}
pub fn read_points(&mut self, input: &mut Read) {
pub fn read_points(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
self.points.clear();
@@ -78,7 +78,7 @@ impl Day06 {
grid
}
pub fn part2_with_limit(&mut self, input: &mut Read, limit: usize) -> usize {
pub fn part2_with_limit(&mut self, input: &mut dyn Read, limit: usize) -> usize {
self.read_points(input);
self.range()
@@ -96,7 +96,7 @@ fn claim_filter(claim: &Claim) -> Option<usize> {
}
impl Solution for Day06 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_points(input);
let grid = self.compute_claim_grid();
let mut infinite: HashSet<usize> = HashSet::new();
@@ -121,7 +121,7 @@ impl Solution for Day06 {
counts.values().max().unwrap().to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.part2_with_limit(input, 10_000).to_string()
}
}

View File

@@ -39,7 +39,7 @@ impl Day07 {
Default::default()
}
fn read_edges(&mut self, input: &mut Read) {
fn read_edges(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
let regex = Regex::new(r"Step (\w) must be finished before step (\w) can begin").unwrap();
@@ -56,7 +56,7 @@ impl Day07 {
fn part2_parametrized(
&mut self,
input: &mut Read,
input: &mut dyn Read,
base_time: usize,
max_workers: usize,
) -> usize {
@@ -108,7 +108,7 @@ impl Day07 {
}
impl Solution for Day07 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_edges(input);
let mut result = String::new();
@@ -138,7 +138,7 @@ impl Solution for Day07 {
result
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.part2_parametrized(input, 60, 5).to_string()
}
}

View File

@@ -57,7 +57,7 @@ impl Day08 {
}
impl Solution for Day08 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let data: String = read_single_input(input);
let data: Vec<usize> = data.trim().split(' ').map(|x| x.parse().unwrap()).collect();
@@ -66,7 +66,7 @@ impl Solution for Day08 {
result.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let data: String = read_single_input(input);
let data: Vec<usize> = data.trim().split(' ').map(|x| x.parse().unwrap()).collect();

View File

@@ -40,7 +40,7 @@ impl Day09 {
Default::default()
}
fn read_input(input: &mut Read) -> (usize, usize) {
fn read_input(input: &mut dyn Read) -> (usize, usize) {
let mut data = String::new();
input.read_to_string(&mut data).unwrap();
let mut parts = data.split(' ');
@@ -52,13 +52,13 @@ impl Day09 {
}
impl Solution for Day09 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let (elves, marbles) = Day09::read_input(input);
winning_marbles(elves, marbles).to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let (elves, marbles) = Day09::read_input(input);
winning_marbles(elves, marbles * 100).to_string()

View File

@@ -21,7 +21,7 @@ impl Day10 {
Default::default()
}
fn read_inputs(&mut self, input: &mut Read) {
fn read_inputs(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
let matcher =
Regex::new(r"position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>")
@@ -96,7 +96,7 @@ impl Day10 {
dist.checked_div(speed).unwrap_or(1).max(1)
}
fn simulate(&mut self, input: &mut Read) -> i32 {
fn simulate(&mut self, input: &mut dyn Read) -> i32 {
self.read_inputs(input);
let mut prev = self.height();
let mut steps = self.underestimate_time();
@@ -115,12 +115,12 @@ impl Day10 {
}
impl Solution for Day10 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.simulate(input);
self.print_state()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.simulate(input).to_string()
}
}

View File

@@ -93,7 +93,7 @@ impl Default for Day11 {
}
impl Solution for Day11 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let serial = read_single_input(input);
self.compute_summed_area(serial);
let (x, y, _) = self.best(3);
@@ -101,7 +101,7 @@ impl Solution for Day11 {
format!("{},{}", x, y)
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let serial = read_single_input(input);
self.compute_summed_area(serial);
let mut best_result = 0;

View File

@@ -40,7 +40,7 @@ impl Day12 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) -> State {
fn read_input(&mut self, input: &mut dyn Read) -> State {
let state;
let mut reader = BufReader::new(input);
let mut line = String::new();
@@ -107,14 +107,14 @@ impl Day12 {
}
impl Solution for Day12 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let mut state = self.read_input(input);
state = self.simulate_n(state, 20);
self.sum(&state).to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
// Note: this is way too slow
let mut state = self.read_input(input);
let mut seen = HashMap::new();

View File

@@ -98,7 +98,7 @@ impl Day13 {
self.carts.iter().filter(|x| x.alive).count()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
for (y, line) in reader.lines().enumerate() {
let line = line.unwrap();
@@ -183,7 +183,7 @@ impl Day13 {
}
impl Solution for Day13 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let mut collision = None;
while collision == None {
@@ -193,7 +193,7 @@ impl Solution for Day13 {
format!("{},{}", x, y)
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
while self.alive() > 1 {
self.simulate();

View File

@@ -76,12 +76,12 @@ impl Day14 {
}
impl Solution for Day14 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let input = read_single_input(input);
format!("{:010}", skill_after(input))
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let mut buf = String::new();
input.read_to_string(&mut buf).unwrap();

View File

@@ -48,7 +48,7 @@ impl Day15 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
for (y, line) in reader.lines().enumerate() {
@@ -212,7 +212,7 @@ impl Day15 {
}
impl Solution for Day15 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let mut rounds = 0;
while self.simulate() {
@@ -221,7 +221,7 @@ impl Solution for Day15 {
self.return_score(rounds)
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let backup = self.units.clone();
let starting_elves = self.alive[0];

View File

@@ -40,7 +40,7 @@ impl Day16 {
fn determine_options(
&mut self,
mut reader: &mut BufReader<&mut Read>,
mut reader: &mut BufReader<&mut dyn Read>,
) -> [HashSet<OpCode>; 16] {
let mut mappings: [HashSet<OpCode>; 16] = [
HashSet::new(),
@@ -119,7 +119,7 @@ impl Default for Day16 {
}
impl Solution for Day16 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let mut reader = BufReader::new(input);
let mut before = [0; 6];
@@ -143,7 +143,7 @@ impl Solution for Day16 {
counter.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let mut reader = BufReader::new(input);
let mappings = self.determine_options(&mut reader);

View File

@@ -25,7 +25,7 @@ impl Day17 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let matcher = Regex::new(r"(.)=(\d+), (.)=(\d+)\.\.(\d+)").unwrap();
let reader = BufReader::new(input);
@@ -119,7 +119,7 @@ impl Day17 {
}
impl Solution for Day17 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
self.descend((500, 0));
@@ -128,7 +128,7 @@ impl Solution for Day17 {
result.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
self.descend((500, 0));

View File

@@ -78,7 +78,7 @@ impl Day18 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
self.grid.clear();
@@ -141,7 +141,7 @@ impl Day18 {
}
impl Solution for Day18 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
for _ in 0..10 {
@@ -151,7 +151,7 @@ impl Solution for Day18 {
self.score()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let limit = 1_000_000_000;

View File

@@ -17,7 +17,7 @@ impl Day19 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let reader = BufReader::new(input);
for line in reader.lines() {
@@ -39,7 +39,7 @@ impl Day19 {
}
impl Solution for Day19 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let mut cpu = CPU::new();
@@ -53,7 +53,7 @@ impl Solution for Day19 {
cpu.registers[0].to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let mut cpu = CPU::new();

View File

@@ -137,7 +137,7 @@ impl Day20 {
}
impl Solution for Day20 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let mut data = Vec::new();
input.read_to_end(&mut data).unwrap();
let pos = (0, 0);
@@ -146,7 +146,7 @@ impl Solution for Day20 {
self.distances().values().max().unwrap().to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let mut data = Vec::new();
input.read_to_end(&mut data).unwrap();
let pos = (0, 0);

View File

@@ -48,11 +48,11 @@ impl Day21 {
}
impl Solution for Day21 {
fn part1(&mut self, _input: &mut Read) -> String {
fn part1(&mut self, _input: &mut dyn Read) -> String {
ValidInputs::new(0).next().unwrap().to_string()
}
fn part2(&mut self, _input: &mut Read) -> String {
fn part2(&mut self, _input: &mut dyn Read) -> String {
let inputs = ValidInputs::new(0);
let mut seen = HashSet::new();
let mut last: Option<i64> = None;

View File

@@ -63,7 +63,7 @@ fn compute_table((x, y): Coordinate, depth: usize) -> Vec<Vec<usize>> {
table
}
fn read_input(input: &mut Read) -> (usize, Coordinate) {
fn read_input(input: &mut dyn Read) -> (usize, Coordinate) {
let mut buf = String::new();
let mut reader = BufReader::new(input);
reader.read_line(&mut buf).unwrap();
@@ -88,7 +88,7 @@ fn read_input(input: &mut Read) -> (usize, Coordinate) {
}
impl Solution for Day22 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
let (depth, target) = read_input(input);
let mut table = compute_table(target, depth);
table[target.1][target.0] = 0;
@@ -97,7 +97,7 @@ impl Solution for Day22 {
result.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
let (depth, target) = read_input(input);
let mut table = compute_table((target.0 + 200, target.1 + 200), depth);
table[target.1][target.0] = 0;

View File

@@ -72,7 +72,7 @@ impl Day23 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let matcher = Regex::new(r"-?\d+").unwrap();
let reader = BufReader::new(input);
@@ -92,7 +92,7 @@ impl Day23 {
}
impl Solution for Day23 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
self.bots.sort_unstable();
let (best_range, best_pos) = *self.bots.last().unwrap();
@@ -106,7 +106,7 @@ impl Solution for Day23 {
result.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let mut neighbours = vec![HashSet::new(); self.bots.len()];

View File

@@ -52,7 +52,7 @@ impl Day24 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let matcher = Regex::new(r"(\d+) units each with (\d+) hit points (\(([^)]+)\) )?with an attack that does (\d+) (\w+) damage at initiative (\d+)").unwrap();
let weakness_matcher = Regex::new(r"(weak|immune) to ([^;)]+)").unwrap();
let reader = BufReader::new(input);
@@ -194,7 +194,7 @@ impl Day24 {
}
impl Solution for Day24 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
self.full_simulation();
@@ -202,7 +202,7 @@ impl Solution for Day24 {
result.to_string()
}
fn part2(&mut self, input: &mut Read) -> String {
fn part2(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
let original = self.units.clone();

View File

@@ -17,7 +17,7 @@ impl Day25 {
Default::default()
}
fn read_input(&mut self, input: &mut Read) {
fn read_input(&mut self, input: &mut dyn Read) {
let matcher = Regex::new(r"-?\d+").unwrap();
let reader = BufReader::new(input);
@@ -79,12 +79,12 @@ impl Day25 {
}
impl Solution for Day25 {
fn part1(&mut self, input: &mut Read) -> String {
fn part1(&mut self, input: &mut dyn Read) -> String {
self.read_input(input);
self.connected_components().to_string()
}
fn part2(&mut self, _input: &mut Read) -> String {
fn part2(&mut self, _input: &mut dyn Read) -> String {
// As always, no part 2 for day 25.
String::new()
}