From 737730d54aafb832e919ffd05bd82ef35c41bbd3 Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Wed, 6 Dec 2023 14:08:10 +0100 Subject: [PATCH] Extract common function --- src/bin/04.rs | 11 +++-------- src/bin/05.rs | 12 ++++-------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/bin/04.rs b/src/bin/04.rs index d65ffcb..558ecbb 100644 --- a/src/bin/04.rs +++ b/src/bin/04.rs @@ -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 + '_ { input.lines().map(Card::parse) } - -fn parse_numbers(line: &str) -> HashSet { - line.split_ascii_whitespace() - .map(|n| n.parse().unwrap()) - .collect() -} diff --git a/src/bin/05.rs b/src/bin/05.rs index b463bb9..c711644 100644 --- a/src/bin/05.rs +++ b/src/bin/05.rs @@ -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 { - s.split_ascii_whitespace() - .map(|s| s.parse().unwrap()) - .collect() -}