<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RobinFiveWords</title>
    <description>The latest articles on DEV Community by RobinFiveWords (@robinfivewords).</description>
    <link>https://dev.to/robinfivewords</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2084059%2Fb003a633-2d5c-4e4a-be32-c6aaf6b83142.png</url>
      <title>DEV Community: RobinFiveWords</title>
      <link>https://dev.to/robinfivewords</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robinfivewords"/>
    <language>en</language>
    <item>
      <title>PlutoViz: Testing some real things</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Thu, 02 Jan 2025 06:39:19 +0000</pubDate>
      <link>https://dev.to/robinfivewords/plutoviz-testing-some-real-things-4g9o</link>
      <guid>https://dev.to/robinfivewords/plutoviz-testing-some-real-things-4g9o</guid>
      <description>&lt;p&gt;This entire series contains major spoilers for &lt;a href="https://adventofcode.com/2019/day/20" rel="noopener noreferrer"&gt;Advent of Code 2019 day 20&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I reached the point in the &lt;a href="https://docs.manim.community/en/stable/tutorials/building_blocks.html" rel="noopener noreferrer"&gt;Manim tutorials&lt;/a&gt; where I needed LaTeX installed. It took me a few minutes to realize that was the issue and it wasn't simply going to be something I could install via conda, so I moved on. Conveniently, the next example showed how to construct a line from multiple segments, and I created a test spiral of line segments around the origin.&lt;/p&gt;

&lt;p&gt;I had the idea that I'd animate the drawing of these multi-segment lines, and although it took me a while to find it, Manim's &lt;code&gt;Create&lt;/code&gt; function does this. However, for most of my visualization, I should expect to have the entire multi-segment line pop in and out all at once. My solution popped over 21k states from the priority queue, and even if each of those states gets just one frame, at 60 fps it would take six minutes to display them all. I may slowly display the first several states and last several states, but the 21k in the middle will be some kind of blur. Let's say I let the blur last 15 seconds, at 30 fps. Each frame would represent about 50 states. So I'd need to decide how to present 50 states with a single image. Perhaps the "active" / last state would have its entire path highlighted, and the rest would have their last [multi-segment line] step and endpoint highlighted with some low opacity.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>PlutoViz: More prep</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Tue, 31 Dec 2024 06:07:12 +0000</pubDate>
      <link>https://dev.to/robinfivewords/plutoviz-more-prep-pp4</link>
      <guid>https://dev.to/robinfivewords/plutoviz-more-prep-pp4</guid>
      <description>&lt;p&gt;This entire series contains major spoilers for &lt;a href="https://adventofcode.com/2019/day/20" rel="noopener noreferrer"&gt;Advent of Code 2019 day 20&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm leaning toward attempting to animate part 1 in 2D rather than jumping straight to part 2 in 3D. It seems like the wiser approach.&lt;/p&gt;

&lt;p&gt;Tonight I cleaned up my solution script a bit. Instead of importing my 2D point class, I pasted it into the script and removed all the methods I don't use here. I also checked whether any names I used in the global namespace appear in &lt;code&gt;locals().keys()&lt;/code&gt; after &lt;code&gt;from manim import *&lt;/code&gt;, and &lt;code&gt;Point&lt;/code&gt; does, so I changed mine to &lt;code&gt;Point2D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm also starting the Manim &lt;a href="https://docs.manim.community/en/stable/tutorials/index.html" rel="noopener noreferrer"&gt;tutorials&lt;/a&gt;. I'm a tutorial junkie, and as I don't have a background in graphics, it again seems wiser to let the experts show me around first.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>PlutoViz: Introduction</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Mon, 30 Dec 2024 07:19:52 +0000</pubDate>
      <link>https://dev.to/robinfivewords/plutoviz-introduction-31f5</link>
      <guid>https://dev.to/robinfivewords/plutoviz-introduction-31f5</guid>
      <description>&lt;p&gt;"Part 2 seems hella cool. Would love to see this visualized."&lt;/p&gt;

&lt;p&gt;That's what I wrote late at night on November 13, after finishing part 1 of Advent of Code 2019 day 20, and reading part 2.&lt;/p&gt;

