Extract common function
This commit is contained in:
parent
e7f641678a
commit
737730d54a
2 changed files with 7 additions and 16 deletions
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue