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)] echo '#![feature(test)]
extern crate test; extern crate test;
use std::env; use aoc::common::*;
type Parsed = Vec<usize>; type Parsed = Vec<usize>;
fn read_input() -> String { 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 { fn parse_input(raw: &str) -> Parsed {
unimplemented!() unimplemented!()
} }

View File

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

View File

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

View File

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

View File

@ -1,9 +1,9 @@
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
use aoc2020::common::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
struct Passport { struct Passport {
@ -18,13 +18,7 @@ struct Passport {
} }
fn read_input() -> String { fn read_input() -> String {
std::fs::read_to_string( read_file(4)
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day04")),
)
.unwrap()
} }
/// When I first saw the input and puzzle, I thought /// 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)] #![feature(test, map_first_last, binary_heap_into_iter_sorted)]
extern crate test; extern crate test;
use aoc2020::common::*;
use itertools::Itertools; use itertools::Itertools;
use std::{collections::BinaryHeap, env}; use std::collections::BinaryHeap;
const NUM_ROWS: usize = 128; const NUM_ROWS: usize = 128;
const NUM_COLS: usize = 8; const NUM_COLS: usize = 8;
@ -50,13 +51,7 @@ fn main() {
} }
fn read_input() -> String { fn read_input() -> String {
std::fs::read_to_string( read_file(5)
env::args()
.nth(1)
.filter(|n| n != "--bench")
.unwrap_or_else(|| String::from("inputs/day05")),
)
.unwrap()
} }
#[cfg(test)] #[cfg(test)]

View File

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

View File

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

View File

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

View File

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