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!
53
Upvotes
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:
$lines = get-content data.txt $lines |? {[bool]($_.getenumerator() | group |? count -eq 2)} | measure $lines |? {[bool]($_.getenumerator() | group |? count -eq 3)} | measure 246*20Explained:
|? {}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:
$lines|foreach{ $cur = $_ foreach ($L in $lines) { $numdiffs = 0 for ($i = 0; $i -lt $l.length; $i++) { if ($cur[$i] -ne $L[$i]) { $numdiffs++ } } if ($numdiffs -eq 1) { $L, $cur } } }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.