diff options
1 files changed, 16 insertions, 18 deletions
| -rw-r--r-- | src/blog/when-a-language-forces-simpler-thinking/index.du | 34 |
diff --git a/src/blog/when-a-language-forces-simpler-thinking/index.du b/src/blog/when-a-language-forces-simpler-thinking/index.du index fcf3c7e..c99ebd0 100644 --- a/src/blog/when-a-language-forces-simpler-thinking/index.du +++ b/src/blog/when-a-language-forces-simpler-thinking/index.du @@ -1,15 +1,15 @@ -# When a language forces simpler thinking +'def title When a language forces simpler thinking +'def desc An interesting situation where changing how you think results in a better solutions than trying to adapt your original idea -'define expected-reading 5 min -'define created 13 December 2022 -'define edited 13 December 2022 -[$pagenav] +# {title} -This year I decided to join the [url https://adventofcode.com/2022 2022 Advent of Code] event. -The exercise [url https://adventofcode.com/2022/day/7 on the 7th day] involved storage of a folder structure: each folder would have a name, files, and other folders in itself. +{pagenav 5 min;13 December 2022;13 December 2022} + +This year I decided to join the {url 2022 Advent of Code;https://adventofcode.com/2022} event. +The exercise {url on the 7th day;https://adventofcode.com/2022/day/7} involved storage of a folder structure: each folder would have a name, files, and other folders in itself. For the task itself, you don't actually need to store the files, just the sum of their (and of those in subfolders) size, so a folder can be represented as a name, a number and multiple other folders. -My solutions are written purely in [url https://en.wikipedia.org/wiki/Common_Lisp#Syntax Common Lisp]: a programming language where everything is represented as a list of values. A value might be another list or a non-list value, like a number. +My solutions are written purely in {url Common Lisp;https://en.wikipedia.org/wiki/Common_Lisp#Syntax}: a programming language where everything is represented as a list of values. A value might be another list or a non-list value, like a number. So I thought I could represent each folder as a list, where the first item is it's name, second is file size sum and the rest are subfolders, something like this: ``` @@ -21,7 +21,7 @@ Name File size sum Subfolder "e" in "a" | | Subfolder "a" in "/" Subfolder "d" in "/" -Now, after parsing a file, you'll need to update the file size sum in the [=current=] folder [=and=] in the [=parent=] folder. +Now, after parsing a file, you'll need to update the file size sum in the {b current} folder {b and} in the {b parent} folder. To do such a thing, you would either need to .bulleted @@ -49,16 +49,14 @@ Name | File size sum | | | At index 0 is parent "/" Parent is "a" Parent is "/" -'footnote day7 A potential improvement would be to use CLOS to store each folder as it's own object, but this worked fine and I really wanted to spare reading on CLOS at that time. - -Now this solution worked great! With this I was able to [url https://gitlab.com/Syndamia/advent-of-code/-/tree/main/2022/Day07 solve] both tasks easily.[$day7] +Now this solution worked great! With this I was able to {url solve;https://gitlab.com/Syndamia/advent-of-code/-/tree/main/2022/Day07} both tasks easily.{fn A potential improvement would be to use CLOS to store each folder as it's own object, but this worked fine and I really wanted to spare reading on CLOS at that time.} #### Appendix In the exercise, you have commands for navigating between folders. -Going up a folder is trivial, but folders can have duplicate names if they're inside different parent folders, so going to a sub folder requires to search by it's name [*and*] parent index. +Going up a folder is trivial, but folders can have duplicate names if they're inside different parent folders, so going to a sub folder requires to search by it's name {i and} parent index. -Surprisingly enough, in my solution [url https://gitlab.com/Syndamia/advent-of-code/-/tree/main/2022/Day07/part-one.cl#L15-17 (lines 15-17)], I just search from the end to the beginning of all parsed folders for a name. +Surprisingly enough, in my solution {url (lines 15-17);https://gitlab.com/Syndamia/advent-of-code/-/tree/main/2022/Day07/part-one.cl#L15-17}, I just search from the end to the beginning of all parsed folders for a name. This works fine for this exact task and exact inputs, however in general this isn't correct. In a structure like this: ``` @@ -92,8 +90,8 @@ $ cd .. $ cd .. $ cd a -'footnote mkfile [`mkfile`] is like [`touch`], but you could specify a size and the file will be filled with that many zeroes. It seems to exist solely in [url https://docs.oracle.com/cd/E26502_01/html/E29031/mkfile-1m.html Solaris] (as a system administration command) and [url https://ss64.com/osx/mkfile.html MacOS]. I mentioned it and not [`touch`], since the latter only creates empty files, which wouldn't contribute to the total file size sum. - -The last [`\$ cd a`] actually puts you into [`/b/a/`], not [`/a/`]. +The last {` \$ cd a} actually puts you into {` /b/a/}, not {` /a/}. The main thing that saves us (me) is that you can only change directory and list structure (in current directory), so you would only ever be visiting a folder once. -If a folder's contents were [`ls`]'d more than once, or they could change (for example having an [`mkfile`] [$mkfile] command), a proper search would be required. +If a folder's contents were {` ls}'d more than once, or they could change (for example having an {` mkfile} {fn {` mkfile} is like {` touch}, but you could specify a size and the file will be filled with that many zeroes. It seems to exist solely in {url Solaris;https://docs.oracle.com/cd/E26502_01/html/E29031/mkfile-1m.html} (as a system administration command) and {url MacOS;https://ss64.com/osx/mkfile.html}. I mentioned it and not {` touch}, since the latter only creates empty files, which wouldn't contribute to the total file size sum.} command), a proper search would be required. + +{footnotes} |