Add files via upload
This commit is contained in:
parent
a2d090645d
commit
de20842ac7
@ -118,7 +118,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 1.1: .0003 seconds, answer 1830467 ok"
|
"Puzzle 1.1: .0002 seconds, answer 1830467 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
@ -312,7 +312,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 2.2: .0081 seconds, answer 328 ok"
|
"Puzzle 2.2: .0075 seconds, answer 328 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 9,
|
"execution_count": 9,
|
||||||
@ -400,7 +400,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 3.1: .0018 seconds, answer 156388521 ok"
|
"Puzzle 3.1: .0013 seconds, answer 156388521 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 12,
|
"execution_count": 12,
|
||||||
@ -539,7 +539,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 4.1: .0785 seconds, answer 2401 ok"
|
"Puzzle 4.1: .0737 seconds, answer 2401 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 17,
|
"execution_count": 17,
|
||||||
@ -577,7 +577,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 4.2: .0669 seconds, answer 1822 ok"
|
"Puzzle 4.2: .0629 seconds, answer 1822 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 18,
|
"execution_count": 18,
|
||||||
@ -708,7 +708,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 5.1: .0015 seconds, answer 5762 ok"
|
"Puzzle 5.1: .0010 seconds, answer 5762 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 21,
|
"execution_count": 21,
|
||||||
@ -793,7 +793,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 5.2: .0017 seconds, answer 4130 ok"
|
"Puzzle 5.2: .0020 seconds, answer 4130 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 24,
|
"execution_count": 24,
|
||||||
@ -875,7 +875,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 6.1: .0046 seconds, answer 5329 ok"
|
"Puzzle 6.1: .0045 seconds, answer 5329 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 26,
|
"execution_count": 26,
|
||||||
@ -957,7 +957,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 6.2: 14.9723 seconds, answer 2162 ok"
|
"Puzzle 6.2: 14.1021 seconds, answer 2162 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 28,
|
"execution_count": 28,
|
||||||
@ -1095,7 +1095,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 7.1: .0483 seconds, answer 1985268524462 ok"
|
"Puzzle 7.1: .0518 seconds, answer 1985268524462 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 32,
|
"execution_count": 32,
|
||||||
@ -1127,7 +1127,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 7.2: 5.5535 seconds, answer 150077710195188 ok"
|
"Puzzle 7.2: 6.1540 seconds, answer 150077710195188 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 33,
|
"execution_count": 33,
|
||||||
@ -1202,7 +1202,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 8.1: .0007 seconds, answer 220 ok"
|
"Puzzle 8.1: .0014 seconds, answer 220 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 35,
|
"execution_count": 35,
|
||||||
@ -1283,7 +1283,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 8.2: .0022 seconds, answer 813 ok"
|
"Puzzle 8.2: .0029 seconds, answer 813 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 37,
|
"execution_count": 37,
|
||||||
@ -1311,7 +1311,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# [Day 9](https://adventofcode.com/2024/day/9): Disk Fragmenter\n",
|
"# [Day 9](https://adventofcode.com/2024/day/9): Disk Fragmenter\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Today we're confronted with a computer disk that needs to be compacted to gain some contiguous free space. The contents of the disk is represented in the **disk map** format: a string of digits, where the digits alternate between the number of blocks of a file, followed by the number of blocks of free space. We'll parse that as a tuple of digits:"
|
"Today we're confronted with a computer disk that needs to be compressed to gain some contiguous free space. The contents of the disk is represented in the **disk map** format: a string of digits, where the digits alternate between the number of blocks of a file, followed by the number of blocks of free space. We'll parse that as a tuple of digits:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1344,13 +1344,15 @@
|
|||||||
"id": "99d40379-65e1-4872-8c68-17ba4925c24e",
|
"id": "99d40379-65e1-4872-8c68-17ba4925c24e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Part 1: Compact the hard drive. What is the resulting filesystem checksum? \n",
|
"<img src=\"https://pbs.twimg.com/media/GeYt7iuWIAAHxhT?format=jpg&name=medium\" width=400>\n",
|
||||||
|
"\n",
|
||||||
|
"### Part 1: Compress the hard drive. What is the resulting filesystem checksum? \n",
|
||||||
"\n",
|
"\n",
|
||||||
"The disk map \"`12345`\" means that there is 1 block for the first file (which has ID number 0), followed by 2 empty blocks, then 3 blocks for the second file (with ID number 1), followed by 4 empty blocks, and finally 5 blocks for the third file (with ID number 2). It makes sense to convert this into a **disk layout** format, which would be \"`0..111....22222`\", where \"`.`\" represents an empty block.\n",
|
"The disk map \"`12345`\" means that there is 1 block for the first file (which has ID number 0), followed by 2 empty blocks, then 3 blocks for the second file (with ID number 1), followed by 4 empty blocks, and finally 5 blocks for the third file (with ID number 2). It makes sense to convert this into a **disk layout** format, which would be \"`0..111....22222`\", where \"`.`\" represents an empty block.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"To **compact** a disk layout, move file blocks one at a time starting by taking the rightmost non-empty block and moving it to the leftmost empty position; repeat until no more moves are possible.\n",
|
"To **compress** a disk layout, move file blocks one at a time starting by taking the rightmost non-empty block and moving it to the leftmost empty position; repeat until no more moves are possible.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The final answer is a **checksum** of the compacted disk: the sum of the product of the block position times the file ID number for all non-empty blocks."
|
"The final answer is a **checksum** of the compressed disk: the sum of the product of the block position times the file ID number for all non-empty blocks."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1371,7 +1373,7 @@
|
|||||||
" layout.extend(disk_map[i + 1] * [empty])\n",
|
" layout.extend(disk_map[i + 1] * [empty])\n",
|
||||||
" return layout\n",
|
" return layout\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def compact_layout(layout: list) -> list:\n",
|
"def compress_layout(layout: list) -> list:\n",
|
||||||
" \"\"\"Mutate layout by moving blocks one at a time from the end to the leftmost free space.\"\"\"\n",
|
" \"\"\"Mutate layout by moving blocks one at a time from the end to the leftmost free space.\"\"\"\n",
|
||||||
" N = len(layout)\n",
|
" N = len(layout)\n",
|
||||||
" free = -1 # Start looking for free space from the left\n",
|
" free = -1 # Start looking for free space from the left\n",
|
||||||
@ -1397,7 +1399,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 9.1: .0430 seconds, answer 6332189866718 ok"
|
"Puzzle 9.1: .0520 seconds, answer 6332189866718 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 40,
|
"execution_count": 40,
|
||||||
@ -1407,7 +1409,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"answer(9.1, 6332189866718, lambda:\n",
|
"answer(9.1, 6332189866718, lambda:\n",
|
||||||
" checksum(compact_layout(disk_layout(disk_map))))"
|
" checksum(compress_layout(disk_layout(disk_map))))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1415,9 +1417,9 @@
|
|||||||
"id": "2c05a497-cc66-4698-b88b-25c33eea224a",
|
"id": "2c05a497-cc66-4698-b88b-25c33eea224a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Part 2: Compact the hard drive with the new method. What is the resulting filesystem checksum? \n",
|
"### Part 2: Compress the hard drive with the new method. What is the resulting filesystem checksum? \n",
|
||||||
"\n",
|
"\n",
|
||||||
"In Part 2, there is a new method of compacting the disk, where we move full files rather than a block at a time. Again we start on the right, and try to move a file to the leftmost position where it will fit. If there is no such position, the file doesn't move. `compact_layout2` implements this new method, performing a move by swapping two [**slices**](https://docs.python.org/3/library/functions.html#slice) of the disk layout: \n",
|
"In Part 2, there is a new method of compressing the disk, where we move full files rather than a block at a time. Again we start on the right, and try to move a file to the leftmost position where it will fit. If there is no such position, the file doesn't move. `compress_layout2` implements this new method, performing a move by swapping two [**slices**](https://docs.python.org/3/library/functions.html#slice) of the disk layout: \n",
|
||||||
"\n",
|
"\n",
|
||||||
" layout[file], layout[free] = layout[free], layout[file]`\n",
|
" layout[file], layout[free] = layout[free], layout[file]`\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -1433,7 +1435,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def compact_layout2(disk_map: Ints) -> list:\n",
|
"def compress_layout2(disk_map: Ints) -> list:\n",
|
||||||
" \"\"\"Mutate layout by moving files one at a time from the end to the leftmost free space.\"\"\"\n",
|
" \"\"\"Mutate layout by moving files one at a time from the end to the leftmost free space.\"\"\"\n",
|
||||||
" layout = disk_layout(disk_map)\n",
|
" layout = disk_layout(disk_map)\n",
|
||||||
" for file in file_slices(disk_map):\n",
|
" for file in file_slices(disk_map):\n",
|
||||||
@ -1478,7 +1480,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Puzzle 9.2: 6.5013 seconds, answer 6353648390778 ok"
|
"Puzzle 9.2: 6.7720 seconds, answer 6353648390778 ok"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 42,
|
"execution_count": 42,
|
||||||
@ -1488,7 +1490,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"answer(9.2, 6353648390778, lambda:\n",
|
"answer(9.2, 6353648390778, lambda:\n",
|
||||||
" checksum(compact_layout2(disk_map)))"
|
" checksum(compress_layout2(disk_map)))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1519,24 +1521,24 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Puzzle 1.1: .0003 seconds, answer 1830467 ok\n",
|
"Puzzle 1.1: .0002 seconds, answer 1830467 ok\n",
|
||||||
"Puzzle 1.2: .0002 seconds, answer 26674158 ok\n",
|
"Puzzle 1.2: .0002 seconds, answer 26674158 ok\n",
|
||||||
"Puzzle 2.1: .0013 seconds, answer 257 ok\n",
|
"Puzzle 2.1: .0013 seconds, answer 257 ok\n",
|
||||||
"Puzzle 2.2: .0081 seconds, answer 328 ok\n",
|
"Puzzle 2.2: .0075 seconds, answer 328 ok\n",
|
||||||
"Puzzle 3.1: .0018 seconds, answer 156388521 ok\n",
|
"Puzzle 3.1: .0013 seconds, answer 156388521 ok\n",
|
||||||
"Puzzle 3.2: .0009 seconds, answer 75920122 ok\n",
|
"Puzzle 3.2: .0009 seconds, answer 75920122 ok\n",
|
||||||
"Puzzle 4.1: .0785 seconds, answer 2401 ok\n",
|
"Puzzle 4.1: .0737 seconds, answer 2401 ok\n",
|
||||||
"Puzzle 4.2: .0669 seconds, answer 1822 ok\n",
|
"Puzzle 4.2: .0629 seconds, answer 1822 ok\n",
|
||||||
"Puzzle 5.1: .0015 seconds, answer 5762 ok\n",
|
"Puzzle 5.1: .0010 seconds, answer 5762 ok\n",
|
||||||
"Puzzle 5.2: .0017 seconds, answer 4130 ok\n",
|
"Puzzle 5.2: .0020 seconds, answer 4130 ok\n",
|
||||||
"Puzzle 6.1: .0046 seconds, answer 5329 ok\n",
|
"Puzzle 6.1: .0045 seconds, answer 5329 ok\n",
|
||||||
"Puzzle 6.2: 14.9723 seconds, answer 2162 ok\n",
|
"Puzzle 6.2: 14.1021 seconds, answer 2162 ok\n",
|
||||||
"Puzzle 7.1: .0483 seconds, answer 1985268524462 ok\n",
|
"Puzzle 7.1: .0518 seconds, answer 1985268524462 ok\n",
|
||||||
"Puzzle 7.2: 5.5535 seconds, answer 150077710195188 ok\n",
|
"Puzzle 7.2: 6.1540 seconds, answer 150077710195188 ok\n",
|
||||||
"Puzzle 8.1: .0007 seconds, answer 220 ok\n",
|
"Puzzle 8.1: .0014 seconds, answer 220 ok\n",
|
||||||
"Puzzle 8.2: .0022 seconds, answer 813 ok\n",
|
"Puzzle 8.2: .0029 seconds, answer 813 ok\n",
|
||||||
"Puzzle 9.1: .0430 seconds, answer 6332189866718 ok\n",
|
"Puzzle 9.1: .0520 seconds, answer 6332189866718 ok\n",
|
||||||
"Puzzle 9.2: 6.5013 seconds, answer 6353648390778 ok\n"
|
"Puzzle 9.2: 6.7720 seconds, answer 6353648390778 ok\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user