15
votes
Day 2: Password Philosophy
Today's problem description: https://adventofcode.com/2020/day/2
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Using Python and I got a chance to use some regular expressions too!
Day 2 A
For this one I wrote a regular expression and broke up each line into it's component pieces. From there it was a matter of using each of the pieces to check if the password fit.Regular expressions are somewhat cryptic, and if you haven't learned how to deal with them, I highly recommend https://regexone.com/
It does take a bit of work to learn how to use them, but they are excellent at solving certain types of problems
Day 2 B
For this one my regex solution was robust enough that I didn't have to rework it. Though I did rename some of the variables for added readability. I did need to redo the password validation check though.This is basically an XOR logic gate, where it will be true if exactly one of the parameters is TRUE and FALSE if neither or both of the parameters are TRUE. I put a quick one together by starting with 0. Each of the criteria that was TRUE would add +1. If the value at the end was exactly 1, I knew my result was valid. If it was anything other than 1, I knew the result was invalid.
In the code below I separated out 0 and 2 as results for my own debugging purposes, but really the only check you need to make is if it equals 1.
Tips for Beginners
Don't worry about the competition part, just compete against yourself.
Don't be afraid to ask for help. But if you ask for help, try to understand what people are telling you, rather than just copying it
These will get substantially harder as time goes on. The later ones especially will be a challenge even for experienced programmers. Don't beat yourself up if you can't figure them out
Sometimes the hardest part is just getting started. Even getting to the point where you can even start working on the problem takes some effort. Before you even start trying to solve it you need 1)Somewhere to run the code. 2)The problem data in a place you can work with it. I recommend downloading Anaconda and using Jupyter Notebook. Then you can just copy-paste the data into a variable (but you'll need to reformat it or use triple quotes like in my examples
I noticed Day 2 is easily parallelizable so I took a multithreaded approach to the problem:. The trick was to use crossbeam's
scope
function to create a scope that borrowedlines
, so that the borrow checker would be satisfied that its lifetime was long enough to be borrowed into the threads instead of moved. I managed to do this with no dynamic allocations except for the initial reading of the file, which I wasn't sure I would be able to do, so I'm really happy about that.Day 2
Solutions in Rust:
Solution
Took me a second to remember how to do this in pure AWK, but I got there.
Part 1
Part 2
Stealing great formatting ideas from @Bauke and @spit-evil-olive-tips to present my solution.
Part 1
Part 2 diff
Perl solution for part 1 and part 2!
Part 1 & 2
I edited my code slightly from part 1 to create an array from the password string and store each character in the array, then check that the allowed letter was in only one of those positions.My Day 2 Solutions...
Part 1
If I were smart, I would have used Regex to parse the inputs... But just messing with .split() kinda worked out fine...Part 2
The first bit of the code that parses out the values is the same... I guess I could consolidate it all into one script with a parse() function or something if I wanted. I guess the Min and Max variable names don't quite make sense anymore... But that's fine!Formatting sidenote... no matter how many new lines I put here... They don't seem to render out my separate paragraphs separately?
Also, I have this terrible line...
if (var1 == True and var2 == False) or (var1 == False and var2 == True):
It's my equivalent of "or but not and"... Does Python have such an operator?
Any feedback is welcome :)
Yep! This is called exclusive or (XOR) and you can use it with the
^
operator.Python Solutions by using count.
I love that this is possible in python:
a <= b <= c
Part 1
Part 2
Wait, what? That's pretty cool!
I'm using Google Sheets!
Part 1
Part 2
I am so happy I was able to successfully use regex on this one! I started parsing based on character indexes but that got painful quick. Luckily, the bit of regex practice I've been getting at work paid off in spades! Repo is here
Part 1
And it may be a minor victory to the more experienced developers here, but I was super happy I only had to change one function from part 1 to part 2. This was one instance where the compartmentalization used in part 1 ended up saving me tons of time in part 2.
Part 2
After finishing a fairly boring python implementation, I decided to implement this as a digital electronic circuit, described in VHDL. The problem is perfect for this, since solving it needs only a small, fixed amount of state.
The design consists of three main parts:
parser.vhd
verifier.vhd
top.vhd
To actually use the design without having to set it up in hardware and toggling a bunch of switches for hours, there's also a simulation testbench that feeds a file into the circuit character-by-character, adds some framing information, and prints out the final number of verified passwords. VHDL isn't exactly an application programming language, so all the non-synthesizable constructs are a bit weird and quite Ada-y, but it's not actually all that bad.
sim.vhd
Java again. Got tripped up a little on part 2, when I modified my part 1 solution.
Part 1 - 5.44 ms
Part 2 - 34.37ms
MIT scheme solution
Part 1 & 2
I know I'm not a great programmer, but I can usually sort my way through this sort of simple stuff somewhat easily. Then again, is it "easy" if I have to comb through documentation to do it?
Day 2
I was wondering if any c# wizards could offer me some help? I spent hours trying to crack part 2 because I couldn't get a simple evaluation to work and stumbled into the bodge above, which basically does the same thing that i wanted to. BTW, don't get it wrong too much, or you get to wait 10 minutes.
The above statement simply doesn't provide any output, and I know there's data that follows it in the dataset.
The WriteLine() is just a placeholder, but that test doesn't work. My original approach had me
I'm not great at Python and decided this is a chance to finally dive into it a little more. I spent like an hour trying to cleanly install Python 3 on macOS, which is positively kafkaesque. But once it's running, it's quite pleasant how quickly you can get things to work!
Quick question: Is the whole os-path mumbo jumbo really necessary to just load a file in the same folder? It seems odd but I had to paste this in to avoid a "file not found" error.
Part 1+2
Nope! The file path can be absolute or relative. It all depends on which directory you're currently in in your terminal when you run the script. If you
ls
in your terminal and you see both the script you're trying to run and the file you need to read, all you should need to give the script is the name of the file.BUT(!) remember that relative paths are relative to your current working directory in your terminal! So if you're in
Documents
and both your script and input file are inDocuments/code/
, say, and you run the script with the invocationpython code/script.py
, and you specify the input path as just the file name, Python will look for the file inDocuments
, even though it's running the script inDocuments/code/
!I hope this was helpful! If you want to read more it looks like there are loads of blog posts about absolute vs relative paths on the internet! :)
Ah, thanks! I had an advent_of_code folder with each day a subfolder and python ran from the parent folder so I had to add the subfolder name to the filepath.
OK, day 2 felt more straightforward than day 1, even though it involved a lot of regex shenanigans. Here's my answers:
Part 1 runs in 0.0049 seconds
Part 2 runs in 0.0037 seconds
As a follow-up, I was both confused and annoyed to discover that the "or" operator in python is not exclusive. I was beating my head against the wall for a fair while as I tried to fathom out why part 2 was spitting out such a high answer. It feels much more elegant to be able to write
but it seems that's not allowed. Can anybody advise how to get an exclusive or statement to work, because after extensive Googling, I came up with nothing that actually worked. The ^ operator doesn't seem to count for strings, I believe?
^
has a higher precedence than==
oror
. You probably did something like this, accidentally (added parentheses to show ordering):To solve that, you need to do something like this:
Ahhh, that makes a lot of sense. Sort of like the order of operations in maths, right? Thanks!
If you ever want to be able to look it up, it seems to usually be referred to as "operator precedence" for programming. Here's the relevant section in the Python docs: https://docs.python.org/3/reference/expressions.html#operator-precedence
I would be surprised if the
or
operator is exclusive in any programming language. Theor
operator performs a logical disjunction on its operands.Both parts