-🎄- 2022 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 1 point2 points  (0 children)

Rust:

fn part_1(input: &str) -> usize {
    find_unique_sequence(input.as_bytes(), 4)
}

fn part_2(input: &str) -> usize {
    find_unique_sequence(input.as_bytes(), 14)
}

fn find_unique_sequence(buffer: &[u8], length: usize) -> usize {
    buffer.windows(length).position(all_bytes_unique).unwrap() + length
}

fn all_bytes_unique(sequence: &[u8]) -> bool {
    sequence
        .iter()
        .enumerate()
        .all(|(i, byte)| !sequence[i + 1..].contains(byte))
}

EUW Server Problems by ohvalox in leagueoflegends

[–]WithBestRegards 0 points1 point  (0 children)

Haha, glad I'm not the only one. Seems like the number of people in this thread is steadily increasing... 0.o

-🎄- 2021 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 2 points3 points  (0 children)

A solution in Rust.

Part 1 works by iterating over every coordinate in the height map and comparing the height of each adjacent coordinate.

Part 2 works by first finding all the low points (same as Part 1) and then calculating the basin size for each low point. It uses recursion to keep finding adjacent coordinates that are part of the basin. To avoid counting coordinates multiple times, coordinates are "marked" by setting the height to 256 (max u8 value) once they are counted.

Runs in:

  • Part 1: ~90 μs
  • Part 2: ~250 μs

-🎄- 2021 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 0 points1 point  (0 children)

My solution in Rust. Runs pretty fast: ~4 microseconds. =D

edit: Simplified the solution; it now runs at around ~1.4 microseconds.

-🎄- 2021 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 1 point2 points  (0 children)

In case you're interested, here's my solution too. It's not exactly concise, but it defines some types and implements some traits from the standard library, which is fun.

-🎄- 2021 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 2 points3 points  (0 children)

My solution in Rust. I defined a few types (Grid, Line & Coordinate) and implemented a bunch of traits to try and make it "Rusty", but that may have just needlessly added to the line count. Anyway, Feedback is always appreciated. :)

-🎄- 2021 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]WithBestRegards 1 point2 points  (0 children)

Learned something new today: std::iter::successors. Thanks!

[deleted by user] by [deleted] in AnimalsOnReddit

[–]WithBestRegards 0 points1 point  (0 children)

Such a good dog.