Oh hey! It's a problem related to my current job! Let's try to brute force some passwords like real l337 h4xx0rz.
Day 4 - The Problem
M...
For further actions, you may consider blocking this person and/or reporting abuse
Regex to the rescue!
Just to make it clear:
([^1]|^)11([^1]|$)
says:([^1]
find anything other than 1|
or^)
the beginning of the string. The parentheses create a capture group that allows the use of|
to mean "or".11
find "11"([^1]
find anything other than 1|
or$)
the end of the string.Essentially this searches for "11" surrounded by anything that isn't a 1. Then I repeated it for the other digits.
Nice, although I wouldn't inflict that on my team-mates!
Even I, someone who loves regexes so much that I made a regex that converted words to Pig Latin, find this hard to read!
It makes sense! I just worry that if I look away from it for too long it will have changed...
It’s still more readable than the regex that “matches a b c where a+b=c” (a,b,c being any rational number)
Wow. ._.
It is a bit of a monster.
I've being trying to think of a way to refine it, but then again I'm not likely to be asked to find numbers like that outside of AoC :D
Wow, nice solution.
Interesting, I used the same regex for my part 1 too
Nice solution. I've not used rust, but I hear it's really fast!
Kotlin Solution
It's important to use
Sequence
here so that the map/filter/count function can happen per item instead of applying to the whole list for each operation.Where in
part1()
are you checking that 2 adjacent digits are the same?Here’s my math/logic for n=3 that expands to a general case simply in my mind (if it doesn’t I can work on a more formal proof)
Given:
a<=b<=c
a==b
ORb==c
Therefore:
distinct(a,b,c)
must be equal to(a,c)
OR(a)
aba
is never a legal patternExplanation of the second therefore: By looking at a 4 digit number that is
abca
we see that since this impliesc<=a<=b
that this isn’t a legal string. We can also insert any number of digits (that follow the rules) betweenb
andc
and the contradiction with given #1 still occurs.Something about seeing so many greens <3
Waiting for someone to post a functional/not-very-imperative JS solution!
Back again with more Clojure. Cool thing to point out on this one: the
frequencies
function. It takes as input a collection, and then returns a map of the values present in that collection, and the count of the times they appear. Made part two real easy.repl.it: repl.it/@ballpointcarrot/AoC-Clojure
github: github.com/ballpointcarrot/AoC/blo...
I had to write my own in kotlin! Their design philosophy states that GroupBy->MapValues(Count) is good enough, and I... kind of agree?
Still handy to do it in one step though.
I'd agree that it's not strictly necessary, as
(group-by identity "112233")
would solve the same problem; however, if you give me a convenience function, I'll take it. :DMy JavaScript solution:
Threw a Ruby solution together, just because it feels under-represented here. Ended up faster than the Clojure solution, probably because I'm doing the processing for the values as they're generated:
Swift Solution here. By reading the statement I felt easy but took almost same time as yesterday to solve this as well.
Brute force Python solution:
For me, this was 100x easier than day 3.
The first part was pretty easy and in the second part I was trying to construct a perfect regex initially so that didn't work out. One thing that stumped me a bit was that in rust
string.split("")
throws a""
at the beginning and the end.I haven't completed Day 3 yet, gonna do that later. I figured out the logic for Day 3 but there's some problem with the implementation.
I've uploaded all my Advent of Code 2019 solutions in rust on a github repo : github.com/coolshaurya/advent_of_c...
Day 3 solution using Rust
It's not as good as I wanted it to be, am pretty sure it was possible to solve in a nicer way with regex. But had to do it quickly between meetings so 🤷♀️ Am satisfied though
My motto for today is: it's not stupid if it works. I didn't feel like fighting with Rust over "whose characters belong to whom" and "you can't borrow that" and "don't add strings together". Don't judge me.
Haskell solution
My solutions in Elixir. I'm just posting the final solution for part two since that essentially contains the solution for part one as well. A very nice day to use the power of pattern-matching!
JavaScript...thank goodness I got this one! Still struggling to work out day 3, the mapping ones always get me stuck...I'll get it soon, though!
Basically, I filled out all the digits in my input range, then checked through each number to see if it was in order & had at least one double. Tried to break the second I saw an out of order number, so it would be a bit faster. Thankfully each input number is small, so it ran pretty quickly!
Here's mine. Lot of room for DRY-ing it and making it declarative:
Back in the early 1990s in my CS degree we did a genetic algorithm polynomial solver in Prolog. My Prolog has become a bit rusty through disuse since then but I was hoping for a constraint solving problem to dust it off.
Later I might try again using one of my MiniKanren implementations (e.g. github.com/neilgall/KotlinKanren)
My solution in JavaScript, with some regex magic.
For the second part, replace
hasGroupOfTwo
withhasGroupOfOnlyTwo
inisCandidate
.Check out my solutions at github.com/MaxArt2501/advent-of-co...
Part 2 solution
not a fan of my
has_exactly_two_adjacent
method but, hey, it works ¯\_(ツ)_/¯my part 1's
has_two_adjacent
method was a lot nicer:PHP again! I solved it within the first half hour but was too embarrassed by my code to post it here, so spent most of my evening figuring out how to make it look less bad, oops. And I overwrote my Part 1 code, so this is just Part 2:
I did take some inspiration from other solutions to make this improved version :)
These are fine instincts that will serve you well in future pull requests.
No shame in making sure all the corners are sharp and you replaced all the variables named
x
,fart
,someKindaFunc
, etc. (I must admit to editing my posted code after an elegant refactor reveals itself to me)Kotlin code, used
groupBy
for the double digit condition checks - github.com/sup95/AdventOfCode-19/b... :)Unsolicited code review:
filter(x).isEmpty()
is equivalent tonone(x)
filter(x).isNotEmpty()
is equivalent toany(x)
filterNot(x).isEmpty()
is equivalent toall(x)
filterNot(x).isNotEmpty()
is equivalent toany(!x)
c.any{it == x}
is equivalent tox in c
I find future me tends to remember what I was doing faster when I use the any/none/all functions. (They also short-circuit, but the readability boost to my productivity helps more than the loop optimization)
I wrote my solution in Zig this time. Funny experiment. Code is a bit ugly and for sure suboptimal, as it usually happens when you try out a language for the first time:
Here's my Ruby attempt for part 1
I was trying to avoid iterating through every number but it seems a little complex
This time I went to bed early and woke up at 4am to do the problem. Oddly, I think that will work better for me! And I found this problem much easier than Day 3.
I had the same issue with the wording of Part Two, so thank you for the interpretive hint.
I have a tendency to write very low-level implementations (without including libs)...partly because I'm not all that familiar with what's available in the Python ecosystem, and partly because I kind of like controlling the nitty gritty!
here is my python solution (basically a one-liner):
part1:
part2:
a bit shorter:
A javascript walkthrough:
Solution in Clojure:
Day 4