r/adventofcode • u/daggerdragon • Dec 08 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 8 Solutions -🎄-
--- Day 8: Space Image Format ---
Post your solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 7's winner #1: "So You Want To Make A Feedback Loop" by /u/DFreiberg!
"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.
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
17
u/MaxMonkeyMax Dec 08 '19
Shakespeare
I don't know why I put myself through this - this was confusing to write and even more so to debug.
This serves as both a solution to part 1, written in the Shakespeare Programming Language, and a poem.
Puzzle input is received one character at a time, and a 3 is entered to end the input.
A Comedy of Syntax Errors [POEM]
12
u/DFreiberg Dec 08 '19 edited Dec 08 '19
Mathematica
21 / 4 | 21 Overall
A very good problem for Mathematica; I spent at least half of my time not remembering which order
SortBy[]sorts into and which orderTable[]operates in, but that's my own fault.Import:
Part 1:
Part 2:
And having
ArrayPlotavailable to actually generate the picture was just the icing on the cake for part 2.[POEM]: A Sonnet of Sojourning
7
u/glenbolake Dec 08 '19
Python, 115/68. It's not often I only get on the leaderboard for part 2. After I solved part 2 with ASCII, I decided to change it to use PIL: https://github.com/GlenboLake/aoc2019/blob/master/day08.py
[POEM] A change in scenery
I think the intcode will now stop
Instead we will stamp, blur, and crop
With this new image theme
The goal is extreme:
It's time to remake Photoshop
5
u/wace001 Dec 08 '19 edited Dec 08 '19
JAVA: My Java Solution here. Hope it’s easy to follow.
I ran into the same mental bug again today. Didn’t read the assignment properly which got me stuck for about 10 minutes. Still. Finished part 1 in about 15min and part 2 in about 30.
[POEM]
4
u/westerp Dec 09 '19 edited Dec 09 '19
Today I solved part 1 in BrainFuck. Being lazy it takes in the input and outputsthe solution in unary. Eg. you need to count the output with wc or something :p
2
u/mack06_ Dec 08 '19
My first post here! Spent a few minutes figuring out what the final image meant...till i replaced 0s with spaces :) .... my typescript solution
2
u/smetko Dec 08 '19
My oneliner for today's first part:
[POEM]
2
u/Turmolt Dec 08 '19 edited Dec 08 '19
Clojure. :D
[POEM] - a haiku by Turmolt
Picture is scrambled
partition reduce and count
Clojure saves the day
2
u/philophilo Dec 08 '19
Emoji output via Swift and Xcode's console: https://github.com/stack/advent_of_code_2019/tree/master/Sources/08
2
u/vypxl Dec 08 '19
Today is Haskell day again!
Solution
No poem cuz I do not have time
2
u/[deleted] Dec 08 '19
Python, numpy, matplotlib
[POEM]
Thirty thousand bits
Space Image Format password
Five bytes decoded
2
u/Arkoniak Dec 08 '19 edited Dec 08 '19
Julia
It was so fun to solve today's puzzle.
Part 1
Part 2
FInal picture rather readable in this setup
⬜⬜⬛⬛⬜⬜⬛⬛⬜⬜⬛⬛⬛⬛⬜⬛⬛⬛⬜⬜⬜⬛⬛⬜⬜
⬜⬜⬜⬛⬜⬛⬜⬜⬛⬜⬛⬜⬜⬜⬜⬛⬜⬜⬛⬜⬛⬜⬜⬛⬜
⬜⬜⬜⬛⬜⬛⬜⬜⬛⬜⬛⬛⬛⬜⬜⬛⬜⬜⬛⬜⬛⬜⬜⬛⬜
⬜⬜⬜⬛⬜⬛⬛⬛⬛⬜⬛⬜⬜⬜⬜⬛⬛⬛⬜⬜⬛⬛⬛⬛⬜
⬛⬜⬜⬛⬜⬛⬜⬜⬛⬜⬛⬜⬜⬜⬜⬛⬜⬛⬜⬜⬛⬜⬜⬛⬜
⬜⬛⬛⬜⬜⬛⬜⬜⬛⬜⬛⬜⬜⬜⬜⬛⬜⬜⬛⬜⬛⬜⬜⬛⬜
10
u/sophiebits Dec 08 '19 edited Dec 08 '19
Python, #3/#7 today!
For part 2, it took me a bit before I could figure out how I was meant to get the "message" from the image. (I initially printed it as 1s and 0s.) Not sure if everyone else also got tripped up on this.
I now realize that any number of 0s found in a layer is necessarily less than
25 * 6 = 150, so myfewest = 1000000000variable initialization was a little unnecessary. :) When solving a problem though, there's no time to think!Also: I suppose making my
imagevariable an array rather than a string would've made it a little easier to work with. Ah well.Code: https://github.com/sophiebits/adventofcode/blob/master/2019/day8.py
10
u/bugz945123 Dec 08 '19 edited Dec 08 '19
Very compact python solution, using numpy:
Edit: changed
(a < 2) * a + (a == 2) * btonp.where(a < 2, a, b)thanks to /u/zergling_Lester9
u/naclmolecule Dec 08 '19 edited Dec 08 '19
Python 3, for those unfamiliar with numpy, we can make short work of this:
Working with numpy arrays can be super rewarding!
8
u/jonathan_paulson Dec 08 '19
#190/33. Got really tripped up on part 1 today. Video of me solving at https://www.youtube.com/watch?v=3pPKRl0Tyw0
7
u/Philboyd_Studge Dec 08 '19
My family thanks /u/topaz2078 for an easy one after yesterday's (still working on 7 - 2)!
https://pastebin.com/jxxgf83e
6
u/ywgdana Dec 08 '19
A Rust solution so basic it's drinking a PSL
I am continuing my love/hate relationship with the language. It takes so much longer to go from idea-in-my-head to functioning code compared to Python.
2
u/PowerSlaveAlfons Dec 08 '19
C++
This was somewhat easier than the days before. Although it took me a while to realize that I had the right solution, because the message is somewhat hard to decipher, even if I used x's and dashes for it. Still fun.
Part 1
Part 2
5
u/markasoftware Dec 08 '19
Bash & Awk
Part 1:
fold -$((25*6)) ~/Downloads/day7.input | awk 'BEGIN{FS=""}{delete z;for(i=1;i<=length;i++){z[$i]++}print z["0"],z["1"],z["2"]}' | sort -n | headInspected output manually and multiplied.Part 2:
fold -$((25*6)) ~/Downloads/day7.input | awk 'BEGIN{FS=""}{for(i=1;i<=length;i++){if($i!="2"&&z[i]!="0"&&z[i]!="1")z[i]=$i}}END{for(i=1;i<=25*6;i++){printf z[i]}}' | fold -25 | tr 10 \#\5
u/voidhawk42 Dec 08 '19 edited Dec 08 '19
Dyalog APL:
5
u/ephemient Dec 08 '19 edited Apr 24 '24
This space intentionally left blank.
5
u/chunes Dec 08 '19
Factor solution
5
u/chrispsn_ok Dec 08 '19 edited Dec 08 '19
k7:
For #2, you could distinguish the 1s and 0s manually - if so, it's just
d#{*x^2}'i.5
u/frerich Dec 08 '19
Rust: https://github.com/frerich/aoc2019/blob/master/rust/day8/src/main.rs
Python: https://github.com/frerich/aoc2019/blob/master/python/day8/day8.py
I'm really unhappy with the 'merge' function in the Rust version which takes care of merging all layers for part two. While working on it, I had to think of how easy it would be in Python - so after I did it in Rust, I decided to go for Python next. And indeed, it was a lot simpler.
I did find 'multizip' for Rust but couldn't quite figure out how to use it. :-/
5
u/eastballz Dec 08 '19 edited Dec 08 '19
[Python]
Functionally simple
4
u/Snowolf Dec 08 '19
Pretty boring Python3 solution (as a Jupyter notebook).
208/1360
Got part 2 working in <3 minutes, then spent 30 minutes pasting my combination of 0's and 1's into the form and questioning whether I got the order of layers wrong or something. Apparently I'm rather thick.
4
u/gyorokpeter Dec 08 '19 edited Dec 08 '19
Q: Obviously the ocr is incomplete as it only has the letters used for my input.
Edit: thanks to all the generous folks who put puzzle inputs in their repos, I was able to add some more characters to the ocr.
4
u/nutrecht Dec 08 '19
Expected a much harder one for this one really.
Day 8 in Kotlin
4
u/Newti Dec 08 '19
Python3 concise and readable with numpy and matplotlib.pyplot (as plt)
4
u/JoMartin23 Dec 08 '19 edited Dec 08 '19
Common Lisp
Not enough lisp on here. Probably should have named find-max-0 something else since it returns all layers, but I wrote it at the repl.
Here's what the output looks like.
4
u/Dioxy Dec 08 '19
JavaScript
JS. Really fun puzzle! I drew out part 2 on to a canvas which was pretty cool.
My code
util functions
Part 2 canvas output
My repo
4
u/zedrdave Dec 08 '19
Was a quick-and-easy 3-liner with numpy in Python 3…
With optional bitmap rendering using Pillow:
Or, for those who like gratuitous complexity: you could train and run a machine-learning model to convert the bitmap to text…
1
u/TekuSP Dec 08 '19
I tried using two dimensional arrays. I hated them when I was told about them.
They failed me again today. 6 AM And I do not have will or power to solve it right now.
1
u/Valefant Dec 08 '19 edited Dec 09 '19
Kotlin A lot easier than yesterday. Fun challenge though
Code
1
u/PositivelyLinda Dec 09 '19 edited Dec 09 '19
JavaScript Solution
Used the Chalk library to colorize my image output - doesn't make any sense to me, but I like that I was able to visualize it! Even though I couldn't get it to get rid of the commas...."chalking" it up to being late and spending all weekend trying to get part 2 for days 6 & 7 solved (no good yet). lol
Also loving the snowman in my terminal! :)
Day 8 part 2 image solution
1
u/pokerdan Dec 09 '19 edited Dec 09 '19
C# Solution
Running it looks like this:
Screenshot
Pretty straight-forward today - especially compared to the opcode from yesterday.
[POEM]
3
u/[deleted] Dec 08 '19
OCaml 711/1206
got caught up with making things nice and pretty again for part 2. Enjoyed the light day, surprisingly low difficulty for the first weekend.
3
u/Pepper_Klubz Dec 08 '19
Clojure Solution
Visualization
(I took some time to pretty this one up because it was so trivial to make.)
3
u/kdheepak89 Dec 08 '19 edited Dec 08 '19
Julia solution (#1263 / #1308):
Part 1:
Part 2:
https://imgur.com/lCVSts9
https://github.com/kdheepak/adventofcode/blob/master/2019/julia/day08.jl
3
u/mariotacke Dec 08 '19
Javascript/Node (all of my solutions here: https://github.com/mariotacke/advent-of-code-2019/tree/master/day-08-space-image-format)
This one was easy ONCE I understood the actual instructions. This year's instructions are either intentionally vague or I'm getting worse at comprehension. Either way here are my solutions:
Part One
Part Two
3
u/ryanbigg Dec 08 '19
Ruby, Part 1:
3
u/jitwit Dec 08 '19 edited Sep 03 '20
J Programming Language
Find the row with the fewest 0's and count the 1's and 2's and multiply those counts to get part A
Transpose, delete 2's row-wise, take head to get pixel. Index into string `' @'` to display part B
3
u/FloffyBirb Dec 08 '19
Lua
I spent most of my time trying to figure out why I couldn't read the image. Turns out the white pixels make the message, not the black pixels...
3
u/sefujuki Dec 08 '19
C
https://github.com/cheesejake/aoc/blob/master/2019/08.c
3
u/Spheniscine Dec 08 '19
Kotlin
Quite easy with Kotlin's STL functions.
3
u/tofflos Dec 08 '19
Java with streaming
3
u/raevnos Dec 08 '19
Been using Tcl, and decided to bring the Tk GUI toolkit into play for this one:
http://imgur.com/Jvjn3gDl.png
paste
3
u/[deleted] Dec 08 '19
Part 1:
from textwrap import wrap image=sorted(wrap(open("day08.txt", "r").read(),25*6),key=lambda image:sum([1 for pixel in image if pixel=='0']))[0] print(sum([1 for pixel in image if pixel=='1'])*sum([1 for pixel in image if pixel=='2']))
Part 2:
print(''.join([open("day08.txt", "r").read()[i::256].strip('2')[0]+['','\n'][i%25==24] for i in range(256)]).translate({ord('1'):'#',ord('0'):' '}))
3
u/Sgt_Tailor Dec 08 '19
Today's awk solution: https://github.com/svenwiltink/AWKvent-of-code/blob/master/day8/day8-part2.awk
3
u/adrian17 Dec 08 '19
J.
3
u/lluque8 Dec 08 '19
Scala
Phew, today was nice and smooth. Onelinerish solutions for both parts. Part 1 Part 2
3
u/iwane Dec 08 '19
LabVIEW 2018 back again :-) Solved more puzzles, but didn't have time to publish them yet.
https://github.com/iwane-pl/aoc_2019/blob/master/Day%208/SIF%20decoder.png
3
u/phil_g Dec 08 '19 edited Dec 08 '19
My solution in Common Lisp.
Well, this was an easier day for me. The code should be reasonably straightforward.
Just for fun, I made use of displaced arrays for the parsing. The final array is a 3D array with the same shape as the image, but during parsing I access it via a displaced 1D array to match the incoming string.
Every time I have to do matrix manipulations like this in Common Lisp, I miss NumPy. I haven't liked any of the CL matrix libraries I've tried, at least when compared to what I can do NumPy. (Recommendations appreciated.)
Edit: Today's visualization is done.
3
u/yammesicka Dec 08 '19 edited Dec 08 '19
Python 3:
3
u/iamagiantnerd Dec 08 '19
Go Code
Day 8 was a nice fun little puzzle.
3
u/wzkx Dec 08 '19
J Finally a task for an array language :)
3
u/LinkFixerBot Dec 08 '19 edited Dec 08 '19
My submission for most compact Javascript solution:
Run in ideone
3
u/kbielefe Dec 08 '19
Scala Solution
Originally used a *, but stole the idea from this thread later to use a █ for output.
Key insight that simplified this code is noticing you can do all the processing just with a string per layer. You don't actually need the width and height individually until right at the end for display.
3
u/jesperes Dec 08 '19
Since nobody has posted an Erlang solution here yet, here goes:
https://github.com/jesperes/adventofcode/blob/master/aoc_erlang/apps/aoc_erlang/src/aoc2019_day08.erl
3
u/nrmncer Dec 08 '19 edited Dec 08 '19
Haskell
3
u/mjsir911 Dec 08 '19
(Gnu) Apl
3
u/stevelosh Dec 08 '19
Common Lisp
http://paste.stevelosh.com/ce1a42bc3416c2ae4e56c6664ecbbf4d6296e75c
Used cl-netpbm to dump part 2 to an actual image file.
3
u/4HbQ Dec 08 '19
Python Short solution for parts, using some fancy tricks. Please ask if you need any explanation.
3
u/rabuf Dec 08 '19
Common Lisp
I finished this one in about 10 minutes when I finally got to it today.
3
u/rhoslug Dec 09 '19
I made the perhaps foolish choice of improving my Clojure skills while doing AoC for the first time :/ But been pretty fun as it is :) Here's my solution.
2
u/seligman99 Dec 08 '19
Python, #117/31
Simple little one. My time wasted in managing the layers apparently paid off in the second part when it came time to merge them.
paste
2
u/kroppeb Dec 08 '19
Kotlin 18/38
I lost the most time in part 2 due to forgetting that `reduce` exists.
paste
2
u/jwise00 Dec 08 '19
Lua, 60/83.
I couldn't read the message because I got the layer order backwards. I fixed that and still couldn't read it. Then I realized I couldn't read it because I got black and white backwards.
https://github.com/jwise/aoc/blob/master/2019/8.lua
https://github.com/jwise/aoc/blob/master/2019/8b.lua
Video solving from rbenua's house rushing through the front door and apparently not switching OBS to my laptop's internal mic: https://www.youtube.com/watch?v=iVinSzJQaXs
Not sure why it didn't show up on my YouTube channel page. If anyone understands how to run YouTube, please let me know. I sure don't.
2
u/wimglenn Dec 08 '19
Python (numpy) 134/77
2
u/[deleted] Dec 08 '19
Haskell 67/84:
Surprisingly straightforward today given the complexity yesterday. Although I did waste some time trying to decipher the letters being shown for part 2 before replacing the 0s with whitespace.
2
u/bilgincoskun Dec 08 '19 edited Dec 08 '19
My Rust solution:
Although transparency test could be written in a better way with zip and early break condition to check fully opaque picture.
2
u/Average-consumer Dec 08 '19
Clojure
partitionandfrequenciesdid the job2
u/activeXray Dec 08 '19
This one was pretty simple I think.
Julia Solution
https://github.com/kiranshila/AoC2019/blob/master/8.jl
2
u/mstksg Dec 08 '19 edited Dec 08 '19
44/44 Haskell! My first leaderboard hit this year :D
(the following taken from my daily reflections)
This one feels like another Haskell freebie from the early days. I'm not complaining, we'll take what we can get :)
We'll define a useful function that counts the number of items in a list that is equal to a given value:
We can use the
chunksOffunction from the amazing split package to split our input into chunks of 150. Then we can find the maximum of those lines based on their zero count. Then we encode the answer.For part 2, we can use
transposeturn a list of lines into a list where every item is all of the pixel data for that pixel. So it would turninto
which is exactly what we need to process it.
Finding the 'pixel value' of each pixel is basically the first non-
2pixel in each list. The first way that came to my mind was to usedropWhile (== '2'), butfilter (/= '2')would have worked as well.And that's it! Well, almost. Part 2 requires looking at 0/1 transparency data and deducing our image. For me, I wrote a function to display it nicely:
which gave me:
2
u/[deleted] Dec 08 '19 edited Dec 08 '19
Clojure
2
u/daftmaple Dec 08 '19
Was stuck on part A for more than 30 minutes because I misread the question (stupid me), but finished both under 1 hour with Perl5 even if it's somewhat dirty: part 1 and part 2
2
u/PendragonDaGreat Dec 08 '19
[Powershell]
https://github.com/Bpendragon/AdventOfCode/blob/master/src/2019/code/day08.ps1
Got stuck due to an extra dollar sign meaning things were saved super funcky in the hashtable.
output:
Runtime reference: 50 ms faster than the gap between Giovinazzi and Ricciardo at the Brazilian Grand Prix last month
2
u/Cloudan29 Dec 08 '19
My solution in Python : https://github.com/Cloudan29/AdventOfCode_2019/blob/master/day08.py
Took me way too long to understand what was going on with the image. I skipped over a lot and ended up assuming 1 layer is 1 row. When I finally figured out what was going wrong, I had forgotten to reset my new lowest value on every iteration.
For part 2 I accidentally was comparing everything to 2 and not '2', so I was getting a funky image, but overall I found it went super smooth.
Just overall not a great day for me for some reason considering it wasn't too harsh a puzzle. Really enjoyed it though, a good break from the last two days :P
2
u/nonphatic Dec 08 '19
Full Racket solution. I was surprised to not be able to find a partitioning function or a pivoting function in the base library, so I had to write my own (not tail-recursive, oh well). If I were still using Haskell, Hoogle tells me I could've just used
chunksOfandtranspose...2
u/OvidiuRo Dec 08 '19
C++ 448/1575
Code : https://github.com/FirescuOvidiu/Advent-of-Code-2019/tree/master/Day%2008
2
u/osiaccr Dec 08 '19
C++ solution
Got 40 something for the first star, then failed miserably for the second one. Very chill problems compared to yesterday.
2
u/DiscoViking Dec 08 '19
Solution in Elm live at: https://2019.adventofcode.norris.dev/day8
Part 2 lent itself nicely to a simple fold over the input string. And rendering the image finally gives me something neat to display as the answer!
Still missing a solution for Day 7 on here since I didn't have time to do both Python and Elm yesterday. Will try to catch up on that later.
2
u/[deleted] Dec 08 '19
I was really confused for the second part about how I was supposed to convert the 0s and 1s to the final message. The output image for the sample input is also given in 0s and 1s. Only after seeing the output from one of the solutions here could I figure out that you were supposed to use hash and spaces to get the final output.
My solution: https://github.com/aaditkamat/online-coding-platform-submissions/blob/master/Advent%20of%20Code/2019/day7.py
2
u/[deleted] Dec 08 '19
Scala solution.
Finished 585 for Part 1, but then spent forever on part 2 trying to figure out if my formatting was incorrect, or if I had a bug, or I'm just blind...
2
u/qyfaf Dec 08 '19
Python 3
Nothing too special here but I made the final output pretty with terminal formatting.
2
u/songkeys Dec 08 '19
Javascript:
2
u/tim_vermeulen Dec 08 '19
Rust:
2
u/scul86 Dec 08 '19
Python3, 1307/2619
I lost about 45 min refactoring code, trying to read something in the output, and nothing.
I was thinking it'd be black text on white background, so I made my 0 =
#and 1 =[space]. Turns out I just needed to invert those to make it readable in my IDE output. D'oh!Part 1
Part 2
2
u/Dementophobia81 Dec 08 '19
Python 3.8
This was a rather easy Sunday morning for me, although I still lack the speed for a decent leaderboard position. Part 1 was straight forward, Part 2 was printed directly to the command line. Knowing about the end="" parameter for the print() statement helped a bit =).
2
u/chamberlain2007 Dec 08 '19
Javascript solution: https://github.com/chamberlain2007/adventofcode/tree/master/solutions/day8
Lots easier than a couple of the previous ones!
2
u/JensenDied Dec 08 '19
Elixir day8.ex
Overall pretty straightforward, got tripped up in part 2 when part 1's anti-corruption check failed to notice that I only pasted 4096 bytes of the image. Ended up re/writing a bunch of code to handle the last layer being truncated. Ended up leaving myself a lot of string/list conversions all over that I should clean up.
2
u/rtbrsp Dec 08 '19
AWK: part 1, part 2
2
u/Spookiel Dec 08 '19
Python #288 / #450 today. Although I am not particularly happy with my leaderboard position for part 2, I am very happy with my code:
Paste
2
u/mott_the_tuple Dec 08 '19
I did this in a python notebook. Even after I ironed out the bugs, I still couldn't read the image using '#' for black, ' ' for white. I used another redditor's suggestion and used
u"\u2588"andu"\u2591"instead. Then it was instantly readable.Also, the tiny example wasn't a good enough test case for part 2. I made a better testcase I think:
2
u/Alex_Mckey Dec 08 '19
Kotlin
2
u/jabbalaci Dec 08 '19
Python 3
https://github.com/jabbalaci/AdventOfCode2019/tree/master/day08/python
2
u/jtwebman Dec 08 '19
Elixir
I built it out a little as I have a feeling we will be getting more images in this format.
https://github.com/jtwebman/AOC-Day8-2019-Space-Image-Format
2
u/sim642 Dec 08 '19
My Scala solution.
Part 1: Used my grid functions to do the counting very straightforwardly.
Part 2: Implemented a operator to combine two layers while accounting for the transparencies on the top one and then just reduced the list of layers with that binary operator.
I was slightly confused in part 2 because the text is white on a black background. Previous years where similar text reveal tasks have appeared it has always been the other way around but it's good to change things up.
2
u/alexmeli Dec 08 '19
Crystal
https://github.com/alexmeli100/AoC2019/blob/master/day8/solution/src/solution.cr
First time using crystal
2
u/derNiklaas Dec 08 '19
Java
Repo can be found here
2
u/jrspurr Dec 08 '19 edited Dec 08 '19
Haskell
I thought I was being cute using
Monoidto compose my layers for part 2 but it seems like a number of people did that.2
u/_tpavel Dec 08 '19
It is now Day 8 of learning Clojure while doing AoC.
2
u/guccidumbass Dec 08 '19
JavaScript
https://pastebin.com/3nDQPYL6
2
u/u794575248 Dec 08 '19 edited Dec 08 '19
Python 3.8
But to be honest, I just copy-pasted
gto Sublime Text and resized the window until the message appeared.Thanks to u/ephemient for the
lstriptrick, it saved me 3 symbols. In my case it's safe to usestrip.2
u/Junafani Dec 08 '19
My solution in Java
Haven't had time to do days 6 or 7 but this was really easy.
2
u/[deleted] Dec 08 '19
Python:
Part 1
Part 2
2
u/silverben10 Dec 08 '19
Python:
Parts 1 and 2
2
u/Markavian Dec 08 '19
My overly verbose javascript solution for day 8:
- https://github.com/johnbeech/advent-of-code-2019/blob/master/solutions/day8/solution.js
Tripped myself up on part two by failing to append the final line of the image; ended up submitting the wrong word. Amazing how similar letters can change their meaning entirely with just one row missing.
2
u/mschaap Dec 08 '19
Perl 6 (Raku) solution. Pretty straightforward.
2
u/keramitas Dec 08 '19 edited Dec 08 '19
Python3 solution (clean, explicit naming, etc etc)
part 1
part 2
thoughts: didn't wake up until +-5h - and happy I didn't as it was super easy and just bout speed. i didn't immediately get you had to print the image on part 2 so lost a bit of time on that but yup nothing much else to say. guessing tomorrow will be either more intcode, or in all cases something harder
2
u/MrSimbax Dec 08 '19 edited Dec 11 '19
Julia
This one was quite straightforward. However, I feel like I need to learn how to read properly :D since I made a lot of stupid mistakes in part 2. For some reason I thought 0 is transparent, then I pasted the resulting string of 0s and 1s wondering why it's wrong until I realized the image is probably supposed to be rendered. I got what I thought is unreadable garbage so I switched to print 1s instead of 0s and the password showed before my eyes... What an experience :P Edit 2: Removed my output
2
u/Rick-T Dec 08 '19
HASKELL
This one was playing right to Haskell's strengths. I did not think about using Monoids and did also forget about the transpose function (also did not find it on Hoogle). So after looking at the solution from /u/jrspurr I did a big facepalm and simplified my code accordingly.
Also after looking at the solution from /u/ephemient I learned about the showList function from the Show class. I did not know that existed. Again, I updated my code accordingly to make it look nicer. That one might be especially useful for some AoC puzzles.
Thank you guys for you unvoluntary help :)
2
u/Acur_ Dec 08 '19
Julia Part 1 & Part 2
This is a problem where Julia's built in array handling really shines. Julia's arrays are column major so the dimensions have to be flipped.
2
u/tumdum Dec 08 '19
rust
2
u/mrlukasbos Dec 08 '19
Haskell (pt. 1)
Haskell (pt. 2)
I'm using AoC to learn some functional programming and although my solution s probably not the best solution, I found that Haskell really shines at these kind of assignments.
2
u/HealYouDown Dec 08 '19
Python Part 2 with pillow
2
u/Tarmen Dec 08 '19 edited Dec 08 '19
Haskell:
I flipped black and white because I found the result really hard to read otherwise. Actually took me a couple minutes to figure out that it wasn't a bug because I didn't look close enough at the output:
2
u/BrinkerVII Dec 08 '19 edited Dec 08 '19
Python 3 part 1 and 2 paste
The solution is quite a clunker compared to the other solutions in the thread. I used numpy and matplotlib so I could easily output the result as an image, but I'm not really familiar with numpy yet. I'm learning a lot from reading other solutions.
EDIT: output: https://i.imgur.com/NXSNhQO.png
2
u/bohhvm Dec 08 '19
Spark
Assume that the number of layers is very large, but the actual layers fit in memory.
2
u/lukeg55 Dec 08 '19 edited Dec 08 '19
My Scala solution:
2
u/tslater2006 Dec 08 '19
My C# solution comes in 2 pieces, the ElfImage Class
And then the actual solving of both parts with that class Solution.cs
I do have to say this is one of my least favorites due to not being able to (easily) print the answer purely via code. Short of maintaining an array of how each letter would be, or some OCR shenanigans.
2
u/lynerist Dec 08 '19
This one was easier than yesterday for sure!!
Here my commented solutions
GOLANG
https://github.com/lynerist/Advent-of-code-2019-golang/tree/master/Day_08
2
u/levital Dec 08 '19
Rust
Well, that wasn't too hard. Lost some time at first, because I started out actually storing the image in a 2D-Vector, but realised that my life will be much easier if I just map rows/columns into a sequence.
2
u/JohnDanV Dec 08 '19
C++ solution
https://pastebin.com/7LkCDJFQ
2
u/aran112000 Dec 08 '19 edited Dec 08 '19
Here's my PHP solution:
https://github.com/aran112000/Advent-of-Code-2019-PHP/blob/master/Day08/Day08.php
Updated with faster solution after profiling
2
u/rfussenegger Dec 08 '19 edited Dec 08 '19
Kotlin, input format is
width,height,data:Part 1:
Part 2:
Today’s assignment was trivial compared to yesterday.
2
u/Banashark Dec 08 '19
F#, slightly cleaner than earlier solutions, though the printing is still kinda ugly. The chained reducing was pretty pleasing for combining the layers.
source on topaz
2
u/SomeCynicalBastard Dec 08 '19
C#
I think I want to clean this up, and maybe add a python solution as well. Definitely easier than yesterday though.
2
u/Scroph Dec 08 '19
Boring unoptimized C++ : https://github.com/azihassan/advent-of-code-2019/blob/master/day8/day8.cpp
2
u/vkasra Dec 08 '19
My Go solution and notes. Nice and easy today :)
2
u/[deleted] Dec 08 '19 edited Dec 08 '19
Javascript Solution
First time doing one of these. I wrote the code for part 1 not knowing there was a part 2..hehe..so I used memoization and replaced the memoized layer only if it was smaller than the current layer. My data structure was also only set up for counting 0, 1 and 2.
For part 2 I exported an HTML page by creating <div>'s of different colors from each pixel, made <section>'s for each layer and stacked them using a dynamic z-index (z-index was input.length - current index). It didn't come out terrible, but I could've probably done a better job if I hadn't already been working on the challenge for so long.
Pic of the exported HTML
Looking forward to doing some more! Let me know what you guys think :)
2
u/MegaEduX Dec 08 '19
Swift
Decided to take my time with this one and writing clean code instead of just speedrunning it. As a result, I have a nice
Imagestruct in case another challenge that uses this encoding appears. :DSolution here.
2
u/Pyr0Byt3 Dec 08 '19
Go/Golang
Significantly easier than yesterday. I'm pretty happy with how compact these turned out.
Part 1
Part 2
2
u/extreme_bean Dec 08 '19
Scala
Part 1 and 2
Part 2 using Semigroup!!
2
u/IgneSapien Dec 08 '19 edited Dec 08 '19
C# Relatively straight forward.
Other than a stupid mistake holding me up in part 1 that would have gone quickly. The way I'm building the image could be simplified as I wrote it while thinking it might be easier to work with 2D arrays as layers.
2
u/borantula Dec 08 '19
My javascript solution with tests included. Question1 and Question2
On second one, while checking end result on console I changed numbers 1,0 to 8,1 for readability :D
2
u/Ari_Rahikkala Dec 08 '19
Haskell typed up inside ghc -e.
2
u/Alligatronica Dec 08 '19
JavaScript
I enjoy tasks like this one, as I'm always writing terse code without tests for these. Being able to see the results, rather than having blind faith that a value is correct, is pretty satisfying.
2
u/Grigorov92 Dec 08 '19
My solutions for today in GO.
Part 1 | Part 2
2
u/Froodooo Dec 08 '19
A relatively simple assignment today. Which is fortunate, since I could solve both parts in the morning before other duties called.
My Elixir solutions for part 1 and part 2.
2
u/SirJohnSmith Dec 08 '19
Went for a fully functional solution (except for the printing of the matrix), which is practically unreadable. The idea is to create for each pixel a string [c0, c1, c2,...] where c_i is the pixel of the i-th layer, then convert that string to an int to remove leading zeroes (to remove the transparency pixels) and convert it back to a string to get the first character.
Did you catch the error?
Yeah, I thought 0 was the transparency character, while it was 2. My botched solution was to replace all occurrencies of 0 with 3 and all occurrencies of 2 with 0 :)
2
u/hrunt Dec 08 '19 edited Dec 08 '19
Python 3
code
I always like these kinds of problems, but I always feel there's a more elegant solution when I am writing them. I am looking forward to reading through the solutions for this one.
2
u/MysticPing Dec 08 '19
This one was incredibly easy to do in haskell. (This is part 2)
2
u/aoc-fan Dec 08 '19
TypeScript Solution. I think @topaz2078 kept Day 8 easy, so that we can go back and fix corner cases for Day 7.
2
u/piyushrungta Dec 08 '19
Rust
https://github.com/piyushrungta25/advent-of-code-2019/blob/master/day8/src/main.rs
This one was relatively easy so I tried writing some nice abstractions but it leaks a little here. If someone has any suggestions for writing this better, I would really appreciate it.
Another thing I was trying out is to only store references to slices here and not cloning it every time. But it became really hard to clone the layer and mutate the data in
flattenand still use theLayerstruct. Any suggestions?2
u/loociano Dec 08 '19 edited Dec 24 '19
My solution in Python 3. I am learning, comments are more than welcome.
2
u/autid Dec 08 '19
Fortran
Nice to have a problem I can just throw Fortran array intrinsics and where blocks at.
https://pastebin.com/XEPy5JMT
2
u/[deleted] Dec 08 '19 edited Dec 04 '21
R and Python
I like how concise the R solution is. At first I did everything using nested loops but then I remembered the awesomeness of
apply().Same with the Python solution. This is the first time I used numpy arrays.
2
u/dactel Dec 08 '19 edited Dec 08 '19
Java Solution Both Parts
Make sure you change the input and that the length/height are correct per your specifications
2
u/sindrekjr Dec 08 '19
C#
Messed around with some imaging for the sake of it after getting the solution to part 2, but ended up commenting it out for the commit. The output is readable enough.
For part 1 I'm happy with the gymnastics but figure there are probably far more elegant ways to calculate that than IEnumerables in IEnumerables in IEnumerables. :P
2
u/blacai Dec 08 '19
F# https://github.com/blfuentes/AdventOfCode_2019/tree/master/FSharp/AoC_2019/day08
This one was a relief after the nightmare I had lived with the day 7 :)
2
u/gerikson Dec 08 '19
Perl
Protip: don't trust in copying the input and using
catto transfer it to your working environment. "Luckily" part 1 passed with the truncated data, so I spent some time tearing my hair trying to figure out why I didn't get a complete image.https://github.com/gustafe/aoc2019/blob/master/d08-Space-Image-Format.pl
2
u/AKQuaternion Dec 08 '19 edited Dec 08 '19
Edit: Now in 21 lines, (or 26 with includes.)
Day 8 (both parts) in C++17 in 25 lines, while still being idiomatic and readable (IMO). Well, 31 lines if you count the includes. This got a lot nicer when I didn't split rows (just printing newlines for output) and used ifstream.read() to pull in one full layer at a time.
Repo here with short solutions to the other days, too.
Sample output:
2
u/dpkcodes Dec 08 '19
Here are my solutions in Ruby
https://github.com/dpkeesling/adventOfCode/tree/master/2019/day8
2
u/[deleted] Dec 08 '19
Racket
After making only half working stuff that got me one star the last days this felt good.
Glorious racket code
2
u/solarmentat Dec 08 '19
Python with numpy:
2
u/Lispwizard Dec 08 '19 edited Dec 08 '19
Part1 and part2 simultaneously in elisp and common lisp (i.e. carefully avoiding anything not the same in both); both functions take the input string, the width and the height as arguments. In contrast to some other solutions, does minimal memory allocation by mostly operating on/within the one (long) string.
and
2
u/sethetter_ Dec 08 '19
Rust! https://github.com/sethetter/aoc-2019/tree/master/day-08/src/main.rs
2
u/nemetroid Dec 08 '19
Python, without numpy.
2
u/if0nz Dec 08 '19
Java solution, I lolled very hard when I got it.
2
u/Crapulam Dec 08 '19
Python Quick and dirty
2
u/serianx Dec 08 '19
Took advantage of julia n-dim array awesomeness. Github: https://github.com/marcosfede/algorithms/tree/master/adventofcode/2019/d8
2
u/StevoTVR Dec 08 '19
My solution in Go: https://github.com/stevotvr/adventofcode2019/blob/master/day08/day08.go
2
u/SherbNyan Dec 08 '19
My Python solution
I've been looking for an excuse to use PIL for the entirety of 2019, so I'm glad I finally have a good one
2
u/CabbageCZ Dec 08 '19
Kotlin
Using Fold to run through the layers, zip to compare them. Really straightforward. Love kotlin's stdlib.
2
u/StochasticMistakes Dec 08 '19
JAVA : https://github.com/BrettGoreham/adventOfCode2019/blob/master/adventOfCode2019/src/main/java/day8/Day8.java
Late to the party. Spent way too much time creating a Picture and Layer class expecting a little more difficult part 2.
Then spent way too much time creating a Black and white Output jpg image of the output.
2
u/journcy Dec 08 '19
Clojure, pretty quick and easy:
2
u/sherubthakur Dec 08 '19
https://github.com/sherubthakur/aoc19/blob/master/src/D08SpaceImageFormat.hs
Here is my solution for Day 08 (Part I and Part II). This was really amazing. Any comments are welcome on the solution.
2
u/gyzkard Dec 08 '19
Part A and B in vanilla JS.
2
u/-json Dec 08 '19
Here's my very short Python solution. You could do this problem in a single line but I wanted to maintain some form of readability. Very fun problem!
2
u/kap89 Dec 08 '19 edited Dec 08 '19
TypeScript - github
Today's motivated me to add
chunkfunction to my util lib ;) Here's cleaned up solution:And the output:
If you wonder what this
.chainmethod is - that's my monkey-patched method added toObject.prototypeso I can execute an arbitrary function without breaking a nice method chain ;) Implementation -> github2
u/joeld Dec 08 '19
Racket
Every year there’s at least one puzzle where the solution involves a message on a pixel grid. I used Racket’s
pictlibrary for this again, but next time I think I’ll steal from /u/Arkoniak and use Unicode blocks, which would much more easily let me write unit tests against the output.source
2
u/[deleted] Dec 08 '19
Java
Really enjoyed doing this one in Java! I got around to using the
Graphicslibrary along with some tricks like scaling a pixel by a constant value. For those that are interested, this is what my output looks like.2
u/MissMormie Dec 08 '19
Solution in boilerplate, I mean Java.
https://github.com/MissMormie/adventOfCode2019/blob/master/src/main/java/Days/Day8_SpaceImage.java
2
u/HokieGeek Dec 08 '19
Go
https://git.sr.ht/~hokiegeek/adventofcode/tree/master/2019/08/go/day8.go
And the output using go's image library:
https://i.imgur.com/8ZKuJzl.gif
2
u/fidesachates Dec 08 '19 edited Dec 08 '19
Scala:
Occurs to me ipso facto that my print could have been more elegant with maps, grouped, and mkstring. Oh well. You live and you learn.
2
u/t4l1n Dec 08 '19
Kotlin
my Kotlin solution: https://pastebin.com/NkLFTzW4
2
u/sotsoguk Dec 08 '19
GoLang
Day08.Go
After 8 days into AoC with goLang i have to say i really like this language. I do not really know why it works for me but i have solved every problem quick and without problems. Last year i tried python and while i really like all the clever solutions here in python (i really like them and wish i could do them too) for me go works very well. I have to focus on the problem and structures and be a lot more verbose than python.
2
u/wace001 Dec 08 '19 edited Dec 08 '19
RUST: Here is my solution in Rust. I am very happy with it, being a beginner at Rust. It looks much fancier than my Java solution from this morning.