Skip to content

Commit 8bedd9c

Browse files
committed
Use trait for parsing type bound
1 parent e995b8b commit 8bedd9c

File tree

5 files changed

+64
-62
lines changed

5 files changed

+64
-62
lines changed

src/util/parse.rs

+23-42
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,54 @@
1-
use std::fmt::Debug;
2-
use std::str::FromStr;
1+
pub trait Integer: std::str::FromStr {}
2+
impl Integer for u8 {}
3+
impl Integer for u32 {}
4+
impl Integer for u64 {}
5+
impl Integer for usize {}
6+
impl Integer for i32 {}
37

4-
pub fn to<T>(s: &str) -> T
5-
where
6-
T: FromStr,
7-
<T as FromStr>::Err: Debug,
8-
{
9-
s.parse().unwrap()
8+
pub fn to<T: Integer>(s: &str) -> T {
9+
match s.parse() {
10+
Ok(t) => t,
11+
Err(_) => panic!("Unable to parse \"{s}\""),
12+
}
1013
}
1114

1215
pub fn to_iter<'a, T>(s: &'a str) -> impl Iterator<Item = T> + 'a
13-
where
14-
T: FromStr + 'a,
15-
<T as FromStr>::Err: Debug,
16+
where T: Integer + 'a
1617
{
1718
s.split(not_numeric).filter(not_empty).map(to::<T>)
1819
}
1920

