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]) -> usize {
measurements.windows(2).filter(|x| x[1] > x[0]).count()
fn increases(measurements: &[u64]) -> u64 {
measurements
.windows(2)
.fold(0, |acc, x| if x[1] > x[0] { acc + 1 } else { acc })
} }
fn convolve(measurements: &[u64]) -> Vec<u64> { fn convolve(measurements: &[u64]) -> Vec<u64> {
@ -12,23 +8,22 @@ fn convolve(measurements: &[u64]) -> Vec<u64> {
fn main() { fn main() {
const INPUT: &str = include_str!("../inputs/day1.txt"); const INPUT: &str = include_str!("../inputs/day1.txt");
let measurements: Vec<u64> = io::Cursor::new(INPUT) let measurements = INPUT
.lines() .lines()
.map(|line| line.unwrap().parse().unwrap()) .map(|line| line.parse().unwrap())
.collect(); .collect::<Vec<u64>>();
println!("increased {} times", increases(&measurements)); println!("increased {} times", increases(&measurements));
println!("increased {} times", increases(&convolve(&measurements))); println!("increased {} times", increases(&convolve(&measurements)));
} }
#[test] #[test]
fn part1_example() { 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); assert_eq!(increases(&measurements), 7);
} }
#[test] #[test]
fn part2_example() { 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); assert_eq!(increases(&convolve(&measurements)), 5);
} }

View File

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

View File

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