&lt;p&gt;This entire series contains major spoilers for &lt;a href="https://adventofcode.com/2019/day/20" rel="noopener noreferrer"&gt;Advent of Code 2019 day 20&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Part 1 is a two-dimensional rectangular maze with a hole in the middle, and several &lt;a href="https://en.wikipedia.org/wiki/Portal_(series)" rel="noopener noreferrer"&gt;portals&lt;/a&gt; that each connect a point on the inside edge with a point on the outside edge. Part 2 reveals that this maze is recursive, and each portal on an inside edge connects with a point on the outside edge of &lt;em&gt;the next level&lt;/em&gt; of the maze.&lt;/p&gt;

&lt;p&gt;I formed a picture in my head of an inverted pyramid, with each inner level positioned below and a little bit smaller than the previous level. After solving the puzzle, I got some ideas about what a visualization of the search for the shortest path could look like. I've wished for a couple years that I could put together visualizations like the ones I've seen in the &lt;a href="https://www.reddit.com/r/adventofcode/" rel="noopener noreferrer"&gt;AoC subreddit&lt;/a&gt;, and I have a strong enough feeling about this puzzle that I think this is where I should start. A recent &lt;a href="https://www.youtube.com/watch?v=rbu7Zu5X1zI" rel="noopener noreferrer"&gt;3Blue1Brown video&lt;/a&gt; gave me the confidence that &lt;a href="https://www.manim.community/" rel="noopener noreferrer"&gt;Manim&lt;/a&gt; would be the right tool, for me, for this first job.&lt;/p&gt;

&lt;p&gt;Here's what I think I'll need to do to make it happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Manim. Conda wanted to downgrade NetworkX in my base environment, so I'm creating a separate environment for Manim. My solution for this puzzle didn't rely on anything beyond the Python standard library, although Manim's dependencies resulted in somewhere close to 200 packages being installed!&lt;/li&gt;
&lt;li&gt;Enhance my solution to track the full path for each candidate. I'm pretty sure the number of branches to consider is small enough that I could assign each new branch a unique integer id and store the branch's full path at that index of a list.&lt;/li&gt;
&lt;li&gt;Enhance my solution to store the line segments for each path between portals within a level. While I'm using breadth-first search to identify the distance to any reachable portal, I'll also need to track the full path. Then in each path I'll need to combine adjacent steps in the same direction into a single segment.&lt;/li&gt;
&lt;li&gt;Create a static 3D image of my solution path for the larger example.&lt;/li&gt;
&lt;li&gt;Animate a new branch — adding to an existing path.&lt;/li&gt;
&lt;li&gt;Animate multiple branches. I imagine only the branch currently in focus will be fully illuminated. Perhaps the branch previously in focus will fade down to some minimum level.&lt;/li&gt;
&lt;li&gt;Tweak the entire visualization to give a sense of the space, movement, search, etc.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Would it be silly to write my own defaultdict?</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Mon, 23 Sep 2024 06:22:03 +0000</pubDate>
      <link>https://dev.to/robinfivewords/would-it-be-silly-to-write-my-own-defaultdict-1n7c</link>
      <guid>https://dev.to/robinfivewords/would-it-be-silly-to-write-my-own-defaultdict-1n7c</guid>
      <description>&lt;p&gt;TL;DR — Yes? Python's &lt;code&gt;defaultdict&lt;/code&gt; is exactly the right tool for the job I call upon it to do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;defaultdict&lt;/code&gt; is &lt;a href="https://docs.python.org/3/library/collections.html" rel="noopener noreferrer"&gt;part of Python's standard library&lt;/a&gt;. Python is already a high-level language, so it seems arbitrary to say I'll allow myself to use some but not all parts of it as given to me.&lt;/p&gt;

&lt;p&gt;I do, however, like when I know everything that something does, and I know it doesn't do anything else. If I'm not mistaken, &lt;code&gt;defaultdict&lt;/code&gt; is 300 lines of C code (&lt;a href="https://github.com/python/cpython/blob/main/Modules/_collectionsmodule.c" rel="noopener noreferrer"&gt;starting at line 2175 here&lt;/a&gt;), and I can't say I understand everything that it does. When I use &lt;code&gt;defaultdict&lt;/code&gt;, almost always I am looping through data, aggregating values, and looking to avoid the &lt;code&gt;KeyError&lt;/code&gt; the first time a key appears in the data (or having to check whether the key exists each time).&lt;/p&gt;

