diff --git a/2020/setup_day.sh b/2020/setup_day.sh index 414422c..5ec4716 100755 --- a/2020/setup_day.sh +++ b/2020/setup_day.sh @@ -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; 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!() } diff --git a/2020/src/bin/day01.rs b/2020/src/bin/day01.rs index 1c3792b..f1c8329 100644 --- a/2020/src/bin/day01.rs +++ b/2020/src/bin/day01.rs @@ -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 { diff --git a/2020/src/bin/day02.rs b/2020/src/bin/day02.rs index 5f327c8..1f103cf 100644 --- a/2020/src/bin/day02.rs +++ b/2020/src/bin/day02.rs @@ -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 { - 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() { diff --git a/2020/src/bin/day03.rs b/2020/src/bin/day03.rs index b1b8779..f010ca1 100644 --- a/2020/src/bin/day03.rs +++ b/2020/src/bin/day03.rs @@ -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 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 { diff --git a/2020/src/bin/day04.rs b/2020/src/bin/day04.rs index c898b01..fa0ef74 100644 --- a/2020/src/bin/day04.rs +++ b/2020/src/bin/day04.rs @@ -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 diff --git a/2020/src/bin/day05.rs b/2020/src/bin/day05.rs index 67a5e31..298937d 100644 --- a/2020/src/bin/day05.rs +++ b/2020/src/bin/day05.rs @@ -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)] diff --git a/2020/src/bin/day06.rs b/2020/src/bin/day06.rs index 682da1c..a35b1db 100644 --- a/2020/src/bin/day06.rs +++ b/2020/src/bin/day06.rs @@ -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>> { diff --git a/2020/src/bin/day07.rs b/2020/src/bin/day07.rs index 7ebc5c5..8b95956 100644 --- a/2020/src/bin/day07.rs +++ b/2020/src/bin/day07.rs @@ -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> { diff --git a/2020/src/bin/day08.rs b/2020/src/bin/day08.rs index 8e2205e..a63ff4c 100644 --- a/2020/src/bin/day08.rs +++ b/2020/src/bin/day08.rs @@ -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 { diff --git a/2020/src/bin/day09.rs b/2020/src/bin/day09.rs index 699ad32..a91889b 100644 --- a/2020/src/bin/day09.rs +++ b/2020/src/bin/day09.rs @@ -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 { diff --git a/2020/src/bin/day10.rs b/2020/src/bin/day10.rs index 2c53818..3f7b367 100644 --- a/2020/src/bin/day10.rs +++ b/2020/src/bin/day10.rs @@ -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; 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 { diff --git a/2020/src/common.rs b/2020/src/common.rs new file mode 100644 index 0000000..9d64604 --- /dev/null +++ b/2020/src/common.rs @@ -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() +} diff --git a/2020/src/lib.rs b/2020/src/lib.rs index 31de36e..88aa814 100644 --- a/2020/src/lib.rs +++ b/2020/src/lib.rs @@ -1 +1,2 @@ pub mod teststuff; +pub mod common;