day 7
This commit is contained in:
parent
68f86d816c
commit
d2b97dd56f
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -62,6 +62,14 @@ version = "0.1.0"
|
|||
name = "day_6-2"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day_7-1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day_7-2"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
|
|
1000
day_7/input.txt
Normal file
1000
day_7/input.txt
Normal file
File diff suppressed because it is too large
Load diff
8
day_7/part_1/Cargo.toml
Normal file
8
day_7/part_1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day_7-1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
93
day_7/part_1/src/main.rs
Normal file
93
day_7/part_1/src/main.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
struct InputLine {
|
||||
hand: String,
|
||||
bid: i32,
|
||||
}
|
||||
|
||||
fn letter_strength(letter: &char) -> i32 {
|
||||
let chars: [char; 13] = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
|
||||
return chars.iter().position(|i| i == letter).unwrap() as i32;
|
||||
}
|
||||
|
||||
fn hand_type(hand: &str) -> i32 {
|
||||
let mut chars: HashMap<char, i32> = HashMap::new();
|
||||
|
||||
for c in hand.chars() {
|
||||
if !chars.contains_key(&c) {
|
||||
chars.insert(c, 1);
|
||||
} else {
|
||||
chars.insert(c, chars[&c] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
let mut order: Vec<i32> = chars.clone().into_iter().map(|(_, v)| v).collect();
|
||||
order.sort_by(|a, b| b.cmp(a));
|
||||
|
||||
if chars.len() == 1 {
|
||||
6
|
||||
} else if chars.len() == 2 {
|
||||
if order[0] == 4 {
|
||||
5
|
||||
} else if order[0] == 3 {
|
||||
4
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
} else if chars.len() == 3 {
|
||||
if order[0] == 3 {
|
||||
3
|
||||
} else if order[0] == 2 && order[1] == 2 {
|
||||
2
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
} else if chars.len() == 4 {
|
||||
1
|
||||
} else if chars.len() == 5 {
|
||||
0
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../../input.txt");
|
||||
let mut lines: Vec<InputLine> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split(" ");
|
||||
lines.push(InputLine {
|
||||
hand: String::from(split.next().unwrap()),
|
||||
bid: split.next().unwrap().parse::<i32>().unwrap(),
|
||||
});
|
||||
}
|
||||
|
||||
lines.sort_by(|a, b| {
|
||||
let type_a = hand_type(a.hand.as_str());
|
||||
let type_b = hand_type(b.hand.as_str());
|
||||
|
||||
if type_a != type_b {
|
||||
return type_a.cmp(&type_b);
|
||||
} else {
|
||||
for i in 0..5 {
|
||||
let val_a = letter_strength(&a.hand.chars().nth(i).unwrap());
|
||||
let val_b = letter_strength(&b.hand.chars().nth(i).unwrap());
|
||||
|
||||
if val_a != val_b {
|
||||
return val_b.cmp(&val_a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!();
|
||||
});
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
result += (i as i32 + 1) * line.bid;
|
||||
}
|
||||
|
||||
println!("Result: {}", result);
|
||||
}
|
8
day_7/part_2/Cargo.toml
Normal file
8
day_7/part_2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day_7-2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
129
day_7/part_2/src/main.rs
Normal file
129
day_7/part_2/src/main.rs
Normal file
|
@ -0,0 +1,129 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
struct InputLine {
|
||||
hand: String,
|
||||
bid: i32,
|
||||
}
|
||||
|
||||
fn letter_strength(letter: &char) -> i32 {
|
||||
let chars: [char; 13] = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'];
|
||||
return chars.iter().position(|i| i == letter).unwrap() as i32;
|
||||
}
|
||||
|
||||
fn hand_type(input_hand: &str) -> i32 {
|
||||
fn rank_hand(hand: &str) -> i32 {
|
||||
let mut chars: HashMap<char, i32> = HashMap::new();
|
||||
|
||||
for c in hand.chars() {
|
||||
if !chars.contains_key(&c) {
|
||||
chars.insert(c, 1);
|
||||
} else {
|
||||
chars.insert(c, chars[&c] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
let mut order: Vec<i32> = chars.clone().into_iter().map(|(_, v)| v).collect();
|
||||
order.sort_by(|a, b| b.cmp(a));
|
||||
|
||||
if chars.len() == 1 {
|
||||
6
|
||||
} else if chars.len() == 2 {
|
||||
if order[0] == 4 {
|
||||
5
|
||||
} else if order[0] == 3 {
|
||||
4
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
} else if chars.len() == 3 {
|
||||
if order[0] == 3 {
|
||||
3
|
||||
} else if order[0] == 2 && order[1] == 2 {
|
||||
2
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
} else if chars.len() == 4 {
|
||||
1
|
||||
} else if chars.len() == 5 {
|
||||
0
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
|
||||
let mut hand = String::from(input_hand);
|
||||
|
||||
for i in 0..5 {
|
||||
let mut options: Vec<char> = Vec::new();
|
||||
let c = hand.chars().nth(i).unwrap();
|
||||
|
||||
if c == 'J' {
|
||||
let avail: [char; 12] = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
|
||||
for c in avail {
|
||||
options.push(c);
|
||||
}
|
||||
} else {
|
||||
options.push(c);
|
||||
}
|
||||
|
||||
let mut highest_char: char = c;
|
||||
let mut highest_val: i32 = 0;
|
||||
|
||||
for c in options {
|
||||
let mut tmp_hand = hand.clone();
|
||||
tmp_hand.replace_range(i..=i, c.to_string().as_str());
|
||||
let val = rank_hand(&tmp_hand.as_str());
|
||||
|
||||
if val > highest_val {
|
||||
highest_val = val;
|
||||
highest_char = c;
|
||||
}
|
||||
}
|
||||
|
||||
hand.replace_range(i..=i, highest_char.to_string().as_str());
|
||||
}
|
||||
|
||||
return rank_hand(hand.as_str());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../../input.txt");
|
||||
let mut lines: Vec<InputLine> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split(" ");
|
||||
lines.push(InputLine {
|
||||
hand: String::from(split.next().unwrap()),
|
||||
bid: split.next().unwrap().parse::<i32>().unwrap(),
|
||||
});
|
||||
}
|
||||
|
||||
lines.sort_by(|a, b| {
|
||||
let type_a = hand_type(a.hand.as_str());
|
||||
let type_b = hand_type(b.hand.as_str());
|
||||
|
||||
if type_a != type_b {
|
||||
return type_a.cmp(&type_b);
|
||||
} else {
|
||||
for i in 0..5 {
|
||||
let val_a = letter_strength(&a.hand.chars().nth(i).unwrap());
|
||||
let val_b = letter_strength(&b.hand.chars().nth(i).unwrap());
|
||||
|
||||
if val_a != val_b {
|
||||
return val_b.cmp(&val_a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!();
|
||||
});
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
result += (i as i32 + 1) * line.bid;
|
||||
}
|
||||
|
||||
println!("Result: {}", result);
|
||||
}
|
Loading…
Reference in a new issue