Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Advent of Code (in MiniScript), Day 4

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Advent of Code (in MiniScript), Day 4


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Day 4 was the first time I attempted to actually compete, for time, in the Advent of Code contest. I didn't do that well โ€”ย it took me over 6 minutes to complete part A, which gave me a rank of 2181. I did much better on part B, with a total time just over 7 minutes and a rank of 1248. Read on to the end to see some ways I hope to improve in future challenges.

But first, how to tackle this one? Each input line in this challenge consists of two numeric intervals, like "2-4,6-8". For Part A, we need to report whether either span contains the other. My final code looks like this:

f = file.open("day4-input.txt")
total = 0
while true
    line = f.readLine
    if line == null then break // end of file

    parts = line.split(",")
    aFrom = parts[0].split("-")[0].val
    aTo = parts[0].split("-")[1].val
    bFrom = parts[1].split("-")[0].val
    bTo = parts[1].split("-")[1].val

    contained = (bFrom >= aFrom and bTo <= aTo) or
       (aFrom >= bFrom and aTo <= bTo)

    print aFrom + "-" + aTo + " vs " + bFrom + "-" + bTo + ": " + contained
    total = total + contained
end while

print "total: " + total

As on previous days, I was doing this in command-line MiniScript. So to parse out the line, I first split it on a comma, and then split each of those parts on the minus sign, finally applying val to convert those strings to numbers. (This is one place I'll improve โ€” see below.)

Then, checking whether one span contains another is just a matter of carefully comparing those four values. In my rush, I got this wrong, twice. Ouch. That cost me valuable time, and is why part A took so long.

For part B, we only had to count the lines where the two spans overlap. This is considerably easier, and something I was already comfortable with, because it's exactly the same thing you do (for each axis) when checking whether two rectangles overlap. The last three lines within the loop changed to:

    overlap = bFrom <= aTo and bTo >= aFrom

    print aFrom + "-" + aTo + " vs " + bFrom + "-" + bTo + ": " + overlap
    total = total + overlap

...and the rest of the code stayed the same. (I got this one right on the first try, and it took under a minute.)

Future Plans

I'm having a blast solving these challenges with MiniScript, but I'd really like to get into the top 1000. So I need a bit more prewritten code to have my back. So tonight I'm going to do a couple things differently:

  1. I'm going to copy the example data each challenge provides and run my code on that first, comparing my answers to the example answers. This should catch most of the dumb mistakes I might make, and avoid the wasted time of submitting a wrong answer. While it certainly takes a few extra seconds to do, I'm hoping it will be worth it overall.

  2. I'm going to use Mini Micro instead of command-line MiniScript. Mini Micro has an extensive library of generally useful functions in /sys/lib. For example, by importing the stringUtil module, we get a string.match function that would have let me parse the input line out by doing:

    m = line.match("{s1}-{e1},{s2}-{e2}")
    

    and then referencing the values as m.s1, etc.

  3. I'm supplementing the standard libraries with some additional functionality that I hope will be useful. For example, I've written a Span class that represents a numeric interval, and supports all the functions I can think of to do with such things. That includes methods like contains and overlaps, which would have been helpful last night!

I'm sure there will be many nights when I say "Doh! I wish I had generic code that does this!" But hopefully there will be at least a few nights where I say "Aha! I have generic code that does this!" And by leveraging that code, I will be both faster and less likely to make mistakes.

If you want to join me, please do! I'm posting all the code on GitHub, including my little lib/aoc module. I encourage you to join us on Discord too, where we can strategize and commiserate. See you there!

...



๐Ÿ“Œ Advent of Code (in MiniScript), Day 4


๐Ÿ“ˆ 55.48 Punkte

๐Ÿ“Œ Advent, Advent: Hey Santa, hey Santa


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ Aufregung um Tweet vonย Greta Thunberg: Advent, Advent, das Internet brennt


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ Advent, Advent: Jingle Bells, Gaming Elfs


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ โ€žAdvent, Advent, ein Lichtlein brenntโ€œ: Vorweihnachtliche Stimmung fรผr jedes Zuhause


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ Der gute Ton: Advent, Advent, der Adventkranz brennt


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ Lesetipps vom 11.12.2022: Advent, Advent, die Sicherung brennt!


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ Advent, Advent โ€“ kein Lichtlein brennt?


๐Ÿ“ˆ 31.92 Punkte

๐Ÿ“Œ MiniScript Roundup #12


๐Ÿ“ˆ 30.76 Punkte

๐Ÿ“Œ Pratt Parsing in MiniScript


๐Ÿ“ˆ 30.76 Punkte

๐Ÿ“Œ MiniScript Roundup #22


๐Ÿ“ˆ 30.76 Punkte

๐Ÿ“Œ MiniScript on a bare-metal Raspberry Pi


๐Ÿ“ˆ 30.76 Punkte

๐Ÿ“Œ Advent of Code - Day 1


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code - Day 2


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2022 Day 2


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code Day 4


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Day 4 Solutions Advent of Code


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code Day 6


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ HPR3744: Advent of code Day 1 - 4


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Learnings from Advent Of Code Day 1 as a Rust Newbie


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2022 - Day 11


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Calibrating an Elven Trebuchet in Rust: Advent of Code 2023 Day 1


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023 Day 2


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023 Day 2


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023: Day 4 - Scratchcards


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2022 Day 1


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent Of Code Day 1 Solutions


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code Day 2 Solutions


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code Day 3


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023: Day 4 - Scratchcards


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023 Day 6


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023 Day 9


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ HPR4007: Advent of code day 1-5 catchup


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023: Day 11 - Cosmic Expansion


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Advent of Code 2023 Day 16


๐Ÿ“ˆ 24.72 Punkte











matomo