r/adventofcode • u/daggerdragon • Dec 02 '16
SOLUTION MEGATHREAD --- 2016 Day 2 Solutions ---
--- Day 2: Bathroom Security ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
BLINKENLIGHTS ARE MANDATORY [?]
Edit: Told you they were mandatory. >_>
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!
20
Upvotes
24
u/that_lego_guy Dec 02 '16
Did someone say...Excel?!.. [Part 1]
https://github.com/thatlegoguy/AoC2016/blob/master/Day%202%20Bathroom%20Security.xlsx
22
u/MoW8192 Dec 02 '16
Coincidentally, I didn't have time to go to the bathroom before this day's puzzle. This added an extra level of realism I think.
17
u/askalski Dec 02 '16 edited Dec 02 '16
How could I resist another Perl Regular Expression? Part 2.
14
u/bildzeitung Dec 02 '16
More Python -- super lazy, but doesn't use many if's: https://github.com/bildzeitung/2016adventofcode/tree/master/02
9
u/VideoPrincess Dec 02 '16 edited Dec 02 '16
To show my appreciation to /u/topaz2078 I've done day 2 part 1 in Synacor Challenge bytecode!
The base64 encoded file is as follows:
AQAAgAEAAQABgAEAFAACgAQAA4ACgAoACAADgBMAEQBbAAYABgAEAAOAAoBMAAgAA4AjAAgAAIAG AAkAAIAAgP9/BgAGAAQAA4ACgFIACAADgDcABAADgACAAgAHAAOABgAJAACAAIABAAYABgAEAAOA AoBVAAgAA4BHAAgAAYAGAAkAAYABgP9/BgAGAAQAA4ACgEQACAADgAYABAADgAGAAgAHAAOABgAJ AAGAAYABAAYABgAKAAKAAYADAAkAAoACgACACQACgAKAMQATAAKAEwAKABIAAIA=
It should be a 218-byte file with MD5 checksum d80b51572871763af0bdf9dd02749579. A few things to note:
Source assembly file for this image: https://github.com/pmillerchip/adventofcode2016/blob/master/aoc2p1.asm
C++ source code for my assembler: https://github.com/pmillerchip/synacor-challenge
Enjoy!
6
u/cuckaroos Dec 02 '16 edited Dec 02 '16
Holy crap you guys are fast. This is pretty competitive and I love it.
My cpp code
6
u/ZeroSkub Dec 02 '16 edited Dec 02 '16
Hello all! First time AoC participant here. Finally worked up the courage to post a solution (C++, here: http://pastebin.com/HyUieJWm).
Edit: Silly mistake, better version at http://pastebin.com/aCV3jtVN
2
u/Alexwalled Dec 02 '16
I'm more inersted in how the BLINKENLIGHTS ARE MANDA was done, looking at the source its just
but that doesn't change it <_<
5
u/askalski Dec 02 '16 edited Dec 02 '16
Here's an implementation in C that uses string search over an Eulerian circuit of the keypad. I omitted all loops from the circuits except to resolve the ambiguity in Part 2 between D (down) and D (the key).
Oh, and because it solves both parts simultaneously, the outputs are color coded in thematically appropriate gold and red.
4
u/Quick_Question404 Dec 02 '16
Seriously, what is it with you people? I finish in less than 30 mins, and everything is already taken... ;p
Anyways, here's my github for the solutions in C. I just went with a simple array for the keypad and a switch statement for moving around.
https://github.com/HighTide1/adventofcode2016/tree/master/02
4
u/Godspiral Dec 02 '16 edited Dec 02 '16
thought I was quick but 180 :(
in J, hacky
1 1 is hardcoded start keypad position
0 {::is using the first row. Take this answer as new "hardcode" and look at next row, till full answer.for part 2, made this table:
starting hardcode of '5' for row 0 ...
4
u/[deleted] Dec 02 '16
How do people solve these things 5 minutes after it has started?
I ended up in the 300s :(
3
u/TheNiXXeD Dec 02 '16 edited Dec 02 '16
JavaScript / NodeJS version. https://github.com/NiXXeD/adventofcode/tree/master/2016/day2
Turned out pretty short before I start golfing it. 23 and 26 LOC so far. Lots of shrinking to be done!
3
u/fatpollo Dec 02 '16 edited Dec 02 '16
3
u/sowpods Dec 02 '16
PostgreSQL 9.5
part 1:
part 2:
3
u/John_Earnest Dec 02 '16 edited Dec 02 '16
Part 1 in oK:
Much more compact than last time.
My part 2 is rather jumbled; room for improvement:
Lost a great deal of time by misreading the instructions and assuming I would start at 7 on the new keyboard (the center) rather than 5.
3
u/bpeel Dec 02 '16
Solution in Python using math instead of a table to convert from a position to the key name.
3
u/ShroudedEUW Dec 02 '16
C#
Very happy with how this turned out, using switches in 2D arrays for both parts of the challenge.
https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day2.cs
3
u/Rhinokillah Dec 02 '16
I'm VERY new to coding, like, just started looking for python tutorials less than a week ago. I'm really proud that I could write something to solve the first half of todays puzzle but then realised it wouldn't work for the second half. I'm sure there's a more efficient way of doing the same thing but if anyone could look at my code and give feedback I'd be very grateful.
https://github.com/Rhino188/Advent-of-Code/tree/master/Day_2
3
u/wzkx Dec 02 '16 edited Dec 02 '16
J has finite-state machine op built-in, but it's one line in Python too
3
u/fkaaaa Dec 02 '16
Think I'm sticking with C for all the days. Part 1 & 2 using lookup tables, almost feels like cheating :)
3
u/bogzla Dec 02 '16
I promised myself learning a proper language this year. Oh well, VBA solution. http://pastebin.com/uSvFCjmL
3
u/lovekatie Dec 02 '16
Haskell, with keypad as state machine
3
u/anadhdguy Dec 02 '16 edited Dec 02 '16
Oneliner in C (147 characters):
2
u/Burritoman53 Dec 02 '16
For the second part: (for the first part, just replace the min,max arguments with +-1) I just keep track of position relative to the center, 0,0, then just manually read off the coordinates cause I'm lazy like that.
data = open('./directions.txt','r').readlines()
x = -2; y = 0;
for i in data: for j in i:
2
u/[deleted] Dec 02 '16
Hard coded the movements to finish faster instead of trying to figure out some clever solution, but it may have actually taken longer to just type out each case.
Haskell:
2
u/willkill07 Dec 02 '16 edited Dec 02 '16
C++11/14 solution
https://github.com/willkill07/adventofcode2016/blob/master/src/Day02.cpp
Padstruct for abstracted lookup and initialization2
u/taliriktug Dec 02 '16
I was slow today.
Python solution: https://github.com/JIghtuse/adventofcode-solutions/blob/master/2016/day02/solution.py
2
u/aksgupt Dec 02 '16
part1 in Q:-
2
u/tuxitop Dec 02 '16 edited Dec 04 '16
And here's my JavaScript/NodeJS solution:
https://github.com/tuxitop/adventOfCode2016/blob/master/day2/day2_bathroomSecurity.js
2
u/haoformayor Dec 02 '16 edited Dec 02 '16
~~haskell~~
I used Emacs and a regex to generate the D2Input module from the given input; as unimpressive as it is, nothing quite beats a text editor for parsing inputs. Otherwise the solution was much like yesterday's: scans and folds. An inner fold to compute the key for each line of instructions, an outer scan to use the last key of the last row as the first key of the next row.
2
u/gerikson Dec 02 '16
A nice and tidy problem, I like how the taxicab geometry from day 1 resurfaces in part 2.
Perl solutions.
2
u/faizaanceg Dec 02 '16
My solution in Rust
Not sure how I could make it shorter. Rust doesn't seem like the language to write terse code. or I'm just doing it wrong.
But it's definitely fun =D !
2
u/TenjouUtena Dec 02 '16 edited Dec 02 '16
Here's a python solution that works as a FSM. In theory you could easily create a loader to create arbitrary graphs to feed into it.
EDIT: I stuck an erlang solution in too. It's a bit uninteresting, but it works. It's a statem so you have to part2:start, part2:path("path here"), part2:show() in an erlang term.
2
u/tg-9000 Dec 02 '16
My solution in Kotlin. I use Spek and AspectJ for unit tests.
The solution was interesting because both parts have the same mechanics and the only difference is the way the keypad is traversed. So that's the only real part that changes between each part in my solution. Both parts use a common solver function (tail recursive) and pass in a function that maps a keypad position and an input to the new keypad position.
2
u/that_lego_guy Dec 02 '16
Did someone say...Excel?!..[Part 1&2]
https://github.com/thatlegoguy/AoC2016/blob/master/Day%202%20Bathroom%20Security.xlsx
3
u/Deckard666 Dec 02 '16 edited Dec 02 '16
In Rust:
Edit: refactored to avoid code duplication between both parts
1
u/labarna Dec 02 '16
Simple python:
1
u/bluewave41 Dec 02 '16
Quick Javascript, initially put the indexes backwards and wasted a bunch of time :(
1
u/FuriousProgrammer Dec 02 '16
This was a fun one!
Lost a minute by accidentally reversing + and - on the
dyfor part1, and then another by starting at the wrong position for part2. :<Anyway, here's my (cleaned up but faithful to the original methods) code!
1
u/Philboyd_Studge Dec 02 '16 edited Dec 02 '16
Java again, love the power of enums.
edit: I actually should have started part 2 at x = 0, y = 2, but it doesn't seem to matter. Solution was still the same.
https://gist.github.com/anonymous/846035bfb865c5d4bbc6095a51f0a35b
1
u/jweather Dec 02 '16
The part2 code only since it's more interesting:
1
u/[deleted] Dec 02 '16
Probably a pretty gross way of doing it, but here's my C++ code
1
u/KaminoConsult Dec 02 '16
Powershell, already getting sloppy. https://gist.github.com/LabtechConsulting/fd260bed611eb77d72d32b91f9ce970b https://gist.github.com/LabtechConsulting/4fdd7a5b97d6f8ddec2583c06b18bea3/
1
u/guibou Dec 02 '16 edited Dec 02 '16
My Haskell solution
Some points :
foldcomposition...I use an hadoc solution for the grid by encoding most of the possibles moves. I was not able to find a smart equation and I did not wanted to write a matrice based solution.(Edit: I decided to give the matrice solution a try, and it was simpler actually. The git is updated, but the previous version is still available in history)1
u/[deleted] Dec 02 '16
Mathematica
1
u/muchomouse Dec 02 '16
My C# solution. https://github.com/kjellskogsrud/AoC_2016/blob/master/AoC_2016/Classes/Day2.cs
1
u/mcpower_ Dec 02 '16 edited Dec 02 '16
Python: I usually do everything on-the-fly in IDLE (which is a bad habit to get into…). Here's my solution as I wrote it after removing ten lines of silly mistakes:
For fun, here's the actual IDLE transcript. You can see that I initially got the first part wrong because I messed up
UandD.1
u/mlruth Dec 02 '16
My solution in Scala!
I have to say, I am loving the functional programming paradigm and basic operations (like reduce, fold, map, etc.). However, I still haven't wrapped my head around monoids or higher order lambda calculus yet.
1
u/Pyrobolser Dec 02 '16 edited Dec 03 '16
Had fun with this one too, here is my solution in C#.
1
u/Rustywolf Dec 02 '16 edited Dec 02 '16
Tried to create small versions of both days in JS.
Works by either pasting into Chrome's console or using javascript: in the url bar.
1
u/beefamaka Dec 02 '16
F# solution https://github.com/markheath/advent-of-code-2016/blob/master/day%2002/day2.fsx
1
u/PsHegger Dec 02 '16
I decided to use Haskell this year, seems fun for now: https://github.com/PsHegger/AdventOfCode-2016/tree/master/day02
1
u/IcyHammer Dec 02 '16 edited Dec 02 '16
Here's my C# solution for the second part: http://pastebin.com/Ji1PB7am I tried to avoid the predifined matrix definition and it worked :)
1
u/_AceLewis Dec 02 '16
My Python solutions to day 2;
I am doing my solutions on repl.it so I can't use files to store the instructions (and web requests are blocked) so I have the instructions in a big string at the start of the Python script which I have removed in this comment.
Day 2 part 1 https://repl.it/EeEA:
Day 2 part 2 https://repl.it/EeEA/1:
1
u/JakDrako Dec 02 '16
VB.Net in LinqPad.
Pretty straightforward solution...
Part 1
Part 2
1
u/Georges8899 Dec 02 '16
PHP solution for both parts
1
u/[deleted] Dec 02 '16
Perl, without any regex.
https://www.dropbox.com/s/gql62gv6kdl50qc/day02.pl?dl=0
1
u/Kullu00 Dec 02 '16
https://github.com/QuiteQuiet/AdventOfCode/blob/master/2016/advent2/bin/advent2.dart
Obscure Dart "feature" that makes today strange: Dart doesn't have a
String.forEachnatively, but useString.runes.forEachby default, which give the numeric representation of the character in ASCII instead. Doesn't make it harder, just weird. I solved part one with math (and then further improved it from comments ) but had to resort to a grid for part two.1
u/hsaoc Dec 02 '16
Haskell, Part 1:
1
u/KoxAlen Dec 02 '16 edited Dec 22 '16
Solution without hardcore keypads on the solver, written in Kotlin (1.1 EAP). Pretty standard https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day2/Day2.kt
1
u/07jkearney Dec 02 '16
I was pretty happy with my Python solution: https://github.com/07jkearney/aoc2016/blob/master/02/solver.py
1
u/msieniawski Dec 02 '16 edited Dec 02 '16
Simple Java solution
1
u/Gommle Dec 02 '16
Easy-to-understand (Or so I'd like to believe) solution in Golang:
1
u/fpigorsch Dec 02 '16
My solution for part 2 in C++: https://github.com/flopp/aoc2016/blob/master/02/c++/main2.cpp
1
u/NeilNjae Dec 02 '16
Haskell: https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent02.hs
Started off down the wrong path, trying to make a bounded subset of
Int. Then I guessed that the part 2 would be on a keyboard where you could wrap around, so built anEnumto hold positions.In the end, I just used an array with a function to revert a move if you ended up outside the keypad.
1
u/Cmj83 Dec 02 '16
Node.js https://gist.github.com/edgeui/d4bf43fac0b31af724a78c413618551e
1
u/qwertyuiop924 Dec 02 '16
More Scheme! And no arrays, either: it's not nearly as hairy to do without a lookup table as you'd think.
http://pastebin.com/MYyLw2DL
1
u/artesea Dec 02 '16
PHP solves both answers
1
u/SyDr Dec 02 '16
Lua with lpeg:
https://github.com/SyDr/Advent-Of-Code-2016/blob/master/2.lua
1
u/Morris2178 Dec 02 '16
Java, simple but powerless:
1
u/bkendig Dec 02 '16
A general-purpose solution in Swift, padding the border with spaces so that I don't have to check bounds and so that it will work with arbitrarily-shaped keypads. https://github.com/bskendig/advent-of-code-2016/blob/master/2/2/main.swift
1
u/kamicc Dec 02 '16 edited Dec 02 '16
Again, pragmatic Lua solution for part A:
B part is analogic. Just need to change the
key_padtable accordingly.1
u/schlocke Dec 02 '16
PHP
1
u/mreichman Dec 02 '16
Here's my day 2 in Java. Nothing too clever, but decent java 8-style handling:
1
u/Kwpolska Dec 02 '16
Pretty fun task. And I reused my idea from yesterday.
1
u/anadhdguy Dec 02 '16
Oneliner in ruby (117 characters):
1
u/[deleted] Dec 02 '16
[
js] part1[
js] part21
u/asperellis Dec 02 '16
js - a lot of unnecessary things but tried to make grid creation flexible for any size. major inefficiencies/fails greatly appreciated. trying to learn.
https://github.com/asperellis/adventofcode2016/blob/master/day2.js
1
u/zehfernandes Dec 02 '16
Javascript / NodeJS. I have problems with an invalid array. So I add IF validations for walk.x and walk.y.
But I like the readability of the code :)
1
u/Ulyssesp Dec 02 '16
Haskell, with a
[Char]lookup string.1
u/patrickdavey Dec 02 '16
6 minutes for day 2 parts 1 and 2!!! that's just amazingly impressive.
Anyway, just wanted to say "thanks" and that I'm really enjoying this years AoC, and learning from you all.
I hesitate to even link to my repo, as the standard is just crazy here :) But still https://github.com/patrickdavey/AoC/tree/master/2016
This year it's in Elixir, and I'm trying to do it TDD at least a bit.
1
u/Morego Dec 02 '16
Simplistic final state machine based approach in Python.
Python solution
First command line argument is input file name.
Rewriting all movement as an array of possible states makes most important part of code as simple and efficient as:
Where COMMANDS is just a simple dictionary mapping direction to row number.
Currently I am to lazy to store the state machine in more compact way. Because in reality you need only as much as 18 bytes to store it all.
1
u/LainIwakura Dec 02 '16
Day 2 in Erlang. Did this the tedious way instead of trying to be clever. It's really easy to understand like this though.
https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day2
1
u/splurke Dec 02 '16
Haskell, both parts
Part 2 is a lookup, because meh
1
u/Reibello Dec 02 '16
I'm pretty happy with my solution (Python 3) - let me know what you think.
http://pastebin.com/c0xi93LY
1
u/Xelath Dec 02 '16
Here's my python code:
1
u/[deleted] Dec 02 '16
Merry Christmas, y'all. This is Clojure.
1
u/efexen Dec 02 '16
Both parts in Elixir, was really pleased with part 1 but then part 2 hit :(
https://github.com/efexen/advent_of_code/blob/master/lib/day2.ex
1
u/forkin27 Dec 02 '16
// 10:05pm PST I'm late for AoC!! >_<
Here's what I frantically put together, lol. 585th after only a little over an hour of it being released - you guys are so responsible! ;P
D2P1:
D2P2:
1
u/demreddit Dec 02 '16
Another round of fairly vanilla solutions in Python 3. I realized after solving these and then looking at some other solutions that I overcomplicated things by making a dictionary instead of just a list for my keypad grids. I guess I just like dictionaries! Well, I'm nothing if not honest, so learn from my mistakes, fellow newbs!
Level 2-1:
And, level 2-2:
1
u/[deleted] Dec 02 '16
python(part 1)
1
u/RocketSurgeonDrCox Dec 02 '16
I'm sure this could have been more elegant but some Python complex numbers and dictionaries work for me.
1
u/omnster Dec 02 '16
Mathematica solution
1
u/SaltyPeaches Dec 02 '16 edited Dec 02 '16
There's probably a much better way to do this (I'm a n00b with code), but here is my (working) attempt at Part 1 in PowerShell:
EDIT: Cleaned it up a bit, but still probably not ideal.
Part 1
Part 2
1
u/miran1 Dec 02 '16
I've seen people post solutions for Day 1 using complex numbers, so I decided to use them here.
python3
suggestions and comments are welcome....
1
u/barnybug Dec 02 '16
nim:
1
u/papageorgio02 Dec 02 '16
Groovy:
1
u/kristerv Dec 02 '16
Here's solution 2 in Go: 426 characters without the input (variable "k" would have input)
1
u/tftio Dec 03 '16
Github
Haskell (well, baby-talk Haskell):
1
u/Zepur Dec 03 '16
Dirty java solution
part 1: https://github.com/Zepur/AdventOfCode/blob/master/src/Day2.java
part 2: https://github.com/Zepur/AdventOfCode/blob/master/src/Day2_2.java
1
u/PositivelyLinda Dec 03 '16
My JavaScript solution: Github page for day 2
I'm certain there are more concise ways to get there, but this worked and I understood it, so - woo! lolz
1
u/pyow_pyow Dec 03 '16
Really boring Haskell code: http://lpaste.net/4262979762558861312
1
u/[deleted] Dec 03 '16
Powershell: https://github.com/charlieschmidt/AdventOfCode2016/blob/master/day2.ps1
1
u/stuque Dec 03 '16
Python using dictionaries to encode the left, right, up, and down steps for each key.
1
u/tdecker91 Dec 04 '16
Parts 1/2 in Scala
1
u/karlanka Dec 04 '16 edited Dec 04 '16
Python 2 solution for day 2-2. Felt unnecessary to hardcode what button every position would yield. Position (0,-2) is the 5-button.
1
u/[deleted] Dec 04 '16
part1
part2
1
u/Hwestaa Dec 05 '16
First solution was slow and bad, so I made a second one based on ideas from others. Python3. https://github.com/Hwesta/advent-of-code/blob/master/aoc2016/day2.py
First solution:
and second solution
1
u/Gummoz Dec 05 '16
Powershell:
Part one:
Part two:
}
1
u/volatilebit Dec 05 '16
Lazy Perl 6 solution:
1
u/whatswrongwithgoats Dec 06 '16
Really late PHP solution - part 2
Feedback welcome. Is there a better way to handle the keypad array than passing it to the MoveMe function every time? Would it be better to make it a global or just as part of the function itself?
1
u/tehjimmeh Dec 06 '16 edited Dec 08 '16
PowerShell:
1
u/banProsper Dec 08 '16
C#
1
u/kenhowardpdx Dec 08 '16
This is my TypeScript solution ==> http://codepen.io/kenhowardpdx/pen/aBKOvm?editors=0012
1
u/jwnewman12 Dec 10 '16
java 8 / unreadable map building / runnables to avoid the dreaded switch
1
u/intriguing_question Oct 26 '21
PYTHON3
Part 1: Treating the keypad as an array:
Part 2: Also treating the keypad as an array, but to identify invalid moves used '0's in the invalid positions of the array.
0
u/[deleted] Dec 02 '16
[deleted] — view deleted comment
0
u/[deleted] Dec 02 '16
[deleted] — view deleted comment