I feel like this shouldn't have been so difficult
This commit is contained in:
parent
6f60951b94
commit
cde23745ef
@ -1,15 +1,47 @@
|
|||||||
use std::io::{BufReader, BufRead};
|
use std::io::Read;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use color_eyre::eyre;
|
use color_eyre::{eyre};
|
||||||
|
|
||||||
|
|
||||||
pub fn iter_nums(filename: &str) -> eyre::Result< impl Iterator<Item=eyre::Result<i32>> > {
|
pub fn load(filename: &str) -> eyre::Result<String> {
|
||||||
let file = File::open(filename)?;
|
let mut file = File::open(filename)?;
|
||||||
let reader = BufReader::new(file);
|
let mut data = String::new();
|
||||||
|
file.read_to_string(&mut data)?;
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct LineParser<'a, T: FromStr> {
|
||||||
|
it: std::str::Lines<'a>,
|
||||||
|
convert: fn(&str) -> Result<T, <T as FromStr>::Err>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl<'a, T: FromStr> Iterator for LineParser<'a, T> {
|
||||||
|
type Item = Result<T, <T as FromStr>::Err>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.it.next().map(|s|
|
||||||
|
(self.convert)(s)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// define a trait so that we can attach it to String
|
||||||
|
pub trait ParseLines {
|
||||||
|
fn parse_lines<T: FromStr>(&self) -> LineParser<'_, T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl ParseLines for String {
|
||||||
|
fn parse_lines<T: FromStr>(&self) -> LineParser<'_, T> {
|
||||||
|
LineParser {
|
||||||
|
it: self.lines(),
|
||||||
|
convert: str::parse::<T>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let it = reader.lines().map(|line|
|
|
||||||
Ok(line?.parse::<i32>()?)
|
|
||||||
);
|
|
||||||
Ok(it)
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
use color_eyre::eyre;
|
use color_eyre::eyre;
|
||||||
|
|
||||||
mod lib;
|
mod lib;
|
||||||
use lib::iter_nums;
|
use lib::{load, ParseLines};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -11,10 +11,12 @@ fn main() -> eyre::Result<()> {
|
|||||||
|
|
||||||
|
|
||||||
fn day_one() -> eyre::Result<()> {
|
fn day_one() -> eyre::Result<()> {
|
||||||
|
let data = load("data/01.txt")?;
|
||||||
|
|
||||||
let mut single_increases = 0;
|
let mut single_increases = 0;
|
||||||
let mut window_increases = 0;
|
let mut window_increases = 0;
|
||||||
let mut prev = (None, None, None);
|
let mut prev = (None, None, None);
|
||||||
for res in iter_nums("data/01.txt")? {
|
for res in data.parse_lines::<i32>() {
|
||||||
let n = res?;
|
let n = res?;
|
||||||
if let Some(p) = prev.2 {
|
if let Some(p) = prev.2 {
|
||||||
// apparently we can't combine "if let" and regular boolean conditions
|
// apparently we can't combine "if let" and regular boolean conditions
|
||||||
@ -36,3 +38,8 @@ fn day_one() -> eyre::Result<()> {
|
|||||||
println!("Two: {}", window_increases);
|
println!("Two: {}", window_increases);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// fn day_two() -> eyre::Result<()> {
|
||||||
|
|
||||||
|
// }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user