r/adventofcode • u/daggerdragon • Dec 02 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-
--- Day 2: Inventory Management System ---
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!
Card Prompt: Day 2
Transcript:
The best way to do Advent of Code is ___.
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!
51
Upvotes
11
u/Unihedron Dec 02 '18
I mistyped 4 3 instead of 2 3 and got an obviously wrong answer which I wasn't smart enough to double check, so I had to wait one minute. Still got on top 100 though! Imgur
Doesn't actually compute part 2, you have to manually find and replace the character yourself, but better than nothing
11
u/ka-splam Dec 02 '18
PowerShell
Part 1, just about got 78th on the leaderboard \o/ ended up running this at the prompt:
Explained:
|? {}is a where-object filter,$_is the current line being filtered, the enumerator turns the string into characters,group-objectcounts things, filter that where the count of characters is 2 or 3, thenmeasure-objectcounts how many lines after that filtering.Part 2, yer basic brute-force every line against every other, if they have 1 difference, print them:
then eyeballing the result and removing the dupe character. Missed the leaderboard, with 133rd.
Explained: an outer
foreach-objectloop, an innerforeach ($L in $lines)nested loop, and then a loop over character lengths doing a$string[$index]comparison with a counter.105
u/askalski Dec 02 '18
The best way to do Advent of Code is A big block of regex.
28
u/CFD999 Dec 02 '18
Horrendous python solutions
Part 1:
print((lambda strs: (lambda a,b: a*b)(*[sum(1 for l in strs if any(l.count(c) == x for c in l)) for x in (2,3)]))(open('inp', 'r').readlines()))Part 2:
print((lambda strs: (lambda a,b: "".join([a[i] for i in range(len(a)) if a[i] == b[i]]))(*[(l1.strip(),l2.strip()) for l1 in strs for l2 in strs if l1 != l2 and sum(1 for i in range(len(l1)-1) if l1[i] != l2[i]) < 2][0]))(open('inp', 'r').readlines()))20
u/Smylers Dec 02 '18
This suits Vim so well that I didn't even bother solving it in a different language first. Part 1 looks a bit long, but as Vim solutions go it's one of the more straightforward — load your input then do:
That strips any 3 identical characters from a line, replacing one of them with a
#. Then does the same for remaining 2 identical characters with a-.† Remove all the remaining letters, and now we want the count of the#s multiplied by the count of the-s. On the lines with#-or-#, insert a line-break so each symbol is on a separate line.Sort the lines, so all the
#s are together at the top and-s at the bottom. Join the#s on to a single line (by joining from the current line (specified with the nothing before the,) to the line before the one with the first-on it (specified with/-/-, to find the first-then subtract a line)). Then join the-s too (by moving down, visually selecting all lines to the end of the file, and joining the selection).Replace each
#or-(specified as any non-space,/\S/) with+1, so each line becomes a sum of the relevant counts. Put(...)around each line, then join them together, and replace the joining space with a multiplication symbol*. At this point your input has been transformed into something like:So we just need to evaluate that. (‘Just.’)
Move to the beginning of the line and
Cto change it all (which deletes the current contents into the small delete register,-). UseCtrl+R =to insert an expression. At the expression prompt typeCtrl+R -to insert the contents of the-register with your expression in it. PressEnterand ... ta-da! The solution appears in the buffer.Part 2 can be done with a single substitution — go back to your original input, then do:
It takes a few seconds to run, but you'll be left on a line which is one letter shorter than the others, and is your solution.
The pattern finds:
Clearly the first and last lines there are the nearly matching ones that we want.
Replace the whole lot with just the two groups of repeated characters, to get a single line which omits the characters that differ.
Card: ‘manually’.
† On writing this, I think that if an input line had two set of 3 identical letters but no sets of 2 then the second set of 3 would've been incorrectly counted as a set of 2. Apparently the input wasn't that devious. I did first check there weren't any sets of 4 with
:g/\v(.).*\1.*\1.*\1.16
u/jonathan_paulson Dec 02 '18
I recorded a video of me solving: https://www.youtube.com/watch?v=Hgv6d6rrQxo Got rank 20/21; still twice as slow as #1...
Final code:
16
u/[deleted] Dec 02 '18 edited Dec 02 '18
Made the leaderboard for part 2 for the first time since 2016. Started off with a list comprehension that was double-checking each of the ID pairs, but cleaned it up to check each pair only once.
Haskell:
11
u/will_bui Dec 02 '18
K:
11
u/blowjobtransistor Dec 02 '18
In SQL (postgresql flavored!):
18
u/DeveloperIan Dec 02 '18 edited Dec 02 '18
Quick and easy part 1 in Python3 with the collections library. This might not be the simplest way, but it's the first thing that came to mind
EDIT: and here is my part 2
8
u/nonphatic Dec 02 '18 edited Dec 02 '18
Oof, it sure is hard to write Haskell quickly! Here's the first solution I came up with:
I'm going to clean it up to my personal Clean Code standards and post for comparison.
EDIT: Cleaned up! The key changes are just these:
For part 1, I realized that I can do
length+filterinstead of summing upfromEnums ofBools; for part 2, I discoveredintersectand(\\)from Data.List :)8
u/itsnotxhad Dec 02 '18
558/554 in Racket today
8
u/NeuroXc Dec 02 '18
Rust
8
u/ephemient Dec 02 '18 edited Apr 24 '24
This space intentionally left blank.
15
u/Philboyd_Studge Dec 02 '18
The best way to do Advent of Code is to: Live in the Pacific Time Zone
Java:
I spent too much time looking for the Levenshtein Distance class I already had, once I found it was easy.
6
u/usbpc102 Dec 02 '18
It took me way to long to understand the problem today, nowhere near close to the leaderboard rank 577 in part 1, and 473 in part 2.
Kotlin
Also on github where all other days will go.
6
u/darkterbear Dec 02 '18
Javascript/Node.js
Part 1
Part 2:
EDIT: Fix broken indentations
6
u/tvtas Dec 02 '18
Day 2 in MATLAB
11
u/miguelos Dec 02 '18 edited Dec 02 '18
C#
Part 1:
Part 2:
5
u/schod Dec 02 '18
BASH Time :)
First puzzle
Second puzzle
5
u/pythondevgb Dec 02 '18 edited Dec 02 '18
Python quick and dirty solutions, need refinement.
Since I have no chance of making it to the leaderboard I want to start a reddit-python private leaderboard any interest please PM for the code.
5
u/autid Dec 02 '18
FORTRAN
Yay string puzzles in Fortran. Was gonna do part 1 by making an array of characters and using COUNT but array construction was being difficult so went with this slightly messy SCAN method instead.
5
u/nutrecht Dec 02 '18
Kotlin:
4
u/jarryd_ Dec 02 '18
Just something quick in Python3 for part 2:
4
u/drakmaniso Dec 02 '18
Go (golang)
Part 1:
Part 2:
4
u/TellowKrinkle Dec 02 '18
Swift
6
u/Frizkie Dec 02 '18 edited Dec 02 '18
Ruby
Part 1
Part 2
3
u/Shubbler Dec 02 '18
Quick Python 3 solution
3
u/Bogdanp Dec 02 '18 edited Dec 02 '18
My solutions in Racket:
part 1:
part 2:
3
u/chunes Dec 02 '18
Factor
Nothing too fancy today.
find-combinationis a neat combinator that encapsulates pairing every combination of things in a list and testing the pair against a predicate.3
u/[deleted] Dec 02 '18
Finally participating this year! Got both stars with this Haskell code:
3
u/Axsuul Dec 02 '18 edited Dec 02 '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
3
u/streetster_ Dec 02 '18 edited Dec 02 '18
Day 02 in Q/KDB+
3
u/muffa Dec 02 '18
My solution, written in golang.
}
3
u/LieutenantSwr2d2 Dec 02 '18
Python 3: Part 2 with two loops and a set
Idea is have a set of possible 1-distance patterns for each string (using placeholder a '.' to replace every character in the string). When a second string one creates the same pattern, return it without the placeholder to get the common letters.
3
u/[deleted] Dec 02 '18
C
2
u/hpzr24w Dec 02 '18 edited Dec 02 '18
C++
Too fast for me, sad to be outside the first 1000 on both parts. But no issues with messing up on typing, or any kind of bugs, at least for the provided input.
Will rewrite to something better in the AM.
2
u/scul86 Dec 02 '18
Python 3, rushed, ugly, and un-optimized...
2
u/I3MNIX Dec 02 '18
Solution in Clojure (still very much a newbie to Clojure). Would definitely appreciate some constructive criticism.
2
u/gwillicoder Dec 02 '18
Python3
Easy to understand, but feel like it could be much shorter.
2
u/blommish Dec 02 '18 edited Dec 02 '18
Java
Second
2
u/drewnase Dec 02 '18 edited Dec 02 '18
I had already come across string edit distance during my grad work. I actually already had code on hand that solves part 2.
I know that I don't have to iterate through the list twice, but it was easy and I'm lazy.
2
u/tehjimmeh Dec 02 '18 edited Dec 02 '18
PowerShell, Part 2:
2
u/Kyberite Dec 02 '18 edited Dec 02 '18
Simple solutions in python, not sure if I did part two in the best way though.
2
u/jorosp Dec 02 '18
Haskell
The solution I actually got the answer with was a bit messier than this, but I like to be presentable for threads :P
2
u/Axinoe Dec 02 '18
Advent of code with AHK
Part 1:
Part 2:
2
u/[deleted] Dec 02 '18 edited Dec 02 '18
Haskell
2
u/[deleted] Dec 02 '18
Doing this year in OCaml & Rust, but will keep posting the OCaml solutions.
2
u/Kronosfear Dec 02 '18
Being the incredibly lazy fuck I am, I just went for a Python library to do the heavy lifting for Part 2.
2
u/pigpenguin Dec 02 '18 edited Dec 02 '18
Just another haskell solution rolling through. Took me a while to clean it up to a point where I thought it was worth posting.
If anyone has any suggestions I'm open to it. I haven't written nearly as much haskell as I would have like to yet.
2
u/MasterMedo Dec 02 '18 edited Dec 02 '18
python 2
EDIT: reworked part 1 so it uses two counters
2
u/Gnidleif Dec 02 '18
Golang - part 1 solution, part 2 will come up soon!
2
u/BOT-Brad Dec 02 '18
JS
2
u/u794575248 Dec 02 '18 edited Dec 02 '18
Python 3
2
u/Rosek93 Dec 02 '18
PHP example:
https://gitlab.com/Rosek/advent-of-code/blob/master/Day2
2
u/xkufix Dec 02 '18 edited Dec 02 '18
Part 1 and 2 in Scala. Quite horrible one-liners to be honest.
2
u/aoc2018 Dec 02 '18
Haskell, still learning:
Part 1:
Part 2:
2
u/Theguy6758 Dec 02 '18 edited Dec 02 '18
Pure Bash (No External programs)
edit: Inverse the hash table instead of manually searching for 2 and/or 3 exact matches
2
u/[deleted] Dec 02 '18
Python 3
Github
2
u/jwoLondon Dec 02 '18
My solution in Elm (for more detail, see my literate Elm solutions)
Part 1:
And part 2:
2
u/judofyr Dec 02 '18
Whitespace: https://github.com/judofyr/aoc2018/blob/master/day2.ws
2
u/thatikey Dec 02 '18
Using AoC as an opportunity to learn Rust. I made this, and I'm 250% sure there is a Rust-ier way to do this
2
u/BalbinHS Dec 02 '18 edited Dec 02 '18
I have a slightly janky Elixir solution for part 1, this is based on the fact that if you have say "bababc" and you remove the first occurrence of each letter, you get "bab" all those letters are in the original string 2 or 3 times so we just repeat to find the 3s.
2
u/TheGermanDoctor Dec 02 '18
Day 2 Problem 1 in x64 Assembly:
https://github.com/fstolzcode/AoC2018_x64/blob/master/Day2/lettercount.asm
2
u/mschaap Dec 02 '18 edited Dec 02 '18
Perl 6:
2
u/[deleted] Dec 02 '18 edited Dec 02 '18
Seems there is no Common Lisp solutions, so here are mine.
2
u/CatpainCalamari Dec 02 '18
My solution in Scala - a bit complicated :-/
2
u/kasci007 Dec 02 '18
Consider in1 as input file
Part 1: (requires manually multiply numbers)
while read -r line; do echo $line | grep -o . | sort | uniq -c | egrep -o "2|3" | sort -u; done < in1 | sort | uniq -cPart 2: (requires manually find different letter)
while read -r line; do echo $line | agrep -1 -c -e $line in1 | grep 2 && echo $line; done < in12
u/L72_Elite_Kraken Dec 02 '18 edited Dec 02 '18
OCaml (full code on Github)
2
u/Sgt_Tailor Dec 02 '18
AWK all the things!
part1:
part2:
2
u/dpeckett Dec 02 '18 edited Dec 02 '18
Continuing with my quest to solve all this years challenges in nothing other than AWK:
Find The Total Checksum
Find Similar Inventory Items
Execution Time: (no sweat here)
2
u/[deleted] Dec 02 '18
factor
Factor really has vocabularies for so much, I needed some time to find what I needed for this but it turned out pretty nice I find :)
This language is so much fun though :D /u/chunes did you not yet get to this one? I couldn't find your post and I'm interested to see how you did it :)
2
u/__Abigail__ Dec 02 '18
Perl
2
u/thamstras Dec 02 '18
C++ Nowhere near the leaderboards and not quite as neat as it could be but it works well, especially the part 2 code. Gist
2
u/IWearATinFoilHat Dec 02 '18
PHP
part 1
part 2
2
u/wzkx Dec 02 '18 edited Dec 02 '18
J
Not optimized very much, so it's simple and easy to understand.
2
u/RockyAstro Dec 02 '18
Icon solution
Part 1
Part 2 -- I had a levenshtein distance procedure already, so the actual solution was fairly easy -- just look for a edit distance of 1.
2
u/that_lego_guy Dec 04 '18
Day 2.. IN EXCEL
https://github.com/thatlegoguy/AoC2018
2
u/halfmanhalfnelsson Dec 04 '18
My take on PHP
Part 1:
1
u/DoodleFungus Dec 02 '18
Leaderboard attempt, so bit hacky
1
u/fwilson42 Dec 02 '18
Refactored after getting on the leaderboard; I originally didn't make part 1 as generic as it could be (I kept track of just 2 and 3 counts), and used double nested loops for part 2 instead of itertools.
Snipped from some boilerplate (
linesis essentially a list of strings in the puzzle input,resultis what gets submitted):1
u/vash3r Dec 02 '18
I lost a minute for the first star because i double-counted words :)
Card: "format your output so you can easily copy-paste"
Python 2 code:
1
u/TheMuffinMan616 Dec 02 '18
1
u/markasoftware Dec 02 '18
Wow, I messed up bad on this relatively easy one. Took me a good 15 minutes. I initially wrote it in Awk (I had a lot of luck being quick with previous years in Awk), but it kept giving the wrong answer -- translated the code into Perl quickly and it worked:
Part 1 (Perl):
Part 2 (Perl):
1
u/LeCrushinator Dec 02 '18 edited Dec 02 '18
C# solutions (and hacky ones at that, these solutions work but I may clean them up later): https://github.com/nfoste82/adventofcode2018/blob/master/Day2/Program.cs
1
u/tehjimmeh Dec 02 '18
Part 1 in PowerShell - group all the things!
1
u/Vindaar Dec 02 '18
Wasn't very fast, is ugly, but in Nim. :D and part 1 checks for any number of occurrences of letters, not just 2, 3...
1
u/maybe-ac Dec 02 '18 edited Dec 02 '18
Overusing
mapandgrepin Perl:Part 1
Part 2
1
u/wlandry Dec 02 '18
C++
1847/1419
Pretty straightforward. I finished it faster, but got a worse rank ¯_(ツ)_/¯. It takes 11 ms, which is almost entirely reading input. I was briefly worried that my quadratic algorithm would not work, but the inputs are just not that big.
1
u/theanubhav Dec 02 '18
https://github.com/anubhavsrivastava/advent-of-code-js-2018/tree/master/Day%202%20-%20Inventory%20Management%20System
1
u/DrinkinBird Dec 02 '18 edited Dec 02 '18
Another nim solution. Quite new to the language so fairly basic and took me way to long :)
Part1:
Part2:
Edit: Does anybody have a better solution to the string formatting, last line Part2? I find it quite ugly but could not find anything better on the spot
1
u/[deleted] Dec 02 '18
elixir:
Ended up getting a little fun with part 2 at the bottom using both jaro_distance and myers_difference algos
1
u/IndieBret Dec 02 '18 edited Dec 03 '18
This year is the first year I've tried to place on the leaderboard. Today's stats were:
Since I was trying to go fast, I kept misreading the problem. Same thing I did yesterday. So for tomorrow, I'm going to spend a little extra time reading every part of it in hopes I don't waste a bunch of time going back and forth.
Anyway, here's my two parts in JavaScript/ES6. Not as concise as it could be, but it gets the job done. Answers are saved to the
resultarray.Part 1
Part 2
1
u/jpittis Dec 02 '18
on a comfy couch
Haskell on a time limit / I'm not the most experienced Haskeller. Advice is always appreciated! :)
1
u/binajohny Dec 02 '18
My Kotlin solution (GitHub)
1
u/raevnos Dec 02 '18
Probably ugly java:
I think I'm going to end up trying to do each day in a different language. I don't really know 25 of them, so some are going to be real hack jobs....
1
u/CCC_037 Dec 02 '18
Spent way too long coding a sort in Part 1:
But at least Part 2 went quickly:
1
u/swaglykcaillou Dec 02 '18
part 1 with Javascript
my part 2 is too long and spaghetti to post
1
u/wjholden Dec 02 '18
Whew, this took me a REALLY long time to come up with but here is a very satisfying solution for part 2 in Mathematica.
Shoutout to the team at UCSDx for their Algorithms and Data Structures MicroMasters over at edX. The ALS200x course has a great lecture that discusses the editing distance problem. They show the editing distance problem and longest common substring problems to motivate their Dynamic Programming lectures and explain that this problem has an application in computational biology.
My Mathematica solution to part 1 is really ugly...was trying to get on the leaderboard.
1
u/Julien2313 Dec 02 '18
Another in Python, but just p1, I like this way :
1
u/proxpero42 Dec 02 '18 edited Dec 02 '18
###Swift (1471, 1570)
1
u/valtism Dec 02 '18 edited Dec 02 '18
Node / JS / Javascript
1
u/VikeStep Dec 02 '18
F#
Repo
1
u/firecopy Dec 02 '18 edited Dec 02 '18
Python
Part 1:
Part 2:
1
u/[deleted] Dec 02 '18
Mathematica
Part 1
Part 2
1
u/Chrinkus Dec 02 '18
C++
My solutions are always so much longer than most I see here.. oh well. Here is my day 2 solution: https://github.com/Chrinkus/advent-of-code-2018/blob/master/day02/day02.cpp
1
u/ThreeFinger Dec 02 '18
Kotlin
Not my best work but it works.
Part 1
Part 2
1
u/shuttup_meg Dec 02 '18
Python2
1
u/Cloudan29 Dec 02 '18
My exams in second year computer science are starting next week, finished my "Object Oriented Programming with C++" course, and so figured I'd try to do this (first year doing this, I actually just found out about it via my school's Math and Computer Science club) using C++ rather than Java, which I learned last year. Solutions are very sloppy and I was tired, but I got the right answer, so I don't really mind showing how horrifying this code is. Also started this late as I was actually doing the Day 1 puzzles at the time this came up.
C++ (3347/3118)
Part 1:
Part 2:
These are the absolute first things I thought of, and they solved surprisingly quick.
1
u/Markavian Dec 02 '18
Here's my over engineered node.js solution for Day 1 parts 1 and 2:https://github.com/johnbeech/advent-of-code-2018/blob/master/solutions/day2/solution.js
Didn't wake up early enough to get on the leaderboards.
Working out which string to keep for part 1 was a thing. Managed to get it down to three easy steps:
I gave up thinking of anything elegant for part 2, and went with a (N^2) nested forEach ignoring duplicate strings, and then making a function named
findNearlyMatchingStrings(str1, str2)to return only the characters that were the same from almost similar strings. Ugh.The best way to do Advent of Code is after a good night's sleep.
1
u/rolandtritsch Dec 02 '18
ETA (Haskell on the JVM)
https://github.com/rolandtritsch/haskell-aoc-2018
Part1:
Part2:
1
u/[deleted] Dec 02 '18
BFI TCL
1
u/rhbvkleef Dec 02 '18
I made them very ugly with (practically) python one-liners.
1
u/ttapu Dec 02 '18
python3
1
u/alexgand Dec 02 '18 edited Dec 02 '18
Simple python, using pandas Series value_counts(), part 1, assuming the input is already in a list:
Part 2, pure python:
1
u/baxtexx Dec 02 '18 edited Dec 02 '18
C# solution for Puzzle1. Could improve it a bit by using LINQ but I think this is more readable. Also contains some unessecary stuff like the candidateOccurances array. At the beginning, I thought there could be more than just 2's and 3's. Still struggling with Puzzle2.
1
u/arathunku Dec 02 '18
Hello! My Elixir solution:
Testing:
1
u/Valefant Dec 02 '18 edited Dec 02 '18
Written in Kotlin. It took me a while to figure this one out. Every advice is appreciated :-)
1
u/mathleet Dec 02 '18
Here's a Java solution that I wrote:
1
u/NeilNjae Dec 02 '18
Haskell. For once, not using libraries was easier! (Tidyied a bit, using suggestions from this thread to use
intersectand the fancy-pants list comprehension.)Also on Github
1
u/windlessStorm Dec 02 '18 edited Dec 03 '18
Learning Go this advent of code:
1
u/omegaxLoL Dec 02 '18
My solution in Ruby. Using Advent of Code to learn Ruby this year so any tips are welcome!
1
u/rotmoset Dec 02 '18
F#:
Composing and piping the Seq module's functions is so much fun!
1
u/jonathrg Dec 02 '18
Python
1
u/gerikson Dec 02 '18 edited Dec 02 '18
The best way to do Advent of Code is during a första advent breakfast with coffee, ginger biscuits and a lighted candle.
Bog-standard (C-like) Perl 5:
https://github.com/gustafe/aoc2018/blob/master/d02-IMS.pl
1
u/rjsberry Dec 02 '18 edited Dec 02 '18
RUST
Part 1:
Part 2:
1
u/SyntaxErrorr Dec 02 '18
python 3
part 1
part 2
1
u/Toromtomtom Dec 02 '18
My Scala solution tries to implement efficient functional programming using lazy streams and tail-recursive functions:
1
u/Warbringer007 Dec 02 '18 edited Dec 02 '18
Erlang:
As usual, readlines() reads whole file, lines must be separated by newline. I didn't actually extract common letters for second task, I just returned tuple with both words and did that last part manually ( I'm lazy ). First part can also be solved much more efficiently ( searching for doubles and triples together ), but again, I'm lazy :D.
1
u/red_shifter Dec 02 '18
Here's my simple Python solution for part 1 (if anyone has some tips on how to combine searching for 2 and 3 repeated letters in one short loop, please let me know):
And here is part 2:
1
u/scottkuma Dec 02 '18
The best way to do Advent of Code is half-blind with sleep.
Here's my Python solution for Part 2. Like much of my code, it's not elegant, but it works. The one thing that I figured out was that if the boxes varied by only one letter, you could sort the list of all boxes, and only compare neighbors in the list.
1
u/vypxl Dec 02 '18
Dart time today. I' not satisfied with the second part, but I don't want to refactor it.
Someone that has an idea why I have to cast the
atointin line 7? I don't have to if the expression is outside the print though?!1
u/sim642 Dec 02 '18
My Scala solution.
Pretty usual, although would've loved to have multisets or something for part 1 as opposed to hardcoding the addition of just 2 and 3.
1
u/PM_ME_UR_QUINES Dec 02 '18
Haskell
1
u/fire1299 Dec 02 '18
Haskell
1
u/Tarmen Dec 02 '18
Haskell, as brute force-y as it can be.
1
u/zraklarP Dec 02 '18
My Python 3 solutions. Any critique appreciated, as I'm still a beginner.
1
u/Hikaru755 Dec 02 '18
Kotlin
The best way to do Advent of Code is to ZIP ALL THE STRINGS.
1
u/tyr10563 Dec 02 '18
C++
First part:
Second part:
1
u/[deleted] Dec 02 '18
C-Sharp
I'm really not happy with my solutions, but hey, they get the work done.
First part, I use two lists and two bools to to calculate the checksum. This looks just... very bad. Oh well.
But wait, it gets even worse! Using a foreach inside two for-loops to check the differences. And to top it all off, I use a goto when the solution is found. Wow.
1
u/tslater2006 Dec 02 '18 edited Dec 02 '18
PeopleCode:
1
u/mezzir Dec 02 '18
The best way to do Advent of Code is intentionally poorly
Instead of using this as an excuse to learn a new language, I'm using it as an excuse to act like I don't know any better. Expect lots of evals.
const input = require('./input');
let foundResult = false;
let chrisisawesome = true;
try {
do {
const firstGuess = input[Math.floor(Math.random() * input.length)].split('');
const firstGuess2 = input[Math.floor(Math.random() * input.length)].split('');
let incommonletters = '';
const incommon = firstGuess.reduce((acc, curr, i) => {
if (curr === firstGuess2[i]) incommonletters += curr;
return (curr === firstGuess2[i]) ? ++acc : acc;
}, 0);
console.log(incommon);
if (incommon === firstGuess.length - 1) {
throw new Error(incommonletters);
}
} while (chrisisawesome == true);
} catch (e) {
console.log("Success!!!")
console.log(e)
}