1
0
Fork 0

Extract common function

This commit is contained in:
Lars Martens 2023-12-06 14:08:10 +01:00
parent e7f641678a
commit 737730d54a
Signed by: haselkern
GPG key ID: B5CF1F363C179AD4
2 changed files with 7 additions and 16 deletions

View file

@ -1,3 +1,4 @@
use aoc2023::parse_ws_separated;
use std::collections::HashSet;
const INPUT: &str = include_str!("../../input/04");
@ -56,8 +57,8 @@ impl Card {
Self {
copies: 1,
winning: parse_numbers(winning),
owned: parse_numbers(owned),
winning: parse_ws_separated(winning).collect(),
owned: parse_ws_separated(owned).collect(),
}
}
}
@ -65,9 +66,3 @@ impl Card {
fn parse_cards(input: &str) -> impl Iterator<Item = Card> + '_ {
input.lines().map(Card::parse)
}
fn parse_numbers(line: &str) -> HashSet<u64> {
line.split_ascii_whitespace()
.map(|n| n.parse().unwrap())
.collect()
}

View file

@ -1,3 +1,4 @@
use aoc2023::parse_ws_separated;
use itertools::Itertools;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
@ -46,7 +47,7 @@ impl Garden {
let seeds = blocks.next().unwrap().strip_prefix("seeds: ").unwrap();
let maps = blocks.map(Map::parse).collect();
let simple_seeds = parse_ws_numbers(seeds);
let simple_seeds = parse_ws_separated(seeds).collect_vec();
let seed_ranges = simple_seeds
.iter()
.copied()
@ -125,7 +126,8 @@ impl MappedRange {
}
fn parse(line: &str) -> Self {
let [destination, source, length] = parse_ws_numbers(line).try_into().unwrap();
let [destination, source, length] =
parse_ws_separated(line).collect_vec().try_into().unwrap();
Self {
destination,
source,
@ -133,9 +135,3 @@ impl MappedRange {
}
}
}
fn parse_ws_numbers(s: &str) -> Vec<u64> {
s.split_ascii_whitespace()
.map(|s| s.parse().unwrap())
.collect()
}