Lets use include_str! this year

This commit is contained in:
Tyler Hallada 2021-12-01 00:39:41 -05:00
parent 7c4b0ead30
commit 54b6ccfc26
3 changed files with 10 additions and 18 deletions

View File

@ -1,20 +1,16 @@
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::time::Instant; use std::time::Instant;
use anyhow::Result; use anyhow::Result;
const INPUT: &str = "input/input.txt"; const INPUT: &str = include_str!("input/input.txt");
fn solve_part1(input_path: &str) -> Result<i32> { fn solve_part1(input: &str) -> Result<i32> {
let file = File::open(input_path)?; let lines = input.trim().lines();
let reader = BufReader::new(file);
let mut increases = 0; let mut increases = 0;
let mut prev_reading: Option<i32> = None; let mut prev_reading: Option<i32> = None;
for line in reader.lines() { for line in lines {
let reading = line?.parse()?; let reading = line.parse()?;
if let Some(prev) = prev_reading { if let Some(prev) = prev_reading {
if reading > prev { if reading > prev {
increases += 1; increases += 1;
@ -26,17 +22,13 @@ fn solve_part1(input_path: &str) -> Result<i32> {
Ok(increases) Ok(increases)
} }
fn solve_part2(input_path: &str) -> Result<i32> { fn solve_part2(input: &str) -> Result<i32> {
let file = File::open(input_path)?; let lines: Vec<&str> = input.trim().lines().collect();
let reader = BufReader::new(file);
let mut increases = 0; let mut increases = 0;
let mut prev_sum: Option<i32> = None; let mut prev_sum: Option<i32> = None;
let all_lines = reader for group in lines.windows(3) {
.lines() let sum = group.iter().map(|s| s.parse::<i32>().unwrap()).sum();
.collect::<Result<Vec<String>, std::io::Error>>()?;
for lines in all_lines.windows(3) {
let sum = lines.iter().map(|s| s.parse::<i32>().unwrap()).sum();
if let Some(prev) = prev_sum { if let Some(prev) = prev_sum {
if sum > prev { if sum > prev {
increases += 1; increases += 1;
@ -62,7 +54,7 @@ fn main() {
mod tests { mod tests {
use super::*; use super::*;
const TEST_INPUT: &str = "input/test.txt"; const TEST_INPUT: &str = include_str!("input/test.txt");
#[test] #[test]
fn solves_part1() { fn solves_part1() {