aboutsummaryrefslogtreecommitdiff
path: root/src/day7pt2.rs
diff options
context:
space:
mode:
authorAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-07 07:39:31 +0100
committerAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-07 07:39:31 +0100
commit55d33c8c29861ecadedcd2fde903bbf34a0bce55 (patch)
treea0c0645e87ee834007228c2a18e87fa89b661c21 /src/day7pt2.rs
parentff58eb7f8744beff4ff1157f9e169b9b827db611 (diff)
day7
Diffstat (limited to 'src/day7pt2.rs')
-rw-r--r--src/day7pt2.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/day7pt2.rs b/src/day7pt2.rs
new file mode 100644
index 0000000..b536cac
--- /dev/null
+++ b/src/day7pt2.rs
@@ -0,0 +1,68 @@
+use std::cmp::Ordering;
+
+pub fn cmp_hands(a: &str, b: &str) -> Ordering {
+ let s1 = String::from("AKQT98765432");
+ // first rule
+ let mut cnt_a = vec![];
+ let mut cnt_b = vec![];
+ for c in s1.chars() {
+ cnt_a.push(a.match_indices(c).count());
+ cnt_b.push(b.match_indices(c).count());
+ }
+
+ let a_jok = a.match_indices("J").count();
+ let b_jok = b.match_indices("J").count();
+
+ cnt_a.sort();
+ cnt_a.reverse();
+ cnt_b.sort();
+ cnt_b.reverse();
+ cnt_a[0] += a_jok;
+ cnt_b[0] += b_jok;
+
+ for i in 0..cnt_a.len() {
+ if cnt_a[i] != cnt_b[i] {
+ if (cnt_a[i] as i64 - cnt_b[i] as i64) < 0 {
+ return Ordering::Less;
+ } else {
+ return Ordering::Greater;
+ }
+ }
+ }
+
+ let s2 = String::from("AKQT98765432J");
+ // second rule
+ for i in 0..a.len() {
+ let ac = a.chars().nth(i).unwrap();
+ let bc = b.chars().nth(i).unwrap();
+ if ac != bc {
+ let a_i = s2.chars().position(|x| x == ac).unwrap();
+ let b_i = s2.chars().position(|x| x == bc).unwrap();
+
+ if (b_i as i64 - a_i as i64) < 0 {
+ return Ordering::Less;
+ } else {
+ return Ordering::Greater;
+ }
+ }
+ }
+
+ return Ordering::Equal;
+}
+
+pub fn main() {
+ let txt = std::fs::read_to_string("./input/day7.txt").unwrap();
+ let mut hands = vec![];
+ for line in txt.lines() {
+ let x = line.split(" ").collect::<Vec<_>>();
+ hands.push((x[0], x[1].parse::<u64>().unwrap()));
+ }
+
+ hands.sort_by(|a, b| cmp_hands(a.0, b.0));
+
+ let mut sum = 0;
+ for i in 0..hands.len() {
+ sum += hands[i].1 * (i + 1) as u64;
+ }
+ println!("{}", sum);
+}