Extract input reading into commons

This commit is contained in:
kageru 2020-12-10 14:44:42 +01:00
parent 728122032d
commit 6bf1811339
13 changed files with 40 additions and 85 deletions

View File

@ -8,14 +8,15 @@ sed -i -e '$a\' inputs/day$today
echo '#![feature(test)]
extern crate test;
use std::env;
use aoc::common::*;
type Parsed = Vec<usize>;
fn read_input() -> String {
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or_else(||String::from("inputs/day'$today'"))).unwrap()
read_file('$today')
}
fn parse_input(raw: &str) -> Parsed {
unimplemented!()
}

View File

@ -1,16 +1,10 @@
#![feature(test, bool_to_option)]
extern crate test;
use aoc2020::common::*;
use itertools::Itertools;
use std::env;
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day01")),
)
.unwrap()
read_file(1)
}
fn parse_input(input: &str) -> Vec<usize> {

View File

@ -1,7 +1,8 @@
#![feature(test)]
extern crate test;
use aoc2020::common::*;
use itertools::Itertools;
use std::{env, ops::RangeInclusive};
use std::ops::RangeInclusive;
use text_io::scan;
#[derive(Debug)]
@ -42,16 +43,7 @@ impl PasswordVerification {
}
fn read_input() -> Vec<PasswordVerification> {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day02")),
)
.unwrap()
.lines()
.map_into()
.collect()
read_file(2).lines().map_into().collect()
}
fn main() {

View File

@ -1,8 +1,9 @@
#![feature(iter_map_while)]
#![feature(test)]
extern crate test;
use aoc2020::common::*;
use itertools::Itertools;
use std::{env, iter};
use std::iter;
#[derive(Debug, PartialEq, Copy, Clone)]
enum Tile {
@ -29,13 +30,7 @@ impl From<u8> for Tile {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day03")),
)
.unwrap()
read_file(3)
}
fn parse_input(raw: &str) -> Forest {

View File

@ -1,9 +1,9 @@
#![feature(test)]
extern crate test;
use aoc2020::common::*;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::env;
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Passport {
@ -18,13 +18,7 @@ struct Passport {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day04")),
)
.unwrap()
read_file(4)
}
/// When I first saw the input and puzzle, I thought

View File

@ -1,7 +1,8 @@
#![feature(test, map_first_last, binary_heap_into_iter_sorted)]
extern crate test;
use aoc2020::common::*;
use itertools::Itertools;
use std::{collections::BinaryHeap, env};
use std::collections::BinaryHeap;
const NUM_ROWS: usize = 128;
const NUM_COLS: usize = 8;
@ -50,13 +51,7 @@ fn main() {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day05")),
)
.unwrap()
read_file(5)
}
#[cfg(test)]

View File

@ -1,6 +1,7 @@
#![feature(test)]
extern crate test;
use std::{collections::HashSet, env};
use aoc2020::common::*;
use std::collections::HashSet;
fn main() {
let input = parse_input(&read_input());
@ -9,13 +10,7 @@ fn main() {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day06")),
)
.unwrap()
read_file(6)
}
fn parse_input(raw: &str) -> Vec<Vec<HashSet<char>>> {

View File

@ -1,6 +1,7 @@
#![feature(test)]
use std::{collections::HashSet, env};
use std::collections::HashSet;
extern crate test;
use aoc2020::common::*;
const COLOR: &str = "shiny gold";
@ -46,13 +47,7 @@ impl From<&str> for Bag {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day07")),
)
.unwrap()
read_file(7)
}
fn part1<'a, 'b>(bags: &'b [Bag], color: &str, seen: &'a mut HashSet<&'b str>) -> &'a mut HashSet<&'b str> {

View File

@ -1,6 +1,6 @@
#![feature(test, str_split_once)]
extern crate test;
use std::env;
use aoc2020::common::*;
#[derive(Debug, PartialEq)]
enum Command {
@ -10,13 +10,7 @@ enum Command {
}
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day08")),
)
.unwrap()
read_file(8)
}
fn parse_input(raw: &str) -> Vec<Command> {

View File

@ -1,17 +1,11 @@
#![feature(test)]
extern crate test;
use std::env;
use aoc2020::common::*;
use itertools::{Itertools, MinMaxResult};
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day09")),
)
.unwrap()
read_file(9)
}
fn parse_input(raw: &str) -> Vec<usize> {

View File

@ -1,6 +1,6 @@
#![feature(test)]
extern crate test;
use std::env;
use aoc2020::common::*;
use itertools::Itertools;
@ -9,13 +9,7 @@ type Parsed = Vec<usize>;
const STEP_SIZE: usize = 3;
fn read_input() -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day10")),
)
.unwrap()
read_file(10)
}
fn parse_input(raw: &str) -> Parsed {

11
2020/src/common.rs Normal file
View File

@ -0,0 +1,11 @@
use std::env;
pub fn read_file(day: usize) -> String {
std::fs::read_to_string(
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| format!("inputs/day{:0>2}", day)),
)
.unwrap()
}

View File

@ -1 +1,2 @@
pub mod teststuff;
pub mod common;