Posts Advent of Code 2019
Post
Cancel

Advent of Code 2019

Series Navigation

  • Advent of Code 2019
  • Advent of Code 2020
  • Advent of Code 2021
  • Advent of Code 2022

  • A collection of Mathematica programs and rhymed poetry written for Advent of Code 2019. Highlights include running red lights, a sonnet of off-by-one errors, and fleas with little fleas to bite 'em.

    Mathematica

    This repository stores my Advent Of Code solutions for 2019, written in Mathematica. I used .m files this year, so they’re not going to be too readable unless you have a Mathematica instance to read them with - webMathematica is available for free if you are interested but don’t have the IDE. And as there’s a poetry competition over on the Advent of Code subreddit this year, I’ll be writing a poem for each problem each day and adding them here.

    Days Completed

    Click on any of the day #s to see the problem for that day, and on any of the titles for my code.

    Poems

    Day 1: Achilles, Zeno, Newton, Tsiolkovsky

    In ancient Greece, in olden times,
    The gods once held a race:
    Achilles, swiftest of all men,
    Against a turtle’s pace.

    Achilles, turning towards the beast,
    said “I’ll give you a start.
    I’ll stay here at the starting line,
    ‘Til we’re two stades apart.”

    The turtle, slowly, turned his head.
    “My friend, you’ve lost the bet.
    The gods are laughing up on high.
    And you don’t know it yet.”

    The line was drawn, the flag was dropped,
    And off the turtle went.
    A day went by, Achilles woke,
    And raced out from his tent.

    Two stades he ran, and saw the beast
    A mere ten cubits yonder.
    And at that speed he crossed the gap,
    Before his mind could wander.

    He saw the turtle still ahead,
    Though now, by just two πόσιν.
    (The author, here, regretted Greek,
    and wished ‘haiku’ he’d chosen.)

    This race continued, on and on,
    the chasm ever waning.
    Achilles couldn’t close the gap,
    but ran on, ever straining.

    Then from on high a voice boomed out,
    and all around was laughter.
    The great god Zeno entered in,
    Two others walking after.

    “King Achilles, you have lost.
    The turtle is more sprightly.
    For when you get to where he was,
    He’s traveled further, slightly!

    If all these gaps are added on,
    The terms go on forever.
    You race against infinity:
    A Sisyphean endeavour.”

    But Newton, with him, intervened.
    “These sums are not so motile.
    You can add up an endless sum,
    And get a finite total.”

    Achilles, gleeful, ran again,
    and left the turtle trailing.
    In glee, he jumped and tried to fly,
    but each attempt was failing.

    And when the Earth had pulled him back,
    he turned and asked a question.
    “My lords, if men should wish to fly,
    Have you some good suggestion?”

    And then Tsiolkovsky rose and spoke,
    the last among the Powers.
    “Lord Newton gave you finities,
    but mercy won’t be ours!”

    “For logarithms are my crown,
    and gravity my scepter.
    No ship of yours, dear King, will fly
    Unless my laws accept her.”

    And to this day, he reigns supreme.
    His cursed equation rankles.
    Whene’er we reach above, we fly
    with logs tied to our ankles.

    Day 2: A Sonnet of Smoke

    If you wish to care for an old machine,
    No matter if PC, Red Hat, or Mac,
    There is a part which you must not demean:
    A genie that, when freed, will not come back.

    When servers say to “Halt” and then “Catch Fire”,
    The fire lets the smoke out through the fans.
    This magic smoke, it ran your system, prior.
    And cannot be replaced by mortal hands.

    If e’er a man should hear this and object,
    If he cries ‘superstition!’ or ‘That’s wrong!’,
    Do not trust him, or leave his code unchecked;
    For he will not posses his smoke for long.

    And if that man approaches, bar the door!
    If he lets loose your smoke, there is no more.

    Day 3: Maze in Manhattan

    In New York, if you decide to
    Walk through blindly, only tied to
    Ariadne’s yarn to guide you,
    You won’t lose your way.

    But if you should cross a wire,
    Left by someone else entire,
    “How might I” you might inquire,
    “Go back there someday?”

    Notch your yarn out once a meter;
    Keep a compass and repeater,
    So you’re ready, when you meet her,
    To deduce the place,

    Where (part 1) your home was nearest,
    Where (part 2) your signal clearest,
    Where your yarn that you held dearest
    Crossed another’s space.

    Day 4: I’d Like To Thank The Academy

    I got third place on part 1 and first place on part 2 of today’s problem. This was due to exceptionally specific circumstances about the problem and will never happen again, so I’m making the most of it.

    You had a dozen problems which
    Were practically a spoiler.
    Your ‘repunits’ I used to curse.
    ‘Repeated digit’ problems? Worse.
    But they all helped me to rehearse,
    So thank you, Project Euler.

    You wrote up Mathematica,
    Which later you called ‘Wolfram’.
    Your ego might be vast as time,
    ‘No open source’ might feel like slime,
    Your name might be real tough to rhyme,
    But thank you, Stephen Wolfram.

    You wrote the puzzle for today
    (And took my bribe, so great!)
    For Perl and Wolfram must unite
    Against the all-consuming blight,
    Since Python wins most every night,
    So thank you, Topaz…8.

    Day 5: Intcode vs. Sleep

    It’s far too late to write a proper verse;
    My coding skills today were very slow.
    And while that’s bad, what might be even worse:
    There are still ninety opcodes left to go!

    Day 6: With Apologies to Joyce Kilmer

    I think that I shall never see
    A poem named quite like a tree.

    A tree whose ‘root’ is overhead,
    A tree whose height is ‘depth’ instead.

    Whose ‘branches’ go down fathoms deep;
    Whose ‘leaves’ support the hefty ‘heap’.

    A ‘family’ tree, with ‘children’ who
    Have just one ‘parent’ each, not two.

    A tree that’s just a type a graph;
    A ‘forest’ when it’s split in half.

    Poems are named by fools like me…
    A bigger fool must name a tree.

    Day 7: So You Want To Make A Feedback Loop

    To get maximum thrust from your thruster,
    You’ll need all that five Intcodes can muster.
    Link the first to the last;
    When the halt code has passed
    You can get your result from the cluster.

    Day 8: A Sonnet of Sojourning

    The crimson world that roams around our sun
    Has myriads of regions to explore.
    The places where once water might have run
    The places where spirits might one day soar.

    But we, dependent on our verdant home,
    Have not the opportunity to tread
    Upon that distant dirt of sand and loam,
    And so we send out vikings in our stead.

    So what befell those brave explorers there
    Sojourning all alone, so sure to die?
    Where once they trekked through Martian dust and air
    They are entombed beneath the Martian sky.

    And for those valiant pathfinders who fell,
    Their crimson tomb is Heaven and not Hell.

    Day 9: Mission Accomplished

    A 53-bit integer
    Can now be handled fine.
    We have have a base that’s relative
    And we can run a quine.

    We can make threads in parallel
    And loop them without fuss.
    References outside the code?
    They pose no threat to us.

    Ten optcodes and three modes, to boot -
    We’ve realized every one.
    We’ve done four Intcode problems now…

    And now, I’m sure, we’re done.

    Day 10: The Hunting of the Asteroids

    The wild, roaming asteroids,
    Are quite the fearsome prey.
    They’re known to hide inside a pack
    To sneak up first, and then attack,
    The instant you have turned your back,
    Or let your thoughts astray.

    To try to count the asteroids
    Is only fit for fools.
    They’ll sit inside your line of sight
    No matter where you think you might
    Place viewing stands to see their flight,
    Or try to learn their rules.

    The hunting of the asteroids
    Is very seldom taught:
    No need to tell ‘twixt friend and foe,
    No need to spare a fawn or doe:
    With laser beams, you rotate slow
    And vaporize the lot.

    Day 11: Thin Blueshifted Line

    We all know that dread feeling when
    The siren comes to view.
    But I, a foolish man back then
    Thought I knew what to do.

    “Good morning, sir” he said to me,
    “I’ll need your card and name.
    You ran a red light just back there;
    This ticket’s for the same.”

    “But officer,” I tried to say,
    “It wasn’t red for me!
    It must have blueshifted to green:
    It’s all Lorentz, you see!”

    The officer of space then thought,
    And worked out what I’d said.
    “I’ll let you off the hook, this time.
    For going on a red.

    But there’s another ticket now,
    And bigger than before.
    You traveled at eighteen percent
    Of lightspeed, maybe more!”

    The moral: don’t irk SP
    If you have any sense,
    And don’t attempt to bluff them out:
    They all know their Lorentz.

    Day 12: Symmetry

    They dance the music of the spheres
    As epochs pass away.
    And when the tune repeats itself
    So do their dance, and they.

    Their chorus has no earthly sound;
    We have but earthly ears.
    We only hear an echo of
    The music of the spheres.

    Day 13: Keeping Busy

    When you’ve rigged up your joystick for blocking,
    (And kept all your outputs from locking),
    You can render the game
    And get scores in the same,
    And all ‘cause you don’t feel like walking.

    Day 14: One Thing Leads To Another

    Part 0: Examples

    You start with ORE
    And make some A,
    Since that’s what the directions say.

    You make some B
    And then some C
    It’s all as simple as can be,

    You make some D
    and then some E
    And lo! You’ve filled your tanks for free!

    You’ve cooked a bit
    and had your fun,
    And to make more
    Just add some ORE.

    Part 1: Recursion

    You know the FUEL you need to make,
    But don’t know how much ORE to take.

    To make FUEL you need 10 XY
    And 7 Z and 15 I

    And to make Z you need MN,
    And that requires I again,

    And down and down the hole you go
    But there’s at least a floor below:

    If all that’s in the stack is ORE
    You’ve landed safely on that floor.

    Part 2: Insanity

    The depth of my recursion now knows nary an upper bound;
    The math to make a trillion FUEL will eat a coder whole.
    Naively, I have multiplied, but all that I have found
    Are Ceiling[]s placed upon my code, that trap only my soul.

    A day, a night, and I remain, in torment, at my screen,
    A dozen drafts I’ve thrown aside, and hope, and sunlight too.
    To sort it by topology, the best chance I have seen
    To leave this labyrinth of code, this maze of white and blue.

    I make the list into a graph, and careful do I tread,
    I walk and hope that each new part needs but what came before.
    The shield that is recursion, gone, I walk and shake with dread
    Until, at last, I reemerge, and know the FUEL and ORE.

    Part 3: Triumph

    If I should go out to the stars,
    And need some FUEL to make it,
    And if I’ll want to calculate
    The ORE, and not mistake it…

    I’ll take around
    The upper bound,
    And then take more,
    Just to be sure.

    Day 15: Not Simple? What Ever.

    New nifty knack? Not needed now:
    See some sides subtracted,
    Winding wainscotting withdraws;
    Emptiness enacted.

    Day 16: The Trick™ (With Apologies to Gilbert and Sullivan)

    When you’re writing some code in your humble abode, after getting one star with no trouble,
    Then its sequel might seem to be on the same theme, and require the same code but double.
    But you’ll quickly be scared (what with order n-squared) of the ten-thousand-fold repetition,
    And you know your brute force, though compiled (of course), will still lose in a war of attrition.
    For The Trick™ for part two has just nothing to do with the star that you’d gotten just previous,
    And though coding’s a breeze, with The Trick™ up your sleeves, if bereft, you’ll just have to get devious:

    From the last digit first, do a cumsum reversed,
    then reverse it all back and add 1 to the stack,
    and make sure your offset hasn’t drifted as yet
    and then do it again till the Mod[]s hurt your brain
    and go back and unplug when your-off-by-one bug
    wrecks your program until you’ve just lost all the thrill
    and you’ve fallen behind with about half a mind
    to just quit and go drowning your sorrow,

    But the program is done, and the second star’s won!
    And the poem’s complete, and the challenge is beat

    …right until we get Intcode tomorrow!

    Day 17: ABABCCBCBA

    A flare now billows outward from the sun’s unceasing glare.
    It menaces the ship with its immense electric field.
    And scaffolding outside the ship, and bots all stationed there
    Would fry if they remained in place, the wrong side of the shield.

    Your tools: an ASCII camera, a vaccuum bot for dust,
    Schematics of the scaffolding. Not much, but try you must.
    First, you need your bearings: when the junctions are revealed
    You will know just where your vacuum bot can put its wheels and trust.

    Map all the turns of scaffolding, and ZIP them tightly sealed,
    Then, map compressed, send out the bot, with not a tick to spare.

    Day 18: The Key

    Padlocked doors
    Are all you see.
    And every letter,
    Has a key.

    To travel first
    from a to b
    Would take too long
    For every key.

    Code up a state
    With constants three:
    {x,y}, the length,
    And traversed key.

    Make a cache
    To some degree:
    The distances
    ‘Twixt every key.

    And all the doors
    from A to Z?
    Note them if they
    Block a key.

    Combine the states
    Which both agree
    On all but length;
    Repeat per key.

    To beat the maze
    And set you free,
    Use Dijkstra.
    That’s the key.

    Day 19: Off By One

    How can a man learn fully and correct
    Against the anger that frustration brings him?
    When after algorithms had been checked
    An error, off-by-one, lurks out and stings him?

    The disconnect between “it is” and “ought”
    May be well be infinite for a machine.
    The man must know his code’s not what he thought;
    He cannot simply say “Do what I mean”.

    A fitting line, then, from an ancient ode,
    And recollected wrong, from slumbering:
    “The fault, dear Brutus, is not in our code,
    But in ourselves, and in our numbering.”

    For if that man should know not what he’s done,
    His tractor beams will all be off by one.

    Day 20: A Sonnet from AA to ZZ

    0) An Appetite for interesting sights 1) (eXcept For Eris; bugs aren’t fun at all) 2) Conspired Kindly to bring me to spots 3) Zipped High above by pilots more banal.

    4) Why Bother? ‘Cause I’m curious and bored. 5) I Can’t resist distraction. Never could. 6) Resistance? Futile, far as I’m concerned; 7) Never Met a puzzle and said “I’m good”.

    8) Lost? Perhaps. This maze is rather large. 9) For Dungeons, Pluto’s sure do take the cake. 10) XQ is next, and down and down I go; 9) Walking Backwa…wait, backwards?

    I’m trying to find the exit? Nothing more? No treasure at its heart? Then what’s it for?

    Day 21: Watch Your Step

    A disaster, then, briefly, a lull,
    So I sent out a springdroid to mull.
    But when I tried to pull
    All its springscript was full,
    So it fell through a hole in the hull.

    Day 22: Scrambled

    To mix one hundred trillion cards
    One-hundred-trillion-fold
    Cannot be done by mortal hands
    And shouldn’t be, all told.

    The cards make razors look like bricks;
    An atom, side to side.
    And even so, the deck itself,
    Is fourteen km wide.

    The kind of hands you’d need to have,
    To pick out every third,
    From cards that thin and decks that wide?
    It’s, plain to say, absurd!

    And then, a hundred trillion times?
    The time brings me to tears!
    One second each per shuffle, say:
    Three point one million years!

    Card games are fun, but this attempt?
    Old age will kill you dead.
    You still have an arcade in here…
    How ‘bout Breakout instead?

    Day 23: A Sonnet of Networking

    We are the unsung heroes who set sail.
    We’ve flown through space, and touched old Triton’s soil.
    And like the bees of Virgil’s epic tale,
    We hum and buzz in unison, to toil.

    But Virgil and his bees could not compete,
    And nor could any errant Carthiginian.
    Our network is by far man’s greatest feat
    (Though you may disagree with that opinion).

    And though our pilot has the smarts of bricks
    (His tractor beam produced the ship’s condition
    And tried to get a trillion cards to mix)
    He knows we’re vital to his greater mission.

    For when he sets a course out through the air,
    He knows our NAT and queues will get him there.

    Day 24: De Morgan’s Dream

    Great fleas have little fleas upon their backs to bite ‘em
    And little fleas have lesser fleas, and so ad infinitum.
    And the great fleas themselves, in turn, have greater fleas to go on;
    While these again have greater still, and greater still, and so on.

    – Augustus De Morgan, A Budget of Paradoxes

    De Morgan had a dream one night
    In which he flew and flew.
    He dreamed he flew by every world,
    And dreamed of Eris too.

    And in that dream, he saw some fleas
    All in a tiny grid.
    They hopped around and lived and died
    As he watched what they did.

    He landed there (not liking fleas,
    And wouldn’t you agree?).
    He walked through every square he saw
    And stepped on every flea.

    But minutes passed, and flea appeared
    That seemed to spawn from void.
    He didn’t know of Pluto’s race;
    He watched, and was annoyed.

    The mystery he dreamed about
    But couldn’t quite unpack:
    “Where are all these fleas coming from
    And how to send them back?”

    De Morgan tossed and turned that night,
    And when he woke from bed.
    He wrote down all the dreams of bugs
    That had been in his head.

    De Morgan couldn’t end the tale,
    But we know what to do:
    We know cellular automata,
    And bugs…

    We’ve seen a few.

    Day 25: A Ballade of Advent

    In French literature, a ballade was a fixed poetic form, with 24 lines followed by a four-line “envoi”, always addressed to the “Prince”, a.k.a. the reader or the patron of the work. That sort of format seemed a fitting way to end a month like this.

    We grabbed the fuel to launch and fly our sleigh;
    Then paused to get some opcodes to our name.
    We traced a maze of wires gone astray;
    And found a word with many parts the same.
    At Mercury we didn’t set aflame,
    But transferred orbits, counting all the way,
    And tied ourselves in loops to play the game
    Of puzzles leading up to Christmas Day.

    On Mars we helped a rover castaway,
    And then, near Ceres, opcodes all were done.
    The asteroids, we blew up with a ray.
    The moons, we watched, as they all danced and spun.
    We made some FUEL from ORE, all one by one
    Before a tractor caught us (much dismay!).
    Though fixing Intcode could be less than fun
    The ints all counted down to Christmas Day.

    We ‘borrowed’ Triton’s tractor beam array
    Then worked through Pluto’s mazes, dark and dim.
    We wrecked the hull with ‘harmless’ tractor play,
    But shuffled lots of cards (‘twas that or gym).
    And though ‘cat 6 disaster’ sounded grim,
    ‘Infinities of bugs’ is worse, I’d say.
    But we reached Santa, pockets stuffed to brim,
    And found one final star on Christmas Day.

    [ENVOI]

    Prince Topaz, you made all this on a whim,
    And mods, you kept the entropy at bay.
    Dear readers, may you all be blessed by Him
    Who long ago was born on Christmas Day.

    Series

  • Advent of Code 2019
  • Advent of Code 2020
  • Advent of Code 2021
  • Advent of Code 2022

  • This post is licensed under CC BY 4.0 by the author.
    Contents

    Comments powered by giscus.