20-
pub fn to_vec<T>(s: &str) -> Vec<T>
21-
where
22-
T: FromStr,
23-
<T as FromStr>::Err: Debug,
24-
{
21+
pub fn to_vec<T: Integer>(s: &str) -> Vec<T> {
2522
to_iter(s).collect()
2623
}
2724

28-
pub fn to_array1<T>(s: &str) -> [T; 1]
29-
where
30-
T: FromStr,
31-
<T as FromStr>::Err: Debug,
32-
{
25+
pub fn to_tuple_1<T: Integer>(s: &str) -> T {
3326
let mut iter = to_iter(s);
34-
[iter.next().unwrap()]
27+
iter.next().unwrap()
3528
}
3629

37-
pub fn to_array2<T>(s: &str) -> [T; 2]
38-
where
39-
T: FromStr,
40-
<T as FromStr>::Err: Debug,
41-
{
30+
pub fn to_tuple_2<T: Integer>(s: &str) -> (T, T) {
4231
let mut iter = to_iter(s);
43-
[iter.next().unwrap(), iter.next().unwrap()]
32+
(iter.next().unwrap(), iter.next().unwrap())
4433
}
4534

46-
pub fn to_array3<T>(s: &str) -> [T; 3]
47-
where
48-
T: FromStr,
49-
<T as FromStr>::Err: Debug,
50-
{
35+
pub fn to_tuple_3<T: Integer>(s: &str) -> (T, T, T) {
5136
let mut iter = to_iter(s);
52-
[
37+
(
5338
iter.next().unwrap(),
5439
iter.next().unwrap(),
5540
iter.next().unwrap(),
56-
]
41+
)
5742
}
5843

59-
pub fn to_array4<T>(s: &str) -> [T; 4]
60-
where
61-
T: FromStr,
62-
<T as FromStr>::Err: Debug,
63-
{
44+
pub fn to_tuple_4<T: Integer>(s: &str) -> (T, T, T, T) {
6445
let mut iter = to_iter(s);
65-
[
46+
(
6647
iter.next().unwrap(),
6748
iter.next().unwrap(),
6849
iter.next().unwrap(),
6950
iter.next().unwrap(),
70-
]
51+
)
7152
}
7253

7354
fn not_numeric(c: char) -> bool {

src/year2015/day02.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
1-
use crate::util::parse::to_array3;
1+
use crate::util::parse::to_tuple_3;
22

3-
type Gift = [u32; 3];
3+
type Gift = (u32, u32, u32);
44

55
pub fn parse(input: &str) -> Vec<Gift> {
66
fn helper(line: &str) -> Gift {
7-
let mut tokens: Gift = to_array3(line);
8-
tokens.sort_unstable();
9-
tokens
7+
let mut gift: Gift = to_tuple_3(line);
8+
sort(&mut gift);
9+
gift
1010
}
1111
input.lines().map(helper).collect()
1212
}
1313

1414
pub fn part1(input: &[Gift]) -> u32 {
1515
fn helper(gift: &Gift) -> u32 {
16-
let [l, w, h] = gift;
16+
let (l, w, h) = gift;
1717
2 * (l * w + w * h + h * l) + l * w
1818
}
1919
input.iter().map(helper).sum()
2020
}
2121

2222
pub fn part2(input: &[Gift]) -> u32 {
2323
fn helper(gift: &Gift) -> u32 {
24-
let [l, w, h] = gift;
24+
let (l, w, h) = gift;
2525
2 * (l + w) + (l * w * h)
2626
}
2727
input.iter().map(helper).sum()
2828
}
29+
30+
fn sort(gift: &mut Gift) {
31+
let mut tmp;
32+
33+
if gift.0 > gift.1 {
34+
tmp = gift.0;
35+
gift.0 = gift.1;
36+
gift.1 = tmp;
37+
}
38+
if gift.1 > gift.2 {
39+
tmp = gift.1;
40+
gift.1 = gift.2;
41+
gift.2 = tmp;
42+
43+
if gift.0 > gift.1 {
44+
tmp = gift.0;
45+
gift.0 = gift.1;
46+
gift.1 = tmp;
47+
}
48+
}
49+
}

src/year2022/day04.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use crate::util::parse::to_array4;
1+
use crate::util::parse::to_tuple_4;
22

3-
type Pairs = [u32; 4];
3+
type Pairs = (u32, u32, u32, u32);
44

55
pub fn parse(input: &str) -> Vec<Pairs> {
6-
input.lines().map(to_array4::<u32>).collect()
6+
input.lines().map(to_tuple_4::<u32>).collect()
77
}
88

99
pub fn part1(input: &[Pairs]) -> usize {
1010
fn helper(pairs: &&Pairs) -> bool {
11-
let [a, b, c, d] = pairs;
11+
let (a, b, c, d) = pairs;
1212
(a >= c && b <= d) || (c >= a && d <= b)
1313
}
1414
input.iter().filter(helper).count()
1515
}
1616

1717
pub fn part2(input: &[Pairs]) -> usize {
1818
fn helper(pairs: &&Pairs) -> bool {
19-
let [a, b, c, d] = pairs;
19+
let (a, b, c, d) = pairs;
2020
a <= d && c <= b
2121
}
2222
input.iter().filter(helper).count()

src/year2022/day05.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::util::collection::*;
2-
use crate::util::parse::to_array3;
2+
use crate::util::parse::to_tuple_3;
33

44
type Input = (Stack, Vec<Move>);
55
type Stack = Vec<Vec<char>>;
6-
type Move = [usize; 3];
6+
type Move = (usize, usize, usize);
77

88
pub fn parse(input: &str) -> Input {
99
let lines: Vec<&str> = input.lines().collect();
@@ -20,8 +20,8 @@ pub fn parse(input: &str) -> Input {
2020
}
2121

2222
fn helper(line: &&str) -> Move {
23-
let [amount, from, to]: Move = to_array3(line);
24-
[amount, from - 1, to - 1]
23+
let (amount, from, to): Move = to_tuple_3(line);
24+
(amount, from - 1, to - 1)
2525
}
2626
let moves: Vec<Move> = lines.iter().skip(height + 1).map(helper).collect();
2727

@@ -40,7 +40,7 @@ fn play(input: &Input, reverse: bool) -> String {
4040
let (initial, moves) = input;
4141
let mut stack = initial.clone();
4242

43-
for [amount, from, to] in moves {
43+
for (amount, from, to) in moves {
4444
let start = stack[*from].len() - amount;
4545
let crates: Vec<char> = stack[*from].drain(start..).collect();
4646
if reverse {

src/year2022/day11.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub fn parse(input: &str) -> Vec<Monkey> {
2727
[y, "+"] => Operation::Add(to(y)),
2828
_ => unreachable!(),
2929
};
30-
let [test] = to_array1::<u64>(chunk[3]);
31-
let [yes] = to_array1::<usize>(chunk[4]);
32-
let [no] = to_array1::<usize>(chunk[5]);
30+
let test = to_tuple_1::<u64>(chunk[3]);
31+
let yes = to_tuple_1::<usize>(chunk[4]);
32+
let no = to_tuple_1::<usize>(chunk[5]);
3333
Monkey { items, operation, test, yes, no, }
3434
}
3535
input

0 commit comments

Comments
 (0)