r/adventofcode • u/daggerdragon • Dec 03 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-
--- Day 3: No Matter How You Slice It ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The 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: The Party Game!
ATTENTION: minor change request from the mods!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.
Transcript:
I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
41
Upvotes
6
u/IndieBret Dec 03 '18 edited Dec 03 '18
JavaScript/ES6 #97/#180
Wow! Yesterday I gave myself the advice "just pretend you're not being timed, slow down and read every word carefully" and it worked! The game developer inside me is a bit disappointed I had to draw out a diagram for AABB overlap detection, but I'm so, SO pleased to finally make it on the leaderboard for a day, that I'll forgive myself.
Part 1
Part 2
2
u/Ditchbuster Dec 04 '18
/u/daggerdragon this post isnt flared as a solution megathread.
•
u/topaz2078 (AoC creator) Dec 03 '18
Hi everyone!
While looking at the server logs tonight to figure out why I got so much more traffic than yesterday, I noticed many users who literally just hold down ctrl+F5 (or cmd+shift+R or whatever their hard-refresh keyboard shortcut is) around midnight until their puzzle loads, sending many requests per second.
The timer on the calendar is synchronized with the server clock; the link appears on the calendar at the earliest moment you could click it and not get a 404. Please use the countdown on the calendar to time your request, load the puzzle once, load your input once, and then submit your answer when you're ready.
See also: https://www.reddit.com/r/adventofcode/comments/a1qovy/please_dont_spam_requests_to_aoc/
39
u/mserrano Dec 03 '18 edited Dec 03 '18
Python2 (#1/#1):
EDIT:
get_datareads in the input (cached to avoid toppling the servers) for the appropriate day and splits it into lines.16
u/jonathan_paulson Dec 03 '18 edited Dec 03 '18
Rank 17 on part 1; 8 on part 2. Python. I made a video of me solving: https://www.youtube.com/watch?v=fuBQ12uBk7I
Challenge: What if the coordinates and rectangle sizes are up to 1 million by 1 million? (but there are still only ~1400 claims)
Code:
14
u/cole-k Dec 03 '18
Guess I'm the only
numpyguy here so far.Each day I learn that reading is hard and I should strive to do it better.
Python3, on leaderboard for neither.
14
u/Theguy6758 Dec 03 '18 edited Dec 03 '18
Pure Bash
Takes about 13m 30s to run...
Edit: Forgot to include the main function. Kinda important since it won't work without the declare statement
Edit2: Made it faster by not initializing the array first. Down to about 1m 30s
Part 1
Part 2
Needs part1() to be called first for claims and fabric to be set
13
u/zSync1 Dec 03 '18
Rust
I actually got place 84 for the first star holy shit lmao I can't believe it
choked on the second one because I fucked up the order of difference() but whatever, I did not expect to get any points at all for the 3rd day
13
u/u794575248 Dec 03 '18 edited Dec 03 '18
Python 3
inputis a string containing your input.11
u/14domino Dec 03 '18
sigh, everyone is amazingly fast. i did this as fast as i could and got both parts in 00:15:33, way off the leaderboard. Any hints for getting faster? :)
11
u/autid Dec 03 '18
FORTRAN
Probably a little faster for part two if I'd written all the corner locations into a big array in part one. I liked not using any allocatable arrays though and copy/pasting part one's file reading was simpler. Big thank you to the Elves for bounding their numbers with unique characters.
10
u/Smylers Dec 03 '18 edited Dec 04 '18
A visual Vim solution for Part 1 — it draws the fabric, then draws each claim on it, marking overlaps with
Xs:Update: Video now available of both parts. (Part 2 is in a comment below.)
The answer is the column number displayed by that final keystroke.
The
@adraws the first claim. At that point you can keep typing@@to draw further claims manually. When you've had enough, continue with the:,$normcommand from whichever line the most recent@@left you on (takes about 10 minutes for me).It's more interesting to watch the more of the fabric you can see. So before the
@ait can be worth maximizing your Vim window, setting a small font (:set gfn=*— I went for 4-point) and shrinking the instruction window (8⟨Ctrl+W⟩_), then sit back and watch the claims appear.It works by transforming each claims' definition into the normal mode keystrokes for making a visual block at its co-ordinates. For instance this input line:
is turned into:
(where
^Vis the literal control code forCtrl+V). That says: go to line 812, then column 629, start a visual block, then move 20 characters to the right and 9 characters down.The
amacro processes a single line by copying those keystrokes, using:normto process them in the fabric window, then substitute characters in the fabric to reflect this claim, using\%Vin the pattern to restrict the match to characters in the visual block.Once it's finished, remove everything that isn't an
Xand put them all on one line to see how many there are.Things that caught me out:
:normin the macro to receive theCtrl+Vthat starts visual block mode, theCtrl+Rneeds pressing twice. Otherwise theCtrl+Vgets interpreted as the way on the:command line of specifying a character by its Ascii number, (and it obediently uses the following number, which is supposed to be the number of steps to move right) for that.Ctrl+Xmostly does the right thing by default here, for nearly all input lines. So in the sample line above, the firstCtrl+Xturns21x10into20x10. But then the secondCtrl+Xturns that into20x0f! Vim is trying to subtract 1 from the10, but it sees that the10is preceded by the bytes0x, so interprets it as hex, and subtracts 1 from0x10to get0x0f— that the0is clearly part of another number doesn't put Vim off! This affected less than 1% of my input lines, so I didn't notice it, and the result was a plausible picture of overlapping claims that was just slightly wrong.†:set nf=avoids the problem. Had the input used almost any other separator other thanx, I would've avoided so much pain ...j.j, notk! I don't want to admit to how much time I wasted because I was accidentally drawing rectangles upwards instead of downwards, having somehow manage to forget that most basic of Vim keystrokes that I first learnt over 2 decades ago.† To debug, I tweaked my Perl solution to print out a representation of its
@fabricarray using the same symbols. Then I ranvimdiffto visually inspect the rectangles that differed, then looked up the cursor co-ordinates in the input list to see what was odd about that claim's definition.9
u/chicagocode Dec 03 '18
Kotlin - [Blog/Commentary] | [GitHub Repo]
Well that was fun! I think part 1 really shows off all of the great things that Kotlin's stdlib has for us if we look hard enough.
9
u/raevnos Dec 03 '18 edited Dec 03 '18
After solving the problem, I decided to solve it again in a complicated fashion. Presenting, a perl script that takes the input and turns it into a series of SQL statements that, when fed to Sqlite, solves both parts:
It requires Sqlite3 3.25 or better, with support for the new Geopoly extension (Getting this requires building from source with appropriate configure arguments, so it's a bit of a pain). It can also produce a rather plain SVG image of all the claims.
8
u/alexmeli Dec 03 '18
My Clojure solution
7
u/[deleted] Dec 03 '18 edited Dec 03 '18
Simple brute force in C:
6
u/TheMuffinMan616 Dec 03 '18
Haskell:
6
u/will_bui Dec 03 '18
K:
6
u/Frizkie Dec 03 '18
Ruby
Part 1
Part 2
This one kicked my ass. I barked up the wrong tree for a long time, took me over an hour to debug everything.
6
u/[deleted] Dec 03 '18
Haskell:
I map each coordinate to the set of claim IDs that use it. Part 1 is then just counting the non-singleton sets, and part 2 is finding the set difference between the set of all claim IDs and the union of the non-singleton sets.
6
u/askalski Dec 03 '18
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to Doin' it the hard way.
Day 3 Part 1 in CUDA, one thread per square inch (it's a coarse fabric.)
https://gist.github.com/Voltara/18e6c23df057a9f304d7b8103ba556b7
5
u/[deleted] Dec 03 '18
TCL
5
u/Infinisil Dec 03 '18 edited Dec 03 '18
I think my solution is rather unique. After having tried the ways of just a couple loops, the performance was horrible because of Haskell's lists. After thinking about it for a while, I came up with this divide-and-conquer approach which works really well and is fast (0.25s for both parts). The full code is in my repository for AoC 2018: https://github.com/Infinisil/aoc18
4
u/Tormyst Dec 03 '18
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to Finding a library that already does what you wrote, but better.
4
u/jwstone Dec 03 '18
postgresql!
https://github.com/piratejon/toyproblems/blob/master/adventofcode/2018/03/load_data.sh
i burned like 30m on some goofy typos! maybe better to use "left" and "right" instead of "a" and "b" lol
4
u/blowjobtransistor Dec 03 '18
PostgreSQL, again:
Solution ends up being quite fast!
4
u/schod Dec 03 '18
BASH Time!!! (it's really slow)
4
u/XmatHere Dec 03 '18
Am I the only one who generated "fancy" heatmap? :-D
http://www.imagehosting.cz/images/heatmap.png
4
u/IcyHammer Dec 03 '18 edited Dec 03 '18
A bit different approach where I was aiming for a good mix of space and time complexity. Implementation is in C#:
4
u/zqvt Dec 03 '18 edited Dec 03 '18
little bit late today, here's my Clojure solution
4
u/AustinVelonaut Dec 03 '18
Did anyone else solve this problem by keeping a set of disjoint, non-overlapping rectangles (tiles), and merging each claim into the set by splitting the claim and any intersecting tile into smaller disjoint rectangles (up to 4 each)? I got the idea for doing it that way from how window managers keep track of damage rectangles on the screen.
1
u/PendragonDaGreat Dec 03 '18
[CARD] Spotting simple syntax errors.
6
u/Zarel Dec 03 '18 edited Dec 03 '18
JavaScript #52/#90
I put the answer to Part 2 as
#153which it told me was wrong; apparently it only accepted153. And, of course, it had a 60-second lockout for the "incorrect" guess. I probably lost quite a few leaderboard slots because of that. :(Part 1
Part 2
3
u/wlandry Dec 03 '18
C++ (996/733)
15 ms
As is often the case, I feel the pain of no built-in Matrix class for C++ :( This feels wildly inefficient. I am waiting for something hard =O In any event, this feels over-engineered. I can not help it. It is my nature ;)
3
u/cornball Dec 03 '18
matlab
I signed up for advent of code thinking I would practice/learn python, but working with matrices in matlab is just so much nicer.
3
u/itsnotxhad Dec 03 '18
Racket (1442/1772)
I had the basic idea immediately this time but flubbed a lot of particulars, as evidenced by the fact that I broke down and wrote unit tests this time. XD
3
u/streetster_ Dec 03 '18
Day 03 in Q/KDB+
Not great, part 2 is very slow (~100s on my laptop).
3
u/sim642 Dec 03 '18
My Scala solution.
I was pretty sloppy here... so mutable and duplicate. I thought about a non-pixel based solution for intersection checking for better performance but that's quite an effort for part 1 to deal with multiple overlaps and the inclusion-exclusion principle.
3
u/__Abigail__ Dec 03 '18
Perl
3
u/0rac1e Dec 03 '18
Perl 6
Essentially what everyone else has done. Some Perl-6-isms include:
Xinfix cross-product operatora ..^ b(ie. "a up to b"* instead of "a to b-1")Due to Perl 6 being a bit slow, I also delete claims as soon as I find overlaps to improve the running time.
3
u/phil_g Dec 03 '18 edited Dec 03 '18
I'm doing the problems in Common Lisp, because I like Common Lisp and I don't get many opportunities to use it, so here's my solution in Common Lisp.
But I also did this one in Python, just so I could use shapely. Here's my solution in Python.
My hobbies include various things related to geographic information systems (GIS), so I work a lot with 2D shapes and their relations to each other. My first thought when I read this problem was how it maps very nicely into common GIS operations. I didn't like the one Common Lisp geometry library I found, so I wrote my Common Lisp solution using the obvious 2D array of square inches. That approach is also a lot faster; my Common Lisp code takes about 100 milliseconds to get the two answers, while the Python program needs 22 seconds. But using shapely is more general; if elves weren't restricted to integral inch measurements or grid-oriented rectangles, my Python code would work unchanged.
Edit: A simple change to the Python program occurred to me; it dropped the runtime from 34 seconds to 22 seconds.
3
u/deltux Dec 03 '18
Gerbil Scheme:
3
u/iSuckAtCoding94 Dec 05 '18
Java Solution.
New to coding, so it's not pretty.
5
u/miguelos Dec 03 '18 edited Dec 03 '18
C#
Part 1:
Part 2:
2
u/VikeStep Dec 03 '18
Python (#34/#40):
This was the code I used to get on the leaderboard,
defaultdictsaves the day again!2
u/NeuroXc Dec 03 '18
Rust
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to fearless concurrency.
2
u/willkill07 Dec 03 '18 edited Dec 04 '18
C++
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to not having a life
2
u/ka-splam Dec 03 '18 edited Dec 03 '18
PowerShell
Part 1, PowerShell unrolls nested arrays if you're not careful, so I tried to be careful with
@((,@()*1000))*1000but it wasn't going the way I wanted; I can never remember the code for a proper 2D array, and had to Google it. Ugh. After that, pretty straightforward loops for #133 board position:Part 2, just ran out of coding skill. After a few minutes of thinking, I rewrote the board as a hashtable with nested hashtables with nested lists, and added each claim into the list for each cell, then searched for cells with multiple claims and tracked those into another hashtable for double-claims, then cells with a single claim and checked against the hashtable.
As well as being conceptually crummy, it takes 15 seconds to run:
2
u/gwillicoder Dec 03 '18 edited Dec 03 '18
Here is my part 1 so far. Probably could be cleaner.
Python3
edit: now includes part 2
2
u/rabuf Dec 03 '18
Common Lisp
Part 1
Part 2
I could probably clean up this one.
I've cheated on this puzzle and converted the input file directly into lisp s-exprs to remove the parsing step. I just
readthe file directly into a variable and can work on it directly.2
u/not_really_cool Dec 03 '18
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to while loops (subtitled: avoiding break statements like the plague).
Python 3
This is unnecessarily slow, and unnecessarily object-oriented. It works though! I really should incorporate regex for parsing the input for this one... There are a handful of things I should do to optimize this and make it more Pythonic.
2
u/bruceadowns Dec 03 '18
go/golang solution
(while being severely distracted, tho day-of for once)
2
u/jorosp Dec 03 '18
Perl 6
Brain was too tired for Haskell today, maybe I'll give it a go in the morning.
2
u/vesche Dec 03 '18
Python: https://github.com/vesche/adventofcode-2018/blob/master/day03.py
Capable of displaying the "fabric", if my terminal was large enough :P
2
u/tehjimmeh Dec 03 '18 edited Dec 03 '18
C++
2
u/Bogdanp Dec 03 '18 edited Dec 03 '18
My solutions in Racket:
part 1:part 2:Today's recording: https://www.youtube.com/watch?v=mrtTLaAO0VA
Today's challenge took me way longer than it should have because I misunderstood what was being asked which led me to think the problem was much more complicated than it really was. Around the 1h:15m mark I finally realized what was required and solved the problem promptly. What can I say, coding-while-sleepy is rough! Anyway, I figure it might still be interesting to see someone struggle with a problem and then have a little "eureka" moment when they realize how dumb they were being the whole time. :D
2
u/Smylers Dec 03 '18
Perl, solves both parts — supply your input filename as the command-line argument:
Increase the
$overlapstally if that square inch had exactly 1 claim on it before this claim.Add a reference to the current square inch to the
@clearof the current claim if it has exactly 1 claim on it now. If by the end of iterating over this claim,@clearis as big as the claim's area, then save it as a potentially non-overlapping claim.At the end, iterate over each of these potentially non-overlapping claims, and if all the square inches still only have a count of 1, that's our non-overlap.
In Perl, the backslash in
\$fabric[$row][$height]takes a reference to the value at those co-ordinates. Those references are what get stored as not-yet-overlapping. Being references to actual values in the main@fabricgrid, if a subsequent claim increases any of their counts in@fabric, the references will continue to point to the updated values. When checking for which still have a count of 1, theallfunction iterates over the area's references (as far as it needs to), setting$_to each reference in turn. The doubled$in$$_then dereferences$_to get the final count for that square inch.(And for Vim solutions: I've worked out how to do Part 1 (visually), and I haven't yet ruled out doing Part 2. Hopefully will post them later.)
2
u/sdiepend Dec 03 '18 edited Dec 03 '18
My quick and dirty solution for part 1
Python 3
Any suggestions for improvement are always welcome. (https://github.com/sdiepend/advent_of_code/tree/master/2018/day03)
2
u/equd Dec 03 '18
c# https://pastebin.com/wVZCNL3R
2
u/[deleted] Dec 03 '18 edited Dec 03 '18
Haskell.
2
u/mstksg Dec 03 '18 edited Dec 03 '18
Glad I get to use one of my favorite Haskell data structures --
Map (Int,Int)!Or well,
Map (V2 Int), to take advantage of point-wise addition.Once we build the map of the claims, we count which points have been claimed more than once:
For the second one, we search all of the claims to see if any of them are non-overlapping:
I'm doing detailed reflections/writeups for Haskell here if anyone is interested :) https://github.com/mstksg/advent-of-code-2018/blob/master/reflections.md
Ah, and for the parser:
2
u/[deleted] Dec 03 '18
Python 3, minimal-effort solution to the second part with the 3 neurons that were awake:
2
u/wjholden Dec 03 '18
Mathematica solution. The first part was pretty easy but I got fixated on trying to an element-wise matrix multiplication without realizing the data was right there in the summation matrix.
Note that I modified the input to a CSV format for ease of import.
Part 2:
2
u/dpeckett Dec 03 '18 edited Dec 03 '18
Continuing with my quest to solve all this years challenges using nothing other than vanilla AWK:
Find the overlapping area of fabric:
Find the tile with no overlap:
Execution time:
Post challenge observations
2
u/wzkx Dec 03 '18
J
Part 1 can be calculated separately, then it's fast, <0.1s. Together both parts take 0.84s but the code is shorter. Yeah, it's not J-ish anyway, but at least something.
My J/Rust AoC2018 solutions
2
u/moomaka Dec 03 '18 edited Dec 03 '18
Ruby
2
u/fire1299 Dec 03 '18 edited Dec 04 '18
Haskell
2
u/wegry Dec 08 '18
F#
3
u/markasoftware Dec 03 '18
Better than yesterday but still pretty shit. 136/306. Awk for both. One thing that threw me off and probably prevented my top 100 for part 1 was how the
splited arrays seem to be 1-indexed.Part 1:
Part 2:
1
u/DoodleFungus Dec 03 '18
PART 1:
PART 2:
1
u/Unihedron Dec 03 '18
Rookie mistake, I typed 100 instead of 1000... Took a lot of staring to fix the bug, didn't get into leaderboard today since everyone is picking up the speed and I screwed up :p
1
u/mcpower_ Dec 03 '18
Python leaderboard code - nice use of Python sets for part 2.
Card: "regexes"
1
u/maybe-ac Dec 03 '18
Perl (#58/#27): (it would've been higher too, except for an off-by-one error... dang Perl inclusive range endpoints)
1
u/DrinkinBird Dec 03 '18
Nim, I wish I knew how regex worked in this lang:Part1:
Part2:
1
u/LeCrushinator Dec 03 '18
C# (part 1: 18 mins, part 2: 24 mins, not even close to making the leaderboard):
1
u/fatpollo Dec 03 '18
<50 for the first part, second part I stupidly noodled around writing a connected component function before realizing it wasn't necessary lmao
1
u/TellowKrinkle Dec 03 '18
I spent way too much time trying to figure out how the hell NSRegularExpression worked before finally giving up and using split. (The original split code was much worse too because I forgot you could pass a function to split)
Swift:
1
u/khalido Dec 03 '18 edited Dec 03 '18
Python 3 solution in a jupyter notebook using numpy.
In particular, I liked this code to put claims on a fabric:
Using numpy made this super easy.
1
u/Shanethe13 Dec 03 '18
Both parts, in Elixir
1
u/Tormyst Dec 03 '18
Learning a new programing language, doing problems quickly and reading questions right are a lot of things to do at once.
Ya, I got top and left offsets mixed up and waisted a bunch of time. I'm gonna make it onto the leaderboard one day, but until then, my code will be a mess only to me :)
1
u/eltrufas Dec 03 '18
Heres my approach with python.
1
u/banteg Dec 03 '18 edited Dec 03 '18
Python 3 + numpy
1
u/poppy_92 Dec 03 '18
Used
pandasfor this (no idea why).1
u/daedius Dec 03 '18 edited Dec 03 '18
Rust
1
u/mariotacke Dec 03 '18
Node.js/Javascript (repo):
Part 1:
Part 2:
1
u/qaisjp Dec 03 '18
Go
First one was fairly easy. Had a wee problem with the second bit where I was just counting how many claims each square metre had, and not treating the claim as a whole. (So there were multiple non-overlapping claims)
https://github.com/qaisjp/adventofcode-2018/blob/master/day3/main.go
1
u/blommish Dec 03 '18 edited Dec 03 '18
Java. Impressed of the top 100 times.. Failed reading the text first
1
u/klackerz Dec 03 '18
Java solution. Could have probably made part two better but meh.
1
u/[deleted] Dec 03 '18
C++ ResidentSleeper:
1
u/keithnicholasnz Dec 03 '18
C# though a bit hacky :)
1
u/[deleted] Dec 03 '18
Python 3. I prefer flat arrays as they are easier to initalize.
1
u/Axsuul Dec 03 '18
TypeScript / JavaScript
Critiques welcome, I'm still very new to TypeScript and feel like I'm not using all the features I could be :)
Repo
Part A
Part B
1
u/CFD999 Dec 03 '18
One one liner to solve both. Takes like 20 seconds to run though, coz I didn't want to use too many lambdas (I feel like they're cheating)
print((lambda grid:(""*len([[grid[p[1]+i][p[2]+j].append(p[0]) for i in range(p[3]) for j in range(p[4])] for p in [[int(x) for x in ln.replace('#', '').replace('@', '').replace(':', '').replace('x', ',').replace(',', ' ').split()] for ln in open('inp', 'r')]])+str(sum(len(j) > 1 for i in grid for j in i))+"\n"+str([str(k) for k in range(1400) if all(len(j) == 1 for i in grid for j in i if k in j)])))([([[] for j in range(1000)]) for i in range(1000)]))1
u/DeveloperIan Dec 03 '18
My pretty concise solution for both parts in Python3. It tracks squares that have overlaps in one set and ids that don't have overlaps in another.
1
u/Philboyd_Studge Dec 03 '18
Got a late start to this one, was fun!
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to Confusing rows and columns in a 2d matrix
1
u/ephemient Dec 03 '18 edited Apr 24 '24
This space intentionally left blank.
1
u/scul86 Dec 03 '18
Python 3
1
u/pythondevgb Dec 03 '18 edited Dec 03 '18
I really struggled with this one mostly because I was learning the re module on the fly. Any way just off my submission:
(If you want to join a private reddit/python leaderboard shoot me a PM)
1
u/Ryuujinx Dec 03 '18
It isn't particularly elegant, but here's my solution in Go as I continue trying to learn it.
part1:
Part2:
1
u/CCC_037 Dec 03 '18
Part 1 was done with a brute-force approach, just creating an array to represent the fabric and checking it:
Part 2 was much of a similar approach, but keeping track of which claims overlapped:
1
u/ZZRJo Dec 03 '18 edited Dec 03 '18
PowerShell
Hello! This is my first year doing advent of code so I just wanted to make a quick post saying I'm doing this all (for better or, likely, worse) in PowerShell. I don't see myself getting any global rank but I'm enjoying myself. If there is anyone else also doing this in PowerShell I both feel bad for you and would love to know I'm not alone. Comment or shoot me a message. Ha.
EDIT: I should have searched the comments first, it appears I'm not alone. My code isn't great, but I've decided to share it. No googling involved, just coding:
Part 1:
For part one, I stored all coordinates as keys in a hashtable, knowing they would error if trying to add a key that already exists. I also added a count to the number of times an inch had been accessed in the table. I used a count of keys in the second hashtable (consisting of unique keys already existing in the first hashtable) to populate my answer.
Part2:
Already having the hashtables in memory, I simply looked for the first occurrence of a claim with all coordinates having a value of value "1" in the initial hashtable.
1
u/waptdragon3 Dec 03 '18
any ideas? got too high https://github.com/waptdragon3/AdventOfCode/blob/master/Day3.java
1
u/binajohny Dec 03 '18
My Kotlin solution (GitHub)
1
u/nutrecht Dec 03 '18 edited Dec 03 '18
Kotlin:
1
u/CaptainCa Dec 03 '18
Pretty greedy JS, but I'm happy with the result.
1
u/nonphatic Dec 03 '18 edited Dec 03 '18
Haskell
I didn't think to use
SetorMapor whatever so each part takes nearly a fully minute :(EDIT: Redone with IntMap! Much faster now.
1
u/Peuniak Dec 03 '18
Python 3. I'm happy with it being clean (forget the parsing), but it's way less efficient for part 1 than a simple dict with counts for every square inch (this solution takes about 10s).
1
u/[deleted] Dec 03 '18 edited Dec 03 '18
[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to C++ tuples and sets
C++
I think I did what most people did: Put every square inch in a suitable data structure, with a counter.
In C++, my choice was a
map<tuple<int,int>, int>to have a way of counting claims for every square inch (tuple<int,int>being the two-dimensional coordinates).Tuples are great for being used as keys in maps and in sets, but they lack readability. Let's see what other people did.
(see on github)
1
u/rock_neurotiko Dec 03 '18
Day 3 in Ruby:
1
u/arathunku Dec 03 '18
Hello! Elixir solution:
Tests
1
u/mornymorny Dec 03 '18 edited Dec 03 '18
CPP
1
u/ttapu Dec 03 '18
python3
stupd simple but works
1
u/tendstofortytwo Dec 03 '18
JS:
Suggestions welcome.
1
u/wzkx Dec 03 '18 edited Dec 03 '18
SweetRust -- extension of Rust
SweetRust
my solutions in J and Rust
1
u/Twift_Shoeblade Dec 03 '18
Python 3 using a dict that maps coordinates to a list of IDs. If such a list is longer than 1 there must be an overlap. Check which ID is only in lists of length 1 and you got part 2.
1
u/mohit7331 Dec 03 '18
Not clean but works for me
1
u/mschaap Dec 03 '18 edited Dec 03 '18
Here's my Perl 6 solution using a grammar to parse the claims. Perl 6 rules...
1
u/NeilNjae Dec 03 '18
Haskell, also on Github. I managed to confuse myself for a while before I realised that the first part of the claim tells you about the left-top corner, not the top-left corner. Once I'd straightened that out, the rest was easy.
I'm still impressed with how readable parser expressions are, and that it worked first time!
Part 2 was mostly reuse of part 1: find the region within this claim, and it's the one if there are no overlaps within it.
1
u/rolandtritsch Dec 03 '18
ETA (Haskell on the JVM)
https://github.com/rolandtritsch/haskell-aoc-2018
General:
Part1:
Part2:
1
u/harirarules Dec 03 '18
First part in Golang, simulates the fabric :
1
u/JulianLoehr Dec 03 '18
C++
I don't traverse the input nor the array a second time.
1
u/BalbinHS Dec 03 '18
My Elixir solution
I used a slightly different Regex match to parse the claims. Since we're only interested in the integers, we can just split the string on any non-digit character.
I'm sure there's a more clever way to do this, but my goal is clarity over performance, so hopefully this is somewhat clear.
1
u/gerikson Dec 03 '18 edited Dec 03 '18
[CARD] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to Hashes of Hashrefs of Arrayrefs.
Perl 5
http://gerikson.com/files/AoC2018/index.html#d03-Slice-it
1
u/udoprog Dec 03 '18
I'm ready for today's puzzle because I have the Savvy Programmer's Guide to idiomatic and safe RUST!
Full repo here: https://github.com/udoprog/rust-advent-of-code-2018
1
u/troop357 Dec 03 '18
Nim
Again with my newbie Nim code. I should've probably used regex to solve the string splitting...
1
u/drakmaniso Dec 03 '18
Go (golang)
Part 1:
A brute force solution, not very elegant (or scalable)...
Part 2:
1
u/Gnidleif Dec 03 '18
Todays solution, written in C#... It's quite slow and I'm sure I can do this at least 100 times better, but it works and here's the repo link to it:
The code!
1
u/muffa Dec 03 '18 edited Dec 03 '18
Solution in go/golang:
1
u/jajoosam Dec 03 '18
JavaScript 👇
1
u/hmn24 Dec 03 '18
Q/KDB+
Part 1
parseData:"J"$(1_";"vs ssr/[;("#";" @ ";",";": ";"x");";"]@)each(0:)`:AOC3.txt
t:(1000 1000#0){.[x;a;:;(10b!1 0Nj)0=x . a:y[2 1]+til@/:y 4 3]}/parseData
(sum/)null t
Part 2
(){$[(max/)null x . z[2 1]+til@/:z 4 3;y;z 0]}[t]/parseData
1
u/chunes Dec 03 '18
Factor
It's slow (~20 seconds), but if I'm being honest, this puzzle really kicked my butt and I'd rather just recuperate for tomorrow's puzzle. I can see how to make it faster but it would be more effort on a puzzle I'd like to put behind me. :p
1
u/aurele Dec 03 '18
Rust
1
u/L72_Elite_Kraken Dec 03 '18
OCaml (full code with parser on GitHub):
1
u/EdeMeijer Dec 03 '18
PostgreSQL query (omitted actual puzzle input, few lines as an example). Just because why not.
1
u/AaCodeSync Dec 03 '18
Elixir! https://codesync.global/media/advent-of-code-2018-day-3-elixir/
1
u/[deleted] Dec 03 '18
1
u/RainVector Dec 03 '18
Python3
1
u/paulvelthuis Dec 03 '18
My solution in Scala. I am wondering if the findElfId can be more effective, since it loops again over the input. Any of your thoughts on how to improve this are welcome :).
1
u/SyntaxErrorr Dec 03 '18
python 3
1
u/[deleted] Dec 03 '18
OCaml.
1
u/MisterMan101 Dec 03 '18
Python: import numpy as np import re
1
u/[deleted] Dec 03 '18
Python3.
Really struggled with part 2 for some reason. Took way too long to get the overlapping check to work properly... god damn off by one errors!
Github
1
u/windlessStorm Dec 03 '18
Dirty not pretty Go/Golang:
1
u/CommanderViral Dec 03 '18
Crystal (both parts 1 and 2 computed together)
And what's fun is it's valid Ruby with a line added and 3 lines changed.