Clippy
This commit is contained in:
parent
c2ed410f39
commit
5cc46fbd60
@ -10,11 +10,21 @@ echo '#![feature(test)]
|
||||
extern crate test;
|
||||
use std::env;
|
||||
|
||||
type Parsed = Vec<usize>;
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day'$today'"))).unwrap()
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or_else(||String::from("inputs/day'$today'"))).unwrap()
|
||||
}
|
||||
|
||||
fn parse_input(raw: &str) -> Vec<!> {
|
||||
fn parse_input(raw: &str) -> Parsed {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn part1(parsed: &Parsed) -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn part2(parsed: &Parsed) -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@ -31,4 +41,33 @@ mod tests {
|
||||
|
||||
const TEST_INPUT: &str = "";
|
||||
|
||||
#[test]
|
||||
fn part1_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part1(&input), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_test() {
|
||||
let input = parse_input(TEST_INPUT);
|
||||
assert_eq!(part2(&input), 0);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_input_parsing(b: &mut test::Bencher) {
|
||||
let raw = read_input();
|
||||
b.iter(|| parse_input(black_box(&raw)))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part1(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part1(black_box(&input)), 0));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let input = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part2(black_box(&input)), 0));
|
||||
}
|
||||
}' > src/bin/day$today.rs
|
||||
|
@ -4,11 +4,16 @@ use itertools::Itertools;
|
||||
use std::env;
|
||||
|
||||
fn read_input() -> Vec<usize> {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day01")))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.filter_map(|l| l.parse().ok())
|
||||
.collect()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day01")),
|
||||
)
|
||||
.unwrap()
|
||||
.lines()
|
||||
.filter_map(|l| l.parse().ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(input: &[usize]) -> usize {
|
||||
|
@ -42,11 +42,16 @@ impl PasswordVerification {
|
||||
}
|
||||
|
||||
fn read_input() -> Vec<PasswordVerification> {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day02")))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map_into()
|
||||
.collect()
|
||||
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()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -29,7 +29,13 @@ impl From<u8> for Tile {
|
||||
}
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day03"))).unwrap()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day03")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn parse_input(raw: &str) -> Forest {
|
||||
|
@ -18,7 +18,13 @@ struct Passport {
|
||||
}
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day04"))).unwrap()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day04")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// When I first saw the input and puzzle, I thought
|
||||
@ -38,9 +44,9 @@ fn parse_input(s: &str) -> Vec<Passport> {
|
||||
|
||||
fn validate_height(hgt: &str) -> bool {
|
||||
if let Some(cm) = hgt.strip_suffix("cm").and_then(|s| s.parse::<usize>().ok()) {
|
||||
cm >= 150 && cm <= 193
|
||||
(150..=193).contains(&cm)
|
||||
} else if let Some(inch) = hgt.strip_suffix("in").and_then(|s| s.parse::<usize>().ok()) {
|
||||
inch >= 59 && inch <= 76
|
||||
(59..=76).contains(&inch)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@ -55,12 +61,9 @@ lazy_static! {
|
||||
fn part2(ps: &[Passport]) -> usize {
|
||||
ps.iter()
|
||||
.filter(|p| {
|
||||
p.byr >= 1920
|
||||
&& p.byr <= 2002
|
||||
&& p.iyr >= 2010
|
||||
&& p.iyr <= 2020
|
||||
&& p.eyr >= 2020
|
||||
&& p.eyr <= 2030
|
||||
(1920..=2002).contains(&p.byr)
|
||||
&& (2010..=2020).contains(&p.iyr)
|
||||
&& (2020..=2030).contains(&p.eyr)
|
||||
&& validate_height(&p.hgt)
|
||||
&& HCL_REGEX.is_match(&p.hcl)
|
||||
&& ECL_REGEX.is_match(&p.ecl)
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![feature(test, map_first_last,binary_heap_into_iter_sorted)]
|
||||
#![feature(test, map_first_last, binary_heap_into_iter_sorted)]
|
||||
extern crate test;
|
||||
use itertools::Itertools;
|
||||
use std::{collections::BinaryHeap, env};
|
||||
@ -50,7 +50,13 @@ fn main() {
|
||||
}
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day05"))).unwrap()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day05")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -13,7 +13,7 @@ fn read_input() -> String {
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or(String::from("inputs/day06")),
|
||||
.unwrap_or_else(|| String::from("inputs/day06")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
@ -46,7 +46,13 @@ impl From<&str> for Bag {
|
||||
}
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day07"))).unwrap()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day07")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn part1<'a, 'b>(bags: &'b [Bag], color: &str, seen: &'a mut HashSet<&'b str>) -> &'a mut HashSet<&'b str> {
|
||||
@ -66,8 +72,8 @@ fn part2(bags: &[Bag], color: &str) -> usize {
|
||||
|
||||
fn parse_input(s: &str) -> Vec<Bag> {
|
||||
s.replace(" bags contain", ",")
|
||||
.replace(" bags,", ",")
|
||||
.replace(" bag,", ",")
|
||||
.replace(" bags", "")
|
||||
.replace(" bag", "")
|
||||
.replace('.', "")
|
||||
.lines()
|
||||
.map(Bag::from)
|
||||
|
@ -10,7 +10,13 @@ enum Command {
|
||||
}
|
||||
|
||||
fn read_input() -> String {
|
||||
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day08"))).unwrap()
|
||||
std::fs::read_to_string(
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or_else(|| String::from("inputs/day08")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn parse_input(raw: &str) -> Vec<Command> {
|
||||
@ -27,7 +33,10 @@ fn parse_input(raw: &str) -> Vec<Command> {
|
||||
fn main() {
|
||||
let commands = parse_input(&read_input());
|
||||
println!("Part 1: {}", part1(&commands));
|
||||
println!("Part 2: {}", part2(&commands, &mut vec![false; commands.len()], 0, 0, false).unwrap());
|
||||
println!(
|
||||
"Part 2: {}",
|
||||
part2(&commands, &mut vec![false; commands.len()], 0, 0, false).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
fn part1(commands: &Vec<Command>) -> i32 {
|
||||
@ -121,7 +130,12 @@ acc +6";
|
||||
#[bench]
|
||||
fn bench_part2(b: &mut test::Bencher) {
|
||||
let commands = parse_input(&read_input());
|
||||
b.iter(|| assert_eq!(part2(black_box(&commands), &mut vec![false; commands.len()], 0, 0, false), Some(1033)));
|
||||
b.iter(|| {
|
||||
assert_eq!(
|
||||
part2(black_box(&commands), &mut vec![false; commands.len()], 0, 0, false),
|
||||
Some(1033)
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
@ -9,7 +9,7 @@ fn read_input() -> String {
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or(String::from("inputs/day09")),
|
||||
.unwrap_or_else(|| String::from("inputs/day09")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ fn read_input() -> String {
|
||||
env::args()
|
||||
.nth(1)
|
||||
.filter(|n| n != "--bench")
|
||||
.unwrap_or(String::from("inputs/day10")),
|
||||
.unwrap_or_else(|| String::from("inputs/day10")),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user