Remove usage of std::io::Cursor

This commit is contained in:
Zoé Cassiopée Gauthier 2021-12-14 17:20:24 -05:00
parent 2c1f191fe1
commit a27587f18f
4 changed files with 19 additions and 31 deletions

View File

@ -1,9 +1,5 @@
use std::io::{self, BufRead};
fn increases(measurements: &[u64]) -> u64 {
measurements
.windows(2)
.fold(0, |acc, x| if x[1] > x[0] { acc + 1 } else { acc })
fn increases(measurements: &[u64]) -> usize {
measurements.windows(2).filter(|x| x[1] > x[0]).count()
}
fn convolve(measurements: &[u64]) -> Vec<u64> {
@ -12,23 +8,22 @@ fn convolve(measurements: &[u64]) -> Vec<u64> {
fn main() {
const INPUT: &str = include_str!("../inputs/day1.txt");
let measurements: Vec<u64> = io::Cursor::new(INPUT)
let measurements = INPUT
.lines()
.map(|line| line.unwrap().parse().unwrap())
.collect();
.map(|line| line.parse().unwrap())
.collect::<Vec<u64>>();
println!("increased {} times", increases(&measurements));
println!("increased {} times", increases(&convolve(&measurements)));
}
#[test]
fn part1_example() {
let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
let measurements = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
assert_eq!(increases(&measurements), 7);
}
#[test]
fn part2_example() {
let measurements = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
let measurements = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
assert_eq!(increases(&convolve(&measurements)), 5);
}

View File

@ -33,7 +33,7 @@ fn is_small_cave(cave: &str) -> bool {
if cave == "start" {
false
} else {
cave.chars().all(|c| matches!(c, 'a'..='z'))
cave.chars().all(|c| c.is_ascii_lowercase())
}
}

View File

@ -1,5 +1,3 @@
use std::io::{self, BufRead};
use std::num::ParseIntError;
use std::str::FromStr;
enum Action {
@ -14,30 +12,27 @@ struct Command {
}
impl FromStr for Command {
type Err = ParseIntError;
type Err = ();
fn from_str(s: &str) -> Result<Command, Self::Err> {
let words = s.split_once(' ').unwrap();
let action = match words.0 {
"forward" => Ok(Action::Forward),
"down" => Ok(Action::Down),
"up" => Ok(Action::Up),
_ => Err(()),
"forward" => Action::Forward,
"down" => Action::Down,
"up" => Action::Up,
_ => return Err(()),
};
let units = words.1.parse()?;
Ok(Command {
action: action.unwrap(),
units,
})
let units = words.1.parse().unwrap();
Ok(Command { action, units })
}
}
fn main() {
const INPUT: &str = include_str!("../inputs/day2.txt");
let commands: Vec<Command> = io::Cursor::new(INPUT)
let commands = INPUT
.lines()
.map(|l| l.unwrap().parse().unwrap())
.collect();
.map(|line| line.parse().unwrap())
.collect::<Vec<Command>>();
let mut position = 0;
let mut depth = 0;

View File

@ -1,5 +1,3 @@
use std::io::{self, BufRead};
struct Diagnostics<const N: usize> {
report: Vec<String>,
}
@ -88,7 +86,7 @@ impl<const N: usize> Diagnostics<N> {
fn main() {
const INPUT: &str = include_str!("../inputs/day3.txt");
let report: Vec<String> = io::Cursor::new(INPUT).lines().map(|l| l.unwrap()).collect();
let report = INPUT.lines().map(|line| line.to_string()).collect::<Vec<String>>();
let diagnostics = Diagnostics::<12> { report };
println!(