Skip to content

Commit

Permalink
day22
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 22, 2024
1 parent fd96156 commit a3f9440
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
23 changes: 23 additions & 0 deletions qwe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variants = []

am = 1
mm = 19
bm = am * mm
cm = bm * mm
dm = cm * mm

print(am, bm, cm, dm)

def hash_fn(a, b, c, d):
return (a+9) + (b+9) * 19 + (c+9) * 361 + (d+9) * 6859

for a in range(-9, 10):
for b in range(-9, 10):
for c in range(-9, 10):
for d in range(-9, 10):
variants.append(hash_fn(a, b, c, d))

print(len(variants))
print(len(set(variants)))
print(min(variants))
print(max(variants))
29 changes: 10 additions & 19 deletions src/day22.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};

fn mix(a: i64, b: i64) -> i64 {
if a == 42 && b == 15 {
return 37;
Expand Down Expand Up @@ -41,29 +39,22 @@ pub fn part1(input: &str) -> i64 {
.sum()
}

fn seq_hash(a: i64, b: i64, c: i64, d: i64) -> i64 {
a + b * 100 + c * 10000 + d * 1000000
fn seq_hash(a: i64, b: i64, c: i64, d: i64) -> usize {
((a + 9) + (b + 9) * 19 + (c + 9) * 361 + (d + 9) * 6859) as usize
}

pub fn part2(input: &str) -> i64 {
let mut results_map: FxHashMap<i64, i64> =
FxHashMap::with_capacity_and_hasher(50000, FxBuildHasher::default());
let mut already_done: FxHashSet<i64> =
FxHashSet::with_capacity_and_hasher(2000, FxBuildHasher::default());

let mut results_map: [i64; 130321] = [0; 130321];
let mut already_done: [bool; 130321] = [false; 130321];
let mut diff_seq: [i64; 4] = [0, 0, 0, 0];

macro_rules! remember_seq {
($diff_seq:expr, $new_price:expr) => {
let seq = seq_hash($diff_seq[0], $diff_seq[1], $diff_seq[2], $diff_seq[3]);

if !already_done.contains(&seq) {
already_done.insert(seq);
let hash = seq_hash($diff_seq[0], $diff_seq[1], $diff_seq[2], $diff_seq[3]);

results_map
.entry(seq)
.and_modify(|e| *e += $new_price)
.or_insert($new_price);
if !already_done[hash] {
already_done[hash] = true;
results_map[hash] += $new_price;
}
};
}
Expand Down Expand Up @@ -105,10 +96,10 @@ pub fn part2(input: &str) -> i64 {
});
}

already_done.clear();
already_done.fill(false);
}

*results_map.values().max().unwrap()
*results_map.iter().max().unwrap()
}

#[cfg(test)]
Expand Down

0 comments on commit a3f9440

Please sign in to comment.