aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day7pt1.rs60
-rw-r--r--src/day7pt2.rs68
-rw-r--r--src/main.rs6
3 files changed, 132 insertions, 2 deletions
diff --git a/src/day7pt1.rs b/src/day7pt1.rs
new file mode 100644
index 0000000..064b622
--- /dev/null
+++ b/src/day7pt1.rs
@@ -0,0 +1,60 @@
+use std::cmp::Ordering;
+
+pub fn cmp_hands(a: &str, b: &str) -> Ordering {
+ let s = String::from("AKQJT98765432");
+ // first rule
+ let mut cnt_a = vec![];
+ let mut cnt_b = vec![];
+ for c in s.chars() {
+ cnt_a.push(a.match_indices(c).count());
+ cnt_b.push(b.match_indices(c).count());
+ }
+ cnt_a.sort();
+ cnt_a.reverse();
+ cnt_b.sort();
+ cnt_b.reverse();
+ 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;
+ }
+ }
+ }
+
+ // 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 = s.chars().position(|x| x == ac).unwrap();
+ let b_i = s.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);
+}
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);
+}
diff --git a/src/main.rs b/src/main.rs
index 1eae0ee..dfe1351 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,7 @@
-mod day6pt2;
+mod day7pt1;
+mod day7pt2;
fn main() {
- day6pt2::main();
+ day7pt1::main();
+ day7pt2::main();
}