&lt;p&gt;I'm sure millions of people have done this for homework or fun before, but I didn't search for their solutions; I searched for how to make an object "subscriptable", how to add a key to a dict without the subscript syntax (which I'm guessing would infinitely recurse), and how to call the parent class's &lt;code&gt;__init__&lt;/code&gt; method (it had been a while). The following &lt;em&gt;seems&lt;/em&gt; to work fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DefaultDict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value_type&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value_type&lt;/span&gt;&lt;span class="p"&gt;()})&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I probably will never use this, because &lt;code&gt;defaultdict&lt;/code&gt; is already the right tool for the job. It throws an exception immediately when the default factory is not callable or None. Someone has already thought through what type the result should have when taking the union of a &lt;code&gt;dict&lt;/code&gt; and a &lt;code&gt;defaultdict&lt;/code&gt;. It has a nice &lt;code&gt;repr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Where I find writing my own tool to be most useful is if an existing tool is complex in a way that makes it unclear whether it is the right tool to use. Or if an existing tool &lt;a href="https://github.com/RobinFiveWords/chutes-and-ladders/blob/main/chutes_and_ladders.ipynb" rel="noopener noreferrer"&gt;solves a bigger problem than I am trying to solve&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java
&lt;/h3&gt;

&lt;p&gt;I must not have needed a "default HashMap" before, because I just now discovered &lt;code&gt;computeIfAbsent&lt;/code&gt;. While I was thinking it through, though, this one-off code seemed straightforward enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The more concise way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;computeIfAbsent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;())).&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think I'm looking for a way to extend &lt;code&gt;HashMap&lt;/code&gt; so that I can write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;myGet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it will run the previous code. This seems somewhat straightforward...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DefaultMap&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;myGet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;computeIfAbsent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...except I would need to create a separate class for each set of key-value types. There might be a way to do this with generics, but because (if I understand correctly) classes and functions aren't first-class objects in Java, I'm not sure how that would be done. Here's where I really need to search for an answer.&lt;/p&gt;

&lt;p&gt;And of course the top Google result is &lt;a href="https://stackoverflow.com/questions/1786206/is-there-a-java-equivalent-of-pythons-defaultdict" rel="noopener noreferrer"&gt;a link I've visited before&lt;/a&gt;, because of course this isn't the first time I've wondered about it. The &lt;code&gt;.class&lt;/code&gt; property effectively allows the class of the default value, such as &lt;code&gt;ArrayList&lt;/code&gt;, to be passed as a parameter to the constructor. I'm going to have to let this simmer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Choosing data structures for Advent of Code 2018 Day 24</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Thu, 19 Sep 2024 03:59:53 +0000</pubDate>
      <link>https://dev.to/robinfivewords/choosing-data-structures-for-advent-of-code-2018-day-24-1ke</link>
      <guid>https://dev.to/robinfivewords/choosing-data-structures-for-advent-of-code-2018-day-24-1ke</guid>
      <description>&lt;p&gt;How should I store the fighters in a battle with complex interactions?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mild spoilers for Advent of Code 2018 Day 24 follow.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm working my way through &lt;a href="https://adventofcode.com/" rel="noopener noreferrer"&gt;Advent of Code&lt;/a&gt;'s back catalog. &lt;a href="https://adventofcode.com/2018/day/24" rel="noopener noreferrer"&gt;2018 Day 24&lt;/a&gt; looks fun. Well, part 1 looks fun. I'm dreading whatever search part 2 will likely require. Anyway, AoC problems tend to be written in a style that suggests a way to implement. For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Each &lt;strong&gt;fight&lt;/strong&gt; consists of two phases: &lt;strong&gt;target selection&lt;/strong&gt; and &lt;strong&gt;attacking&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From this I could immediately write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fight&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;select_targets&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;attack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code already captures at least two ideas: There will be many fights. Target selection is completed before attacking. Of course, at a minimum this is missing what is fighting, how they will be passed into the fight, and whether the fighters will have their stats updated or be replaced with the results of the fight.&lt;/p&gt;

&lt;p&gt;Each side has an army, and each army contains several groups. I briefly considered representing a group as a tuple (ordered list), but as I would have to unpack it each time to write code I could read, it's more convenient to make it an object with named attributes. So I created a &lt;code&gt;Group&lt;/code&gt; class. I should also create an &lt;code&gt;Army&lt;/code&gt; class, right? Well, that immediately caused me problems. The order in which the groups act does not depend on which army a group is in. If I explicitly created two armies, I would need to maintain a separate list of the initiative order to identify the group up next. Then that group would need to know which army it was in, and its army would need to know the opposing army, and that army would need to know its groups.&lt;/p&gt;

&lt;p&gt;I decided it would be enough for each group to know what army it was in. Any time a group needs to act (on groups of the opposing army), I would consider the full list of groups, and filter out the groups from the same army.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;group2&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;group1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;army&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;group2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;army&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This has the added benefit of preventing a group from targeting itself.&lt;/p&gt;

&lt;p&gt;So I was going to store the groups in a single collection, but what kind of collection? During target selection, I wouldn't know whether a target would be selected at the time I computed its potential damage received. And I needed way to exclude a selected target from later target selection in that fight. I decided I wanted some way to access the right group.&lt;/p&gt;

&lt;p&gt;Each group's &lt;strong&gt;initiative&lt;/strong&gt; value looked to be a useful identifier. I could have just assigned a unique ID to each group, and hopefully I won't have to rework this in part 2, but the initiative values in the test input and my real input appear to be distinct positive integers starting from 1 with no gaps. I thought about just using a list with access by index (position in the list), but aside from having to handle index 0 — or get back into a language with 1-based indexing — relying on a list index to remain unchanged would prevent me from deleting groups that have no units remaining. And I wanted to delete groups with no units so I wouldn't have to check for that in many places.&lt;/p&gt;

&lt;p&gt;I settled on a dictionary, with the initiative as the key and the group itself as the value. This way, any time I wanted to track which groups had already been targeted, or anything else, I could just track the initiative values. This would spare me from having to write code to determine whether two groups were the same. Then when I wanted to retrieve the group itself, I'd get it from the dictionary. Deleting a group, deleting the key-value pair, would have no effect on accessing remaining groups.&lt;/p&gt;

&lt;p&gt;I was still a bit hung up on sorting. Python dicts preserve order starting in 3.6, and at first I thought I wanted to store the groups in initiative order. This isn't always the order in which groups act, though. Effective power is sometimes used, and this changes throughout the battle. What eventually seemed best to me was just sorting the groups as needed, when needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiative&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Turns out part 2 was also fun! A search, of course, but with a neat twist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" width="407" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>computerscience</category>
      <category>adventofcode</category>
    </item>
    <item>
      <title>Getting packages to work when writing Java programs from scratch</title>
      <dc:creator>RobinFiveWords</dc:creator>
      <pubDate>Tue, 17 Sep 2024 05:36:10 +0000</pubDate>
      <link>https://dev.to/robinfivewords/getting-packages-to-work-when-writing-java-programs-from-scratch-ia</link>
      <guid>https://dev.to/robinfivewords/getting-packages-to-work-when-writing-java-programs-from-scratch-ia</guid>
      <description>&lt;p&gt;In early 2023, to get better at reading and understanding Java code, I worked through &lt;a href="https://craftinginterpreters.com/" rel="noopener noreferrer"&gt;Crafting Interpreters&lt;/a&gt; as intended, by typing every line of code. Well, almost every line. In the Java section of the book, each class file starts with this package statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.craftinginterpreters.lox&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I omitted that line and put all the Java source files in a single directory, everything compiled and ran as it should. When I tried to build a folder tree to match the package, it didn't work.&lt;/p&gt;

&lt;p&gt;I'll be getting into Groovy soon, and I feel like I should solve this basic Java problem before I have to start learning about Gradle, Maven, Graven, and all other kinds of avens.&lt;/p&gt;

&lt;p&gt;And...I've got it working now. I created a folder tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src
+-- com
    +-- craftinginterpreters
        +-- lox
            +-- *.java files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I navigated to the &lt;code&gt;src&lt;/code&gt; directory and compiled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;javac com/craftinginterpreters/lox/Lox.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;java j.lox.Lox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What took quite a bit of time to troubleshoot tonight is that I initially copied some but not all of the source files. 😬&lt;/p&gt;

&lt;p&gt;I'm still not sure why I couldn't get it working a year and a half ago. In any case, problem solved!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vzsju9nml5maua7tnkn.png" width="407" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
