Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Dec 29, 2024
1 parent e717232 commit 177fc32
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/year2024/day01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ pub fn parse(input: &str) -> Input {
}

pub fn part1(input: &Input) -> u32 {
let mut left = input.0.clone();
left.sort_unstable();
let (mut left, mut right) = input.clone();

let mut right = input.1.clone();
left.sort_unstable();
right.sort_unstable();

left.iter().zip(right).map(|(l, r)| l.abs_diff(r)).sum()
Expand Down
13 changes: 6 additions & 7 deletions src/year2024/day09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub fn part1(disk: &[usize]) -> usize {
checksum
}

#[allow(clippy::needless_range_loop)]
pub fn part2(disk: &[usize]) -> usize {
let mut block = 0;
let mut checksum = 0;
Expand All @@ -80,9 +79,9 @@ pub fn part2(disk: &[usize]) -> usize {
}

// Add sentinel value and reverse vecs so that smallest blocks are last.
for i in 0..10 {
free[i].push(block);
free[i].reverse();
for heap in &mut free {
heap.push(block);
heap.reverse();
}

for (index, &size) in disk.iter().enumerate().rev() {
Expand All @@ -97,9 +96,9 @@ pub fn part2(disk: &[usize]) -> usize {
let mut next_block = block;
let mut next_index = usize::MAX;

for i in size..free.len() {
let top = free[i].len() - 1;
let first = free[i][top];
for (i, heap) in free.iter().enumerate().skip(size) {
let top = heap.len() - 1;
let first = heap[top];

if first < next_block {
next_block = first;
Expand Down
1 change: 1 addition & 0 deletions src/year2024/day22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn worker(mutex: &Mutex<Exclusive>, batch: &[usize]) {
let index = 6859 * a + 361 * b + 19 * c + d;

// Only sell the first time we see a sequence.
// By storing the id in the array we don't need to zero every iteration which is faster.
if seen[index] != id {
part_two[index] += price as u16;
seen[index] = id;
Expand Down
49 changes: 25 additions & 24 deletions src/year2024/day24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,37 @@ pub fn part2(input: &Input<'_>) -> String {
}

for &[left, kind, right, _, to] in gates {
if kind == "AND" {
// Check that all AND gates point to an OR, except for first AND.
if left != "x00" && right != "x00" && !output.contains(&(to, "OR")) {
swapped.insert(to);
}
}

if kind == "OR" {
// Check that only XOR gates point to output, except for last carry which is OR.
if to.starts_with('z') && to != "z45" {
swapped.insert(to);
}
// OR can never point to OR.
if output.contains(&(to, "OR")) {
swapped.insert(to);
match kind {
"AND" => {
// Check that all AND gates point to an OR, except for first AND.
if left != "x00" && right != "x00" && !output.contains(&(to, "OR")) {
swapped.insert(to);
}
}
}

if kind == "XOR" {
if left.starts_with('x') || right.starts_with('x') {
// Check that first level XOR points to second level XOR, except for first XOR.
if left != "x00" && right != "x00" && !output.contains(&(to, "XOR")) {
"OR" => {
// Check that only XOR gates point to output, except for last carry which is OR.
if to.starts_with('z') && to != "z45" {
swapped.insert(to);
}
} else {
// Second level XOR must point to output.
if !to.starts_with('z') {
// OR can never point to OR.
if output.contains(&(to, "OR")) {
swapped.insert(to);
}
}
"XOR" => {
if left.starts_with('x') || right.starts_with('x') {
// Check that first level XOR points to second level XOR, except for first XOR.
if left != "x00" && right != "x00" && !output.contains(&(to, "XOR")) {
swapped.insert(to);
}
} else {
// Second level XOR must point to output.
if !to.starts_with('z') {
swapped.insert(to);
}
}
}
_ => unreachable!(),
}
}

Expand Down

0 comments on commit 177fc32

Please sign in to comment.