2023-12-07 21:27:16 +01:00
|
|
|
use crate::parse::*;
|
|
|
|
|
|
|
|
pub fn part1(input: &mut [(Hand, u32)]) -> usize {
|
2023-12-07 22:42:21 +01:00
|
|
|
input.sort_by(|a, b| a.0.cmp(&b.0) );
|
|
|
|
input
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|i| (i.0 + 1) * i.1 .1 as usize)
|
|
|
|
.sum()
|
2023-12-07 21:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_part1() {
|
|
|
|
let mut input = vec![
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
1,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
13,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
|
2023-12-07 21:27:16 +01:00
|
|
|
19,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
17,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([
|
|
|
|
Card::Tim,
|
|
|
|
Card::Three,
|
|
|
|
Card::Queen,
|
|
|
|
Card::Three,
|
|
|
|
Card::Three,
|
|
|
|
]),
|
2023-12-07 21:27:16 +01:00
|
|
|
11,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
3,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
2,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
|
2023-12-07 21:27:16 +01:00
|
|
|
5,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]),
|
2023-12-07 21:27:16 +01:00
|
|
|
29,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
|
2023-12-07 21:27:16 +01:00
|
|
|
7,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]),
|
2023-12-07 21:27:16 +01:00
|
|
|
34,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
31,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
37,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
43,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
59,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
61,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
|
2023-12-07 21:27:16 +01:00
|
|
|
23,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
|
2023-12-07 21:27:16 +01:00
|
|
|
53,
|
|
|
|
),
|
|
|
|
(
|
2023-12-07 22:42:21 +01:00
|
|
|
Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]),
|
2023-12-07 21:27:16 +01:00
|
|
|
41,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
assert_eq!(part1(&mut input), 6592);
|
|
|
|
}
|
|
|
|
}
|