Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2017/day14-disk_defragmentation/Cargo.toml
Normal file
8
2017/day14-disk_defragmentation/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day14-disk_defragmentation"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
67
2017/day14-disk_defragmentation/challenge.txt
Normal file
67
2017/day14-disk_defragmentation/challenge.txt
Normal file
|
@ -0,0 +1,67 @@
|
|||
\--- Day 14: Disk Defragmentation ---
|
||||
----------
|
||||
|
||||
Suddenly, a scheduled job activates the system's [disk defragmenter](https://en.wikipedia.org/wiki/Defragmentation). Were the situation different, you might [sit and watch it for a while](https://www.youtube.com/watch?v=kPv1gQ5Rs8A&t=37), but today, you just don't have that kind of time. It's soaking up valuable system resources that are needed elsewhere, and so the only option is to help it finish its task as soon as possible.
|
||||
|
||||
The disk in question consists of a 128x128 grid; each square of the grid is either *free* or *used*. On this disk, the state of the grid is tracked by the bits in a sequence of [knot hashes](10).
|
||||
|
||||
A total of 128 knot hashes are calculated, each corresponding to a single row in the grid; each hash contains 128 bits which correspond to individual grid squares. Each bit of a hash indicates whether that square is *free* (`0`) or *used* (`1`).
|
||||
|
||||
The hash inputs are a key string (your puzzle input), a dash, and a number from `0` to `127` corresponding to the row. For example, if your key string were `flqrgnkx`, then the first row would be given by the bits of the knot hash of `flqrgnkx-0`, the second row from the bits of the knot hash of `flqrgnkx-1`, and so on until the last row, `flqrgnkx-127`.
|
||||
|
||||
The output of a knot hash is traditionally represented by 32 hexadecimal digits; each of these digits correspond to 4 bits, for a total of `4 * 32 = 128` bits. To convert to bits, turn each hexadecimal digit to its equivalent binary value, high-bit first: `0` becomes `0000`, `1` becomes `0001`, `e` becomes `1110`, `f` becomes `1111`, and so on; a hash that begins with `a0c2017...` in hexadecimal would begin with `10100000110000100000000101110000...` in binary.
|
||||
|
||||
Continuing this process, the *first 8 rows and columns* for key `flqrgnkx` appear as follows, using `#` to denote used squares, and `.` to denote free ones:
|
||||
|
||||
```
|
||||
##.#.#..-->
|
||||
.#.#.#.#
|
||||
....#.#.
|
||||
#.#.##.#
|
||||
.##.#...
|
||||
##..#..#
|
||||
.#...#..
|
||||
##.#.##.-->
|
||||
| |
|
||||
V V
|
||||
|
||||
```
|
||||
|
||||
In this example, `8108` squares are used across the entire 128x128 grid.
|
||||
|
||||
Given your actual key string, *how many squares are used*?
|
||||
|
||||
Your puzzle answer was `8216`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
Now, all the defragmenter needs to know is the number of *regions*. A region is a group of *used* squares that are all *adjacent*, not including diagonals. Every used square is in exactly one region: lone used squares form their own isolated regions, while several adjacent squares all count as a single region.
|
||||
|
||||
In the example above, the following nine regions are visible, each marked with a distinct digit:
|
||||
|
||||
```
|
||||
11.2.3..-->
|
||||
.1.2.3.4
|
||||
....5.6.
|
||||
7.8.55.9
|
||||
.88.5...
|
||||
88..5..8
|
||||
.8...8..
|
||||
88.8.88.-->
|
||||
| |
|
||||
V V
|
||||
|
||||
```
|
||||
|
||||
Of particular interest is the region marked `8`; while it does not appear contiguous in this small view, all of the squares marked `8` are connected when considering the whole 128x128 grid. In total, in this example, `1242` regions are present.
|
||||
|
||||
*How many regions* are present given your key string?
|
||||
|
||||
Your puzzle answer was `1139`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2017).
|
||||
|
||||
Your puzzle input was `nbysizxe`.
|
106
2017/day14-disk_defragmentation/src/lib.rs
Normal file
106
2017/day14-disk_defragmentation/src/lib.rs
Normal file
|
@ -0,0 +1,106 @@
|
|||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
enum Sector { Free, Unvisited, Visited }
|
||||
|
||||
impl Sector {
|
||||
fn arr_from_hex(byte: u8) -> [Self; 8] {
|
||||
[
|
||||
if byte & 0b10000000 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b01000000 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00100000 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00010000 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00001000 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00000100 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00000010 == 0 { Self::Free } else { Self::Unvisited },
|
||||
if byte & 0b00000001 == 0 { Self::Free } else { Self::Unvisited },
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let mut disk: Vec<Vec<Sector>> = (0..128)
|
||||
.map(|row| knot_hash(&format!("{input}-{row}"), 255)
|
||||
.into_iter()
|
||||
.flat_map(Sector::arr_from_hex)
|
||||
.collect::<Vec<Sector>>())
|
||||
.collect();
|
||||
let mut regions = 0;
|
||||
(0..128).for_each(|row| {
|
||||
(0..128).for_each(|col| {
|
||||
if disk[row][col] == Sector::Unvisited {
|
||||
regions += 1;
|
||||
mark_visited(&mut disk, (row, col));
|
||||
}
|
||||
});
|
||||
});
|
||||
let sectors = disk.into_iter().flatten().filter(|s| s == &Sector::Visited).count();
|
||||
(sectors, regions)
|
||||
}
|
||||
|
||||
fn mark_visited(disk: &mut [Vec<Sector>], (row, col): (usize, usize)) {
|
||||
disk[row][col] = Sector::Visited;
|
||||
let mut new_last_step = Vec::from([(row, col)]);
|
||||
while !new_last_step.is_empty() {
|
||||
let mut new_this_step = Vec::new();
|
||||
for (row, col) in &new_last_step {
|
||||
for next in [
|
||||
(row.checked_sub(1), Some(*col)),
|
||||
(Some(*row+1), Some(*col)),
|
||||
(Some(*row), col.checked_sub(1)),
|
||||
(Some(*row), Some(*col+1))
|
||||
] {
|
||||
match next {
|
||||
(Some(y), Some(x)) if y < 128 && x < 128 => {
|
||||
if disk[y][x] == Sector::Unvisited {
|
||||
disk[y][x] = Sector::Visited;
|
||||
new_this_step.push((y, x));
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
std::mem::swap(&mut new_this_step, &mut new_last_step);
|
||||
}
|
||||
}
|
||||
|
||||
fn knot_hash(input: &str, max: u8) -> Vec<u8> {
|
||||
let mut list: Vec<u8> = (0..=max).collect();
|
||||
let mut current_position = 0;
|
||||
let mut skip_size = 0;
|
||||
|
||||
let ascii_elements: Vec<u8> = input.bytes().chain([17, 31, 73, 47, 23].into_iter()).collect();
|
||||
for _ in 0..64 {
|
||||
twist(&mut list, &ascii_elements, &mut current_position, &mut skip_size);
|
||||
}
|
||||
list.chunks(16)
|
||||
.map(|chunk| chunk.iter().cloned().reduce(|acc, i| acc ^ i).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn twist(list: &mut [u8], elements: &[u8], current_position: &mut usize, skip_size: &mut usize) {
|
||||
let max = list.len();
|
||||
elements.iter().for_each(|length| {
|
||||
let new_sub_list: Vec<u8> = (0..*length).rev().map(|i| list[(*current_position+i as usize) % max]).collect();
|
||||
new_sub_list.iter().enumerate().for_each(|(idx, new_elem)| list[(*current_position + idx) % max] = *new_elem);
|
||||
*current_position += *length as usize + *skip_size;
|
||||
*current_position %= max;
|
||||
*skip_size += 1;
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = "flqrgnkx";
|
||||
assert_eq!(run(sample_input), (8108, 1242));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = "nbysizxe";
|
||||
assert_eq!(run(challenge_input), (8216, 1139));
|
||||
}
|
||||
}
|
0
2017/day14-disk_defragmentation/tests/challenge_input
Normal file
0
2017/day14-disk_defragmentation/tests/challenge_input
Normal file
0
2017/day14-disk_defragmentation/tests/sample_input
Normal file
0
2017/day14-disk_defragmentation/tests/sample_input
Normal file
Loading…
Add table
Add a link
Reference in a new issue