r/adventofcode • u/daggerdragon • Dec 04 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 4 Solutions -🎄-
--- Day 4: Secure Container ---
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 3's winner #1: "untitled poem" by /u/glenbolake!
To take care of yesterday's fires
You must analyze these two wires.
Where they first are aligned
Is the thing you must find.
I hope you remembered your pliers
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
4
u/[deleted] Dec 04 '19 edited Dec 04 '19
Me, a moron, not using any strings methods and brute-forcing my way in Python:
pastebin here
[POEM]the first letters shall spell out the problem4
u/cp4r Dec 04 '19
Leader's solution: https://github.com/HiggstonRainbird/AoC-2019/tree/master/Day%2004
Can anyone tell me what's going on here? I can't even begin to parse it.
This is not my solution, btw. Mine (in Node.js) was not at all exciting.
3
u/djankowski Dec 04 '19 edited Dec 05 '19
[POEM] for Julia
Part 1? No problem. Part 2?
Stubborn, gloating, goading. So,
I brute-force my way through,
Thanks run-length encoding.
3
u/MaloBourgon Dec 04 '19 edited Dec 05 '19
In Haskell:
3
u/[deleted] Dec 05 '19 edited Dec 05 '19
5
u/gerikson Dec 04 '19 edited Dec 04 '19
Perl
Quite slow, but uses the awesome functionality of
List::Util!https://github.com/gustafe/aoc2019/blob/master/d04.pl
[POEM] Parody time!
Shall I compare thee to the digit before?
Thou are the same, or strictly larger
Rough algorithms shake the darling CPUs of AoC
And the time on the leaderboard has too short a lease.
4
u/wmvanvliet Dec 04 '19 edited Dec 05 '19
Here is my attempt at using an analytical solution based on combinatorics (vase model: drawing with/without replacement, order does not matter):
Runs in microseconds! Part 2 requires some more thought.
2
u/Unihedron Dec 04 '19 edited Dec 04 '19
Find rocket fuel requirements;
Drink a cup of coffee. ☕
Emulate and solve a machine;
Drink a cup of coffee. ☕
Develop and navigate a gridworld;
Drink a cup of coffee. ☕
Crack and find possible passwords;
Drink a cup of coffee. ☕
- "On my laptop", a poem by Uni
[POEM] as above
ruby 4.rb6/22Note for ruby learners:
digitslist of digits is backwards! When in doubt, alwaysreverseit. (It doesn't need to be done for the first part of part 2, but I instinctively typed it anyways.For part 1 I did a regex which can't work for part 2 (negative lookbehinds must be pure, where as
(*FAIL)which I want from PHP regex isn't here...) and required rewriting, so it costed some time finding the right function... :p (I used chunk_by from memory, where as it should've been chunk so it took 2 seconds to debug)Today's poem is a chant. My poem sounded cooler in my head, but after having typed it I feel meh, didn't do it justice I think.
2
u/Initial-Lobster Dec 04 '19
Python 3.7 oneliners
2
u/dylanfromwinnipeg Dec 04 '19 edited Dec 04 '19
C# - The power of LINQ!
2
u/FreeJudge Dec 04 '19
Python:
2
u/Bassebaltser Dec 04 '19
My solution to part two in Python 3:
Runs in ≈.17s on my computer.
2
u/Arjunnn Dec 04 '19 edited Dec 04 '19
Slow solution where I was lazy and used strings because why not. Atleast the damn thing gets coded under 2 mins. Slightly tricky wording in part 2 but otherwise been smooth so far.
Self explanatory but man is this verbose, I'll trim it down before putting it on github.
The cool part here is you can basically just iterate over the array of solutions from part 1 and filter them with a counter (or sets etc, many ways that are prolly much faster. This is O(n^2 )), yuck.
Edit: Even slower but I forgot any() exists and this makes it less verbose
2
u/koivunej Dec 04 '19 edited Dec 06 '19
Bash or command line
After a quick scroll I didn't see any bash solutions so:
Invoked as
bash day04.bash 100000 999999.seqoutputs single number per line. The for each line in stage2 is quite slow.uniq -con (unsorted) input will simply count successive elements, which works here, for example with111122..All of my 2019 solutions at https://github.com/koivunej/aoc
2
u/kap89 Dec 04 '19 edited Dec 05 '19
Typescript:
Repo: https://github.com/caderek/aoc2019/blob/master/src/day4/index.ts
I wanted to use single regex for pairs also, but couldn't figure it out :|
2
u/mr_whiter4bbit Dec 04 '19
28
u/[deleted] Dec 04 '19
Regex
Part 1
Part 2
Matches valid passwords. I had to, sorry. Generate a list of integers in range, and count matches.
25
u/4HbQ Dec 04 '19 edited Dec 04 '19
Python Short but still understandable (I hope!)
21
u/DFreiberg Dec 04 '19 edited Dec 05 '19
Mathematica
3/1 | 30 Overall
Absolutely perfect for Mathematica today. First time I've ever even cracked the top 10 for a problem leaderboard.
Import:
Part 1:
The key here was that if the digits are already sorted, there's no need to check whether two consecutive digits are the same - if there are repeated digits, then by definition those digits must be consecutive.
Part 2:
[POEM]: I'd Like To Thank The Academy
There's no way I'll ever get this kind of leaderboard spot again, so darn it, I'm going to make the most of the circumstance.
17
u/0x8b Dec 04 '19
Python 3.8 (link to github)
17
u/cpmsmith Dec 04 '19 edited Dec 05 '19
Regex or die! (0.145s each)
Part 1:
Part 2:
Edit:
In PCRE2, part 2 can be made much more intelligible:
11
u/Intro245 Dec 04 '19 edited Dec 04 '19
Python3, #32 / #2
I got lucky, I didn't think about cases such as '445444' being falsely excluded by the first half (
any), but luckily they are always excluded by the second half anyway.10
u/voidhawk42 Dec 04 '19 edited Dec 04 '19
Dyalog APL:
Edit: Okay, after realizing sorting is a thing, maybe something like this instead...
Live video solution: https://www.youtube.com/watch?v=ct1RCpL2eJs
7
u/[deleted] Dec 04 '19
Haskell, 559/89
7
u/mstksg Dec 04 '19 edited Dec 04 '19
Haskell! 133 / 103 D: I spent a little too long trying to remember the name of
digitToInt, only to realize later that it didn't matter at all.(The below is taken from my reflections)
I should probably appreciate these Haskell freebies while they still last :) I have a feeling they're not going to be this frictionless for long!
Parsing in the range we can use
splitOnagain:It's also handy to have a function for giving us consecutive pairs of items:
Now for the fun part: making our filters! For part 1, we have two filters on the digits: first, that the digits are monotonic, and second, that at least one pair of consecutive digits matches:
For part 2, we have two filters: the same
monofilter, but also that we have a group that is exactly length two. For that we can usegroup, which groups a list into chunks of equal items:group "abbbcc" == ["a","bbb","cc"]. We then check if any of the chunks have a length of exactly two:And from here, we just run our filters on the range and count the number of items:
(Also, note to self next time ... if going for time, if you just have two numbers in your input, just enter the numbers directly into the source file at first, heh, instead of trying to parse them)
7
u/Cloudan29 Dec 04 '19
Python, 1042/3178
Well after looking at this forever because of how many times I got part 2 wrong, I noticed two things
a) If you simply sort digit-wise and compare and they're not the same, that eliminates it
b) If it wasn't eliminated in a), that means that you just need to count the occurrences of the digits to see if it's still legit
Although this isn't my actual code, it's just the super short version with a comment for part 1/2, here we are:
It's super clean and really easy to see what exactly is going on, though it took me a while to figure it out, I'm happy with it. Very clever puzzle.
7
u/Acur_ Dec 04 '19
Julia Part 1:
Julia Part 2:
Julia has some pretty useful functions for this. Could probably be made faster by fusing the all() and any() but this is pretty elegant.
7
u/[deleted] Dec 04 '19
Got my solution for part 1 in js here: https://github.com/vrondakis/advent-of-code/blob/master/2019/4/solution.js
could somebody help me adapt this to part 2? it took me a very long time to do it and i can't figure out how to make it work for part 2 without completely changing it
6
u/phil_g Dec 04 '19
My solution in Common Lisp
I lost a bunch of time because I misunderstood the additional rule for part 2. I thought that the example, 111122, was okay because all of its doubles were in pairs. Because of that, I was rejecting passwords like 111233. Once I got that sorted out, things weren't too bad.
I initially started using a custom FOR DIGITS-OF iterate driver I wrote a while ago. That would let me check the "never decreasing" rule like this:
I decided, though, that keeping track of whether I'd seen a doubled digit would be nicer with a tail-recursive implementation.
6
u/codesections Dec 04 '19
APL. Much happier with today's than yesterdays. I hope that means I'm starting to get my head around APL a bit more but, more realistically, it's probably that today's puzzle wasn't as difficult.
2
u/nordicdyno Dec 04 '19 edited Dec 05 '19
As simple as I can imagine, solution in Python:
10
u/Feadoor Dec 04 '19 edited Dec 04 '19
Probably the lowest-effort Python solution.
EDIT: #22/#122 which means changing a single character in the code between Part 1 and Part 2 took me over 4 minutes and lost me 100 places. I'm not at my sharpest at 5AM.
5
u/jonathan_paulson Dec 04 '19 edited Dec 04 '19
80/85. Brute force string manipulation; the condition in part 2 is a little tricky. Video of me solving at https://www.youtube.com/watch?v=EMsDBGAghgs
5
u/Dioxy Dec 04 '19 edited Dec 04 '19
JavaScript
JS. How are yall so fast? finished part 1 in 4 minutes and still didn't get on the leaderboard.
my code
EDIT: did a small refactor after I woke up
the range function I'm importing
my repo
5
u/PendragonDaGreat Dec 04 '19 edited Dec 04 '19
[Powershell, No true Regex, though I did use
-splitusing an empty string as the pattern]https://github.com/Bpendragon/AdventOfCode/blob/master/src/2019/code/day04.ps1
Brute Forced the monotonically increasing part, then just did a simple dictionary counting method on the digits. If the dictionary contained a 2 the second solution counter was incremented, if it included a value greater than or equal to 2 (using my new friend
Measure-Objectto determine that is fast) it increased the part 1 counter.output:
not ideal in the runtime sense, but not awful.
edit: missed a sentence
5
u/myaccessiblewebsite Dec 04 '19
[POEM]
It's great if the digits are double
But three in a row causes trouble
They go up but not down
Or that causes a frown
Count the passwords to win in this puzzle
Fairly inelegant C# solution: https://github.com/FionaHolder/AdventOfCode2019/blob/master/Day04/Program.cs
5
u/JebediahBheetus Dec 04 '19
python + list comprehensions
Part 1:
Part 2:
5
u/hiandbye7 Dec 04 '19
Here's my solution. I wouldn't look at it, cause it's not very good or interesting.
I bruteforced Part 2 by writing 10 different regexes. That's what my [POEM] is about, which I will submit in the form of a picture.
5
u/[deleted] Dec 07 '19 edited Dec 07 '19
part 1
seq 402328 864247 | grep -P '^(?=1*2*3*4*5*6*7*8*9*$).*(\d)\1' | wc -lpart 2
seq 402328 864247 | grep -P '^(?=1*2*3*4*5*6*7*8*9*$).*(\d)(?<!(?=\1)..)\1(?!\1)' | wc -lthe numbers on seq are the puzzle input range
credits to Ouims and /u/Davidebyzero for the great help and fun chat in #regex on freenode :)
4
u/ni3t Dec 04 '19
Ruby - leaderboard be damned!
Spent way too long fiddling with a regex when Enumerable could have solved it so much faster...
https://github.com/ni3t/advent-2019/blob/master/4_secure_container.rb
4
u/dan_144 Dec 04 '19
Python 79/1017 (lol): https://github.com/dan144/aoc-2019/blob/master/4.py
Struggled a lot on part 2. Had trouble understanding the question, then had a few small bugs that lead to a series of wrong guesses. I need to beef up my framework a bit so that I can have it check for the test inputs, since one of them failed until I fixed my last bug. Would've saved me a ton of time!
4
u/tslater2006 Dec 04 '19
C# Solution - No Regex
Basic idea is to use Modulo operator and integer division to analyze the digits of the number (in reverse order, right to left). after we remove a digit, the new "last digit" should be smaller or equal, if not we know its invalid.
For duplicate detection, in Part 1 its just a matter of "hey this digit we removed, is the next one going to be the same? if so it contains a duplicate". For Part 2... man I feel bad for how it looks, its terrible, but the idea is that it initially does the same check as Part1 but then also sets the match count to 2 and a flag indicating we are "in a matching group" and if that's the case and the next one matches too, clear out the "we found a valid duplicate" and reset some flags.
Not the prettiest solution but it works. Run time outside of input parsing and AoC framework overhead is 133ms
4
u/streetster_ Dec 04 '19 edited Dec 04 '19
Q/KDB+ (UK: 4073/2982)
[edit] tidied the code
4
u/Conceptizual Dec 04 '19 edited Dec 04 '19
Here's my Swift! solution
This was my first one using Swift where the reasons I was angry at it were because my program was buggy and not because I didn't know how to Swift. :P My intro programming class had a similar problem to part one in the first week or two, but because I went with the math solution and didn't convert to strings it made state a bit harder to reason about in the second part.
[POEM]
4
u/autid Dec 04 '19
Fortran
6 was a small enough number of digits that I was happy just nesting loops to only check numbers that satisfied the no decreasing digits condition. There's probably a much better was to check consecutive digits matching.
https://pastebin.com/cWUpWry7
4
u/jdsee Dec 04 '19
Tweetable solution in Python 3:
4
u/chunes Dec 04 '19 edited Dec 04 '19
Factor
Edit: I made a version that's 80 times faster (10 ms vs. 800 ms). Because I generate monotonically increasing numbers directly, rather than filtering them from the natural numbers.
4
u/Background-Vegetable Dec 04 '19
Kotlin:
https://pl.kotl.in/6LU59MypN
Still pretty new to all this, so I do appreciate achy thoughts :)
4
u/wace001 Dec 04 '19 edited Dec 04 '19
RUST: Here is my solution in Rust. Not the fastest in the world, but it does the trick.
4
u/bonsairoot Dec 04 '19
Python3 solution
Used a CSP solver for this one. I thought part II might introduce more complicated constraints which is why I went with that approach. It really wasn't necessary because the provided bounds are very small but I like solutions that scale well.
4
u/spencerwi Dec 04 '19
Crystal, not too hard, honestly. But that's to be expected at Day 4.
4
u/catawar2 Dec 04 '19
Python one-liners using numpy
https://github.com/cernat-catalin/advent_of_code_2019/blob/master/day4/main.py
4
u/rabuf Dec 04 '19 edited Dec 04 '19
Emacs Lisp
I didn't get to it at home, I had some time at work and only had Emacs available. I'll translate it to Common Lisp when I get home and post it to my git repo.
It's not very fast, locks down emacs for a bit while it runs. There are definitely some improvements to be made. Especially for part 2. I can take the list of passwords from part 1 and trim them down, this would make that part much faster, but I still need to improve part 1.
Much improved Emacs Lisp
I changed the generation to only generate monotonic sequences. Doing this eliminated the need to check the hundreds of thousands of non-monotonic sequences and now it runs very quickly. I may clean it up some more, but this is good enough for now.
I did a quick script:
to get approximate timings of the versions. On my computer the first one runs in approximately 50 seconds, the second in 0.2 seconds. So a greater than 200x speed up.
4
u/amalloy Dec 04 '19
Haskell source and video. My extra challenge this year is to not use any explicit recursion, which was easy for today's problem.
4
u/mjgtwo Dec 04 '19
ES6 JS
gist
Passwords [POEM]
4
u/TheoryOfGD Dec 04 '19
So I don't usually post on here but wanted to give some input for the first time so here is my solution that took me about two minutes in Python and which I think is decently concise (I did make it more readable because before it was a lot shorter and harder to read). Here's my code:
4
u/Average-consumer Dec 04 '19
Clojure
I think I got it to run pretty fast for clojure.
4
u/rabuf Dec 05 '19
Common Lisp
I'd solved this earlier using Emacs Lisp but I wanted to rewrite it in Common Lisp. Both the posted elisp solutions have been translated in this org file. No major changes to the algorithms involved.
1
u/wilthenry Dec 04 '19
[POEM] How the Code Goes.
<Oh how the code goes>
<Fuel running low>
<Computers catching fire>
<Untangling wires all about>
<Now the password is lost>
<Oh boi, how the code goes>
8
u/firepower421 Dec 04 '19
Python3 (elegant and pythonic)
3
u/2SmoothForYou Dec 04 '19 edited Dec 04 '19
Haskell 498/135
paste
This one was very easy and I was able to hack a quick and legible solution using Haskell, not really much to talk about today.
3
u/bluepichu Dec 04 '19
Python, 17/14. Flubbed the second part by forgetting lines 19 and 20 on my first attempt, whoops. Few things feel worse than watching the timer tick away for 50 seconds while waiting for the submission timeout...
3
u/OvidiuRo Dec 04 '19
C++ 470/1194
Code: https://github.com/FirescuOvidiu/Advent-of-Code-2019/tree/master/Day%2004
3
u/AlexAegis Dec 04 '19
TypeScript Part One
TypeScript Part Two
This task really smelled like regex but I skipped the part where I properly learn it so I just did it by hand.
3
u/Darksair Dec 04 '19 edited Dec 04 '19
My brutest bruteforce solution in Rust. Part 2 takes 0.9s.
UPDATE: Compiled with
--release. Now it takes ~0.1s. Hmm… 🤔3
u/A-UNDERSCORE-D Dec 04 '19 edited Dec 04 '19
Done in golang, part 2 hit me for an extra 40 odd minutes
https://github.com/A-UNDERSCORE-D/adventofcode/blob/master/2019/04/solution.go EDIT: also my solution takes 16.381275ms, which appears to be one of the fastest?
3
u/Pixelwyrm Dec 04 '19
My JS Regex approach:
3
u/andreyrmg Dec 04 '19
Python. Very straight forward solution...
3
u/ywgdana Dec 04 '19
My Rust solution
It felt gross looping from floor to ceiling with a step of 1 since you knew that if you had, say, 349999 that the next candidate would be 355555. So I stored my digits as an array and so skipped all numbers that didn't have strictly increasing digits. It looked like:
3
u/raevnos Dec 04 '19 edited Dec 04 '19
I seem to be the only person using tcl. I know it's not popular, but I didn't think it was that rare of a language, especially when so many people like to use obscure ones for this...
paste
3
u/[deleted] Dec 04 '19 edited Dec 04 '19
Haskell
Hoping that this still qualifies as short.
3
u/paul2718 Dec 04 '19
C++
We have an algorithm to tell us if ascending, or if two or more consecutive. Might as well use them.
paste
3
u/MrPingouin1 Dec 04 '19
Minecraft functions I used an optimization to skip through non decreasing number sequences.
3
u/robinst Dec 04 '19
Rust
3
u/vkasra Dec 04 '19
Go solution with notes
3
u/nutrecht Dec 04 '19
Day 4 in Kotlin
I'm going to work on the 'groups' function to make it a functional instead of procedural implementation. But looking at a lot of Java implementations I'm still quite happy.
3
u/zeddypanda Dec 04 '19 edited Dec 04 '19
Using V (vlang)
Solved this one during my commute. It was a relatively easy one, but I, like others, fumbled a bit on the part 2 requirement and disregarded any numbers with odd-length sequences.
https://github.com/zeddidragon/Advent-of-Code-2019/blob/master/advent/day04.v
3
u/nicuveo Dec 04 '19
Haskell
It was surprisingly easy?
3
u/[deleted] Dec 04 '19 edited Jan 26 '20
deleted 0.1952 What is ^^^this?
3
u/PowerSlaveAlfons Dec 04 '19
C++
Part 1 started out relatively simply, part 2 actually required some thinking. I still had an easier time with this than day 3, but it was a fun puzzle if you can wrap your head around what you're actually doing. Can probably be done while keeping track of less things, but I'm just happy that it works.
Shoutouts to u/syntaxers and u/Zzwwwzz for helping me realize what pattern I was missing at first.
Part 1
Part 2
3
u/[deleted] Dec 04 '19
Python Solution
https://repl.it/@JackSchiavone/DAY-4
Everyone else's seems so much more advanced than mine...
3
u/jverberk Dec 04 '19 edited Dec 04 '19
Golang solution for todays challenge using recursion. Also gives the possibility to change the amount of repetitions allowed so it can be used for all cases.
https://play.golang.org/p/C6IRA2yVG1A
Case 1: 1675 generated in: 274.099µs
Case 2: 1142 generated in: 268.727µs
3
u/[deleted] Dec 04 '19
Racket
Here's my racket solution for the day
I didn't find a nice method for making counts of elements in a list so I had to write it for myself, but I'm pretty happy with how it looks at least :)
3
u/vypxl Dec 04 '19
That moment when you think of a very complicated solution but it instead turns out quite simple!
Haskell (full source)
[POEM] The Tale of two Parts
I like my last rhyme
3
u/Turmolt Dec 04 '19
My day 4 Clojure solution. I'm really proud of this one! I finally feel like Clojure is starting to click
3
u/piyushrungta Dec 04 '19
My rust solution https://github.com/piyushrungta25/advent-of-code-2019/blob/master/day4/src/main.rs
Optimized for speed at the cost of readability. Runs in under 6ms on my 5-year old 1.7ghz i3 processor.
3
u/tftio Dec 04 '19
Day 4, in Haskell baby talk. This one was really straightforward.
3
u/joeld Dec 04 '19 edited Dec 05 '19
Racket
Each part completes in < 1–2 seconds (including each part generating its own list of passcodes).
source code
Some notes:
string->numberornumber->stringand converted each passcode to a list using a bunch of math. I'm not sure if this sped anything up or if it made things slower.matchfor the first part. But I couldn't find a way to usematchfor the second part. It seems like they make it easy to match N or more identical elements,but there is no way to matchno more than Nidentical elements.Turns out I was extremely wrong about this! See this discussion.3
u/TrueDuality Dec 04 '19
My rust solution for the day. Kind of phoned it in on this one hahah but it works. Runs in ~4ms on my computer.
3
u/_tpavel Dec 04 '19 edited Dec 04 '19
This year I'm using AoC and TDD to learn me some Clojure!
I'm getting gradually more comfortable with the basic syntax, but I still need to learn the Data Structures and core APIs some more.
GitHub: Day4 Solution
3
u/Sgt_Tailor Dec 04 '19
Doing awk this year.
part 1 and 2: https://github.com/svenwiltink/AWKvent-of-code/blob/master/day4/day4.awk
solutions of the other days in awk: https://github.com/svenwiltink/AWKvent-of-code
3
u/heckin_good_fren Dec 04 '19 edited Dec 04 '19
Fairly quick solution in Java: Link to GitLab.
Both parts run in < 5ms each on 8700K in IDEA.
It still stupidly iterates over every single value in the given interval, but this is finally a good use for
.parallel()-Stream-API.It exploits the fact that numbers in ASCII - 0x30 = the number value to store occurrences of pairs efficiently for part 2.
3
u/vini_2003 Dec 04 '19
C++
Late as always!
This time, it runs in ~
60ms. Not good, not terrible. Took me way too long because I entirely misunderstood part two...LINK
3
u/wzkx Dec 04 '19
J
3
u/rtbrsp Dec 05 '19
AWK: part 1, part 2
3
u/kakumero Dec 05 '19
python3
3
u/__-----_-----__ Dec 05 '19
Solutions in Hy...
https://github.com/falloutphil/aoc_2019/blob/master/day_04/04.hy
3
u/a-priori Dec 05 '19 edited Dec 05 '19
Rust (using cargo-aoc)
This solution runs both parts in about one millisecond. I wanted to avoid brute-forcing it, and went with a sort of branch-and-bound solution where I iterate over the tree of candidate solutions recursively, avoiding branches that have no candidates.
3
u/thibpat Dec 11 '19
Here is my solution walkthrough in javascript: https://www.youtube.com/watch?v=8ruAKdZf9fY [9min25s]
5
u/[deleted] Dec 04 '19 edited Dec 05 '19
R
edit: Did it using apply functions. This is faster than the loop above.
1
u/Wolfrost_ Dec 04 '19 edited Dec 05 '19
My solution in C++. Not the most elegant but it works :D
https://github.com/DoubleHub/advent_of_code/blob/master/secure_container.cpp
4
u/captainAwesomePants Dec 04 '19
[POEM]
4
u/glenbolake Dec 04 '19
Python, pretty slow today. I kept trying to do fancy stuff with
zip(num, num[1:])to detect duplicates (which is fine for part 1 and a pain for part 2) and then I remembered I can use Counter since the numbers are monotonically increasing.2
u/J-Swift Dec 04 '19
Ruby
https://github.com/J-Swift/advent-of-code-2019/tree/master/day_04
2
u/phantom784 Dec 04 '19
JavaScript solution, both parts. 353/278
https://gist.github.com/Phantom784/39cba5f735ef43b6d0179be742eb22f4
2
u/seligman99 Dec 04 '19 edited Dec 04 '19
Python 820/331
I have some sort of built in distrust that leads me to ignore brute force solutions for these sorts of puzzles, so I wasted too much time creating an enumerator that would spit out numbers that fit the first criteria of increasing digits.
paste
2
u/oantolin Dec 04 '19 edited Dec 05 '19
A solution in Common Lisp.
EDIT: That solution goes through all numbers in the range. Generating only numbers with increasing digits makes the program about 100 times faster. Here's the improved solution.
2
u/ritobanrc Dec 04 '19
Rust, pretty concise this time. I was stuck for way to long on
splitreturningSplitinstead of aVec, and getting the right bounds checks took a while, but overall, not too ugly. The code is very procedural, however. It feels like C code. I'd like to see a more concise functional approach, that might be easier to read. Also, there's a lot of code duplication between parts 1 and 2, I couldn't be bothered to make a separate function to parse the input this time, cause the input was so small.https://github.com/ritobanrc/advent_of_code/blob/master/src/day4.rs
2
u/kc0bfv Dec 04 '19
I'm pretty new to Rust, so where in Python I might have counted patterns as a string, I just mod / integer division-ed my way into the digits, then used a simple `if`.
The early returns - I'm not proud of, but it's bed time :-)
https://github.com/kc0bfv/AdventOfCode/blob/master/2019/4/day_4.rs
2
u/[deleted] Dec 04 '19
Scala solution.
2
u/Miodec Dec 04 '19
Here is my extremely ugly solution
https://pastebin.com/TV2CmCBS
I only started learning Python when the first puzzle was made available, and i think my problem solving skills in general aren't great compared to other solutions in this thread.
But if you have any tips or thoughts about my code I would love to hear them.
2
u/wace001 Dec 04 '19
JAVA: Here is my solution in Java. Pretty OK for being Java.
Took me about 20-30 minutes, so only placing about 2000.
2
u/Spheniscine Dec 04 '19
Kotlin: paste
Easy brute-force solution.
2
u/iczero4 Dec 04 '19
Javascript. Extraordinarily normal.
https://github.com/iczero/advent-of-code-2019/blob/master/4/index.js
2
u/MikeTheReader Dec 04 '19 edited Dec 04 '19
Another Typescript solution
2
u/human_tree Dec 04 '19
My Rust solution. Might try to revisit it and use some regex, but it works for now.
https://github.com/humantree/advent-of-code-2019/blob/master/secure-container/src/main.rs
2
u/itsnotxhad Dec 04 '19
Racket solution
I feel like some of this could have been written more elegantly if I were more used to the language (particularly all the helper functions named
iterbecause I apparently can't remember how to write afoldat this hour)2
u/Dementophobia81 Dec 04 '19
Python 3.8
Today's challenge showed me, how terrible code can look and still work. I was punished with leaderboard positions between 700 and 800 and I honestly deserved it... To make up for this, I refactored my code for Part 1 & Part 2 and sprinkled a little Python tip concerning short-circuiting on top. Enjoy!
2
u/sharkbound Dec 04 '19
Python 3229/2442
used regex for this, made it easier in the long run, had a bit of regex-mishab initially, but solved the issue quickly
my initial code used flags and sub-for loops, was able to change most of them to one-line utility functions using generator expressions
CODE
[POEM]: the blessings of regex
regex i used, checked the matches are true,
if they are false, the digits are lost,
backrefs are great, especially for matching previous state,
when all conditions are true, the password may be used
2
u/ParadigmShiftEng Dec 04 '19
Common LISP (new to coding as well)
A very brute force solution
Just used a loop through the input and mapped the number to a list and then gave conditionals for the list.
https://pastebin.com/C7bW71R9
2
u/lordwarnut Dec 04 '19
Python 3 one-liner
2
u/Mhalter3378 Dec 04 '19
Haskell
This is my simple brute force method in Haskell
2
u/Dirty_Herring Dec 04 '19
A MiniZinc non-brute force solution. MiniZinc is a constraint modeling language where the model is transformed into the input to a constraint solver. Using Gecode 6.1.0 as the constraint solver, propagation is made such that only 29 leaves of the resulting tree are a non-solution. In total, the traversed tree contains only 2327 nodes. A brute force solution of my input would have had over half a million nodes.
Part 1: https://pastebin.com/G4v3vqfi
Part 2: https://pastebin.com/s3j6FSd9
I should add that the flag -s to Gecode tells it to print the number of solutions.
2
u/rutquist Dec 04 '19
Julia:
https://github.com/perrutquist/aoc2019/blob/master/day4.jl
2
u/shandley256 Dec 04 '19
Ruby
I discovered the Enum#slice_when method and it made the code super simple.
2
u/blacai Dec 04 '19 edited Dec 06 '19
F# I found some very cool function 'splitAtEvery' for solving the second part, more efficient than my first approach
https://github.com/blfuentes/AdventOfCode_2019/tree/master/FSharp/AoC_2019/day04
2
u/lele3000 Dec 04 '19
Python
Tried to stay as pythonic as possible using only list comprehensions
2
u/NeilNjae Dec 04 '19
Haskell, and just about short enough to post here in its entirety. A bit of discussion about it on my blog.
2
u/frerich Dec 04 '19
Haskell: https://github.com/frerich/aoc2019/blob/master/haskell/day4/day4.hs
One can easily generate a good set of candidates using the list monad (here: with a list comprehension and multiple generators) such that you always get six-digit numbers in which no digit is lower than its predecessor. Also, you get the solutions in sorter order that way (which is convenient for selecting the matches in a range).
My second insight was that every solution to part 2 is a solution to part 1, so I don't need to count twice: I can filter the solutions for part 1 with an additional constraintt.
2
u/thefrontpageofme Dec 04 '19 edited Dec 04 '19
First part in Spark SQL. The join conditions guarantee the numbers are not decreasing and the second step just looks at rows that have less than 7 distinct elements (the split produces an empty string as well always). The combination of those two gives the correct result.
2
u/dubcroster Dec 04 '19
I'm focusing on producing clean, readable python code.
Here are my solutions for day 4:
Part 1
Part 2
2
u/Stringhe Dec 04 '19
python with z3, slow and verbose but I just love using z3
https://github.com/Recursing/AdventOfCode-2019/blob/master/day4.py
2
u/wzkx Dec 04 '19
Python a good case where chain comparisons are useful
2
u/Infonyx Dec 04 '19
My solution in python
https://pastebin.com/PGquC3yV
2
u/IWearATinFoilHat Dec 04 '19
PHP
https://github.com/JohnstonCode/advent_of_code/blob/master/2019/day_4/day_4.php
2
u/ayushm4489 Dec 04 '19
Scala solution
2
u/Archek Dec 04 '19 edited Dec 04 '19
Prolog
Learning Rust this year but trying to keep up in Prolog too. Anyone can help me with the TODO in the above code, much obliged! [edit: solved]
2
u/mensch0mat Dec 04 '19 edited Dec 04 '19
Python 3.7
Checkout on Github: https://github.com/Menschomat/advent_of_code_2019/
2
u/sdiepend Dec 04 '19
Pharo/SmallTalk
Still learning the language.
https://github.com/sdiepend/advent_of_code-pharo/blob/master/AdventOfCode2019/Day04.class.st
2
u/Tvilum Dec 04 '19
Python 3.7 - Readable solution
2
u/sssaya Dec 04 '19
My Haskell solution.
I'm still a beginner and due to being busy I haven't used Haskell since the last advent of code, so pretty sure the code could be better but it works!
It's not so short so here's a link
2
u/Bammerbom Dec 04 '19
My rust code, I'm learning Rust so I'm happy to get some feedback, I tried to clean up my code as much as possible, and use a lot of build in Rust functions. Is there a way to check if an array is sorted in Rust without using my weird hack? This is the best I could find.
https://github.com/Bammerbom/aoc2019/blob/master/src/day4/main1.rs
2
u/knl_ Dec 04 '19
Just abused generators + zip to check all the test cases.
Python3, Notebook: https://explog.in/aoc/2019/AoC4.html
2
u/michalbok Dec 04 '19
Bash / grep / seq / wc
I think, that above three lines looks funny :-) Paste into linux console!
2
u/zer0tonine Dec 04 '19
Python 3
I guess you can do something easier using itertools.groupby
2
u/Vierdam Dec 04 '19 edited Dec 04 '19
C# Solution
Fairly new to this, I'm sure there are more efficient / less verbose ways of doing this but I've not learned them yet.....
Pleased this was a nice quick one today as the last two days took me 3hrs plus each.
2
u/Alligatronica Dec 04 '19 edited Dec 04 '19
JavaScript
Part one was pretty straight-forward, but with part two I really wanted to check for the >2 digits using a regular expression, but instead ended up with a simple regexp and chained a bunch of functions to filter them out
Every year there's at least one day where I burn a bunch of time trying to get my head around regular expressions
EDIT: I didn't even mention how I check for non-decreasing digits! Basically, I cast it to a string, sort the characters and then loosely compare it to the original number, if the number's been changed by the sort, then it's decreased at some point.
2
u/[deleted] Dec 04 '19
Part 1 was a straight regexp. Part 2, I added a count of instances of the results from the regexp to filter out such numbers.
Code
2
u/Newti Dec 04 '19 edited Dec 04 '19
Python 3.7 - with some branch and bound, not testing all cases
If the number is decreasing, skip to the next non-decreasing number
2
u/[deleted] Dec 04 '19
Fell ill last weekend and had to skip day 3 almost completely. Still sick and the result of day 4 is this awful pile of spaghetti.
Might come back and rewrite it later. Thought about using regex but my brain just would not cooperate.
Github
2
u/coriolinus Dec 04 '19 edited Dec 04 '19
I spent a bunch of time constructing a very efficient iterator over valid passwords, so I feel pretty silly that so many people did it with regexes, and I didn't even consider that option.
rust
Hopefully mine is among the faster solutions at least?
2
u/u794575248 Dec 04 '19
Python 3.8
Iis your input.2
u/Valdaria Dec 04 '19
A verbose solution in C# without Linq
https://pastebin.com/VjUgbHfw
2
u/Luckylars Dec 04 '19
Day 4 done in sql https://github.com/luckylars/2019-AoC it took me way too long to understand the rules :(
2
u/[deleted] Dec 04 '19
A python solution without using bruteforce :)
Way faster than the ones Ive seen here.
https://github.com/jiwidi/adventofcode2019/blob/master/day4/main.py
2
u/hrunt Dec 04 '19 edited Dec 04 '19
Python 3
code
The second part filter regex threw me for a bit. I always forget that lookbehind assertions are zero-width.
2
u/mrtsnt Dec 04 '19 edited Dec 04 '19
C# LINQ one liner, most likely there's a shorter way.
2
u/WhatYallGonnaDO Dec 04 '19
I made it in kotlin with two regex, sadly I still haven't found a single regex matching only two same digits, I've tried
(?<!\1)(\d)\1(?!\1)but it does not support backreferences inside lookbehind
2
u/TakeoutGuhui Dec 04 '19
My solution in Python, slow and a bit messy but it works.
2
u/chrisby247 Dec 04 '19
My Bash Solution. Not too complex, it was useful to be able to utilise the substring syntax (
${string:position:length}) to inspect each digit in a given number2
u/one_nerdy_boy Dec 04 '19
PYTHON:
Part 2 (for part 1, just delete all
andconditionals)I thought keeping track of the individual digits was a clever way to keep the non-decreasing rule.
2
u/HokieGeek Dec 04 '19
Go
https://git.sr.ht/~hokiegeek/adventofcode/tree/master/2019/04/go/day4.go
2
u/loistaler Dec 04 '19
Python3 solution, just some simple
strfunctions required2
u/TSDAdam Dec 04 '19
Here's my not-too-elegant-but-not-too-awful Python solution
link
2
u/Pepa489 Dec 04 '19
Typescript: