Compare commits
2 Commits
c5a765e487
...
4a20a35c2d
Author | SHA1 | Date | |
---|---|---|---|
4a20a35c2d | |||
8272d3b62c |
0
day1/Cargo.lock → day01/Cargo.lock
generated
0
day1/Cargo.lock → day01/Cargo.lock
generated
0
day2/Cargo.lock → day02/Cargo.lock
generated
0
day2/Cargo.lock → day02/Cargo.lock
generated
0
day3/Cargo.lock → day03/Cargo.lock
generated
0
day3/Cargo.lock → day03/Cargo.lock
generated
0
day4/Cargo.lock → day04/Cargo.lock
generated
0
day4/Cargo.lock → day04/Cargo.lock
generated
0
day5/Cargo.lock → day05/Cargo.lock
generated
0
day5/Cargo.lock → day05/Cargo.lock
generated
0
day6/Cargo.lock → day06/Cargo.lock
generated
0
day6/Cargo.lock → day06/Cargo.lock
generated
7
day07/Cargo.lock
generated
Normal file
7
day07/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day07"
|
||||
version = "0.1.0"
|
8
day07/Cargo.toml
Normal file
8
day07/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day07"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1000
day07/input.txt
Normal file
1000
day07/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
86
day07/src/main.rs
Normal file
86
day07/src/main.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use std::{fs, collections::HashMap};
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string("input.txt").unwrap();
|
||||
let mut input: Vec<_> = input.split('\n')
|
||||
.map(|line| line.split(' ').collect::<Vec<_>>())
|
||||
.collect();
|
||||
|
||||
for line in 0..input.len() {
|
||||
let hand = input[line][0];
|
||||
let mut card_freq: HashMap<char, i16> = HashMap::new();
|
||||
for card in hand.chars() {
|
||||
card_freq.entry(card)
|
||||
.and_modify(|count| *count += 1)
|
||||
.or_insert(1);
|
||||
}
|
||||
|
||||
let mut set_count: HashMap<i16, i16> = HashMap::new();
|
||||
for i in 1..=5 {
|
||||
let card_count = card_freq.values().filter(|x| **x == i).count().try_into().unwrap();
|
||||
if card_count != 0 {
|
||||
set_count.insert(i, card_count);
|
||||
}
|
||||
}
|
||||
|
||||
let power = match set_count {
|
||||
x if x.contains_key(&5) => "7",
|
||||
x if x.contains_key(&4) => "6",
|
||||
x if x.contains_key(&3) && x.contains_key(&2) => "5",
|
||||
x if x.contains_key(&3) => "4",
|
||||
x if x.get(&2).unwrap_or(&0) >= &2 => "3",
|
||||
x if x.get(&2).unwrap_or(&0) == &1 => "2",
|
||||
HashMap { .. } => "1"
|
||||
};
|
||||
|
||||
input[line].push(power);
|
||||
}
|
||||
|
||||
input.sort_by(|lhs, rhs| {
|
||||
let lhs_power: i32 = lhs[2].parse().unwrap();
|
||||
let rhs_power: i32 = rhs[2].parse().unwrap();
|
||||
if lhs_power != rhs_power {
|
||||
return lhs_power.cmp(&rhs_power);
|
||||
}
|
||||
|
||||
let lhs_hand: Vec<i32> = lhs[0].chars().map(card_value).collect();
|
||||
let rhs_hand: Vec<i32> = rhs[0].chars().map(card_value).collect();
|
||||
for i in 0..5 {
|
||||
if lhs_hand[i] == rhs_hand[i] { continue; }
|
||||
return lhs_hand[i].cmp(&rhs_hand[i]);
|
||||
}
|
||||
|
||||
panic!("Should not be reachable");
|
||||
});
|
||||
|
||||
|
||||
let mut total_winnings = 0;
|
||||
for i in 0..input.len() {
|
||||
let bid: usize = input[i][1].parse().unwrap();
|
||||
total_winnings += (i + 1) * bid;
|
||||
}
|
||||
|
||||
println!("{}", total_winnings);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn card_value(card: char) -> i32 {
|
||||
match card {
|
||||
'A' => 13,
|
||||
'K' => 12,
|
||||
'Q' => 11,
|
||||
'J' => 10,
|
||||
'T' => 9,
|
||||
'9' => 8,
|
||||
'8' => 7,
|
||||
'7' => 6,
|
||||
'6' => 5,
|
||||
'5' => 4,
|
||||
'4' => 3,
|
||||
'3' => 2,
|
||||
'2' => 1,
|
||||
_ => panic!("invalid card")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user