r/adventofcode • u/daggerdragon • Dec 04 '18
SOLUTION MEGATHREAD -π- 2018 Day 4 Solutions -π-
--- Day 4: Repose Record ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 4
Transcript:
Todayβs puzzle would have been a lot easier if my language supported ___.
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!
38
Upvotes
View all comments
Show parent comment
1
u/ka-splam Dec 08 '18 edited Dec 08 '18
Hi π,
The
-matchoperator takes a string on the left and a pattern on the right, and tries to match the string with the pattern.$_is my input line each time. So this pattern issort-object { $_ ...}and sort-object gets the input, runs the scriptblock on it, and then sorts on the result of that. So I pull the date out of the lines, make it a PowerShell DateTime so it will sort properly.(I later learned that most of this is unnecessary, and if I'd just put
get-content .\data.txt | sort-object | foreach {}it would have sorted properly anyway. Oops.).-matchreturns true/false, which I donβt care about here because I made sure it will always match on my input data. So I take the result and cast it to β[void]which makes it vanish. Instead I could have used$result = $_ -match '..pattern..'` but that leaves a variable hanging around that is not going to be used, matter of choice if that bothers you.When
-matchcan match the pattern to the text, it sets the automatic variable$matcheswith some information. So the next line works with that as a way to use the result of this line.$matches[1]is the first group()in the pattern.I think itβs pulling the date out of the square brackets and then the text after the square brackets, is it? (The pattern is a regex).
(Was the hashtable explanation any help at all btw?)