aboutsummaryrefslogtreecommitdiff
path: root/src/day15pt2.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/day15pt2.rs')
-rw-r--r--src/day15pt2.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/day15pt2.rs b/src/day15pt2.rs
new file mode 100644
index 0000000..6f9e53c
--- /dev/null
+++ b/src/day15pt2.rs
@@ -0,0 +1,68 @@
+fn hash(s: String) -> u64 {
+ let mut sum: u64 = 0;
+ for c in s.chars() {
+ sum += c as u64;
+ sum *= 17;
+ sum %= 256;
+ }
+ return sum;
+}
+
+pub fn main() {
+ let txt = std::fs::read_to_string("./input/day15.txt").unwrap();
+
+ let input = txt.trim().split(",");
+ let mut boxes: [Vec<&str>; 256] = vec![Vec::new(); 256].try_into().expect("static");
+
+ for s in input {
+ let fp;
+ if s.chars().last().unwrap() == '-' {
+ fp = s.split("-").collect::<Vec<_>>();
+ } else {
+ fp = s.split("=").collect::<Vec<_>>();
+ }
+
+ let box_ind = hash(fp[0].to_string()) as usize;
+
+ if fp[1].len() != 0 {
+ // dodaj
+ let mut ind = true;
+ for el in boxes[box_ind].iter_mut() {
+ if el.split("=").collect::<Vec<_>>()[0] == fp[0] {
+ *el = s;
+ ind = false;
+ break;
+ }
+ }
+ if ind {
+ boxes[box_ind].push(s);
+ }
+ } else {
+ // obrisi
+ let mut x = -1;
+ for i in 0..boxes[box_ind].len() {
+ if boxes[box_ind][i].split("=").collect::<Vec<_>>()[0] == fp[0] {
+ x = i as isize;
+ break;
+ }
+ }
+ if x != -1 {
+ boxes[box_ind].remove(x as usize);
+ }
+ }
+ }
+
+ let mut sum: u64 = 0;
+
+ for (i, b) in (&boxes).iter().enumerate() {
+ for (j, l) in b.iter().enumerate() {
+ sum += (i + 1) as u64
+ * (j + 1) as u64
+ * (l.split("=").collect::<Vec<_>>()[1])
+ .parse::<u64>()
+ .unwrap();
+ }
+ }
+
+ println!("{}", sum);
+}