Reuse input helper.

This commit is contained in:
2018-12-14 11:42:44 +01:00
parent 98da0a9857
commit f4a7b06d47
3 changed files with 6 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
use std::io::Read; use std::io::Read;
use common::Solution; use common::Solution;
use common::read_single_input;
#[derive(Default)] #[derive(Default)]
pub struct Day08 {} pub struct Day08 {}
@@ -58,8 +59,7 @@ impl Day08 {
impl Solution for Day08 { impl Solution for Day08 {
fn part1(&mut self, input: &mut Read) -> String { fn part1(&mut self, input: &mut Read) -> String {
let mut data = String::new(); let data: String = read_single_input(input);
input.read_to_string(&mut data).unwrap();
let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect(); let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect();
let (result, _) = total1(&data); let (result, _) = total1(&data);
@@ -68,8 +68,7 @@ impl Solution for Day08 {
} }
fn part2(&mut self, input: &mut Read) -> String { fn part2(&mut self, input: &mut Read) -> String {
let mut data = String::new(); let data: String = read_single_input(input);
input.read_to_string(&mut data).unwrap();
let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect(); let data: Vec<usize> = data.trim().split(" ").map(|x| x.parse().unwrap()).collect();
let (result, _) = total2(&data); let (result, _) = total2(&data);

View File

@@ -2,6 +2,7 @@ use std::i32;
use std::io::Read; use std::io::Read;
use common::Solution; use common::Solution;
use common::read_single_input;
fn power_at(serial: i32, (x, y): (i32, i32)) -> i32 { fn power_at(serial: i32, (x, y): (i32, i32)) -> i32 {
let rack_id = x + 10; let rack_id = x + 10;
@@ -23,12 +24,6 @@ impl Day11 {
} }
} }
fn read_serial(&self, input: &mut Read) -> i32 {
let mut data = String::new();
input.read_to_string(&mut data).unwrap();
data.trim().parse().unwrap()
}
fn compute_summed_area(&mut self, serial: i32) { fn compute_summed_area(&mut self, serial: i32) {
self.power_grid[0][0] = power_at(serial, (1, 1)); self.power_grid[0][0] = power_at(serial, (1, 1));
for x in 1..300 { for x in 1..300 {
@@ -93,7 +88,7 @@ impl Day11 {
impl Solution for Day11 { impl Solution for Day11 {
fn part1(&mut self, input: &mut Read) -> String { fn part1(&mut self, input: &mut Read) -> String {
let serial = self.read_serial(input); let serial = read_single_input(input);
self.compute_summed_area(serial); self.compute_summed_area(serial);
let (x, y, _) = self.best(3); let (x, y, _) = self.best(3);
@@ -101,7 +96,7 @@ impl Solution for Day11 {
} }
fn part2(&mut self, input: &mut Read) -> String { fn part2(&mut self, input: &mut Read) -> String {
let serial = self.read_serial(input); let serial = read_single_input(input);
self.compute_summed_area(serial); self.compute_summed_area(serial);
let mut best_result = 0; let mut best_result = 0;
let mut best_option = None; let mut best_option = None;

View File

@@ -2,7 +2,6 @@ use std::collections::HashMap;
use std::io::BufRead; use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;
use std::iter::FromIterator;
use common::Solution; use common::Solution;