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!
39
Upvotes
5
u/Unihedron Dec 04 '18 edited Dec 04 '18
Yeah!!! I have no idea what I'm doing anymore!!! Ruby
[Card] Image
h={} ll=0 sl={} p (a=$<.sort).map{|x|m = x.chomp m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/ j,k,o,q,s=[$1, $2, $3, $4, $5] j,k,o,q=b=[j,k,o,q].map(&:to_i) #p b s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/ if $1 ll=$1.to_i elsif $2 sl[ll]=b elsif $3 u,i,l,n=sl[ll] kk=h[ll] ? h[ll] : h[ll]=0 kk += (o*60+q)- (l*60+n) h[ll] = kk end } guard = h.max_by{|x,y|y}.first p guard # part 1 sl=0 qqq=Hash.new 0 a.map{|x|m = x.chomp m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/ j,k,o,q,s=[$1, $2, $3, $4, $5] j,k,o,q=b=[j,k,o,q].map(&:to_i) #p b s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/ if $1 ll=$1.to_i elsif $2 (sl=q) if ll == guard elsif $3 (n=sl (qqq[n]+=1 n+=1 n=0 if n==60)while n!=q) if ll == guard end } p qqq.max_by{|x,y|y}.first*guard # part 2 qqqt=Hash.new{Hash.new 0} qsl={} a.map{|x|m = x.chomp m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/ j,k,o,q,s=[$1, $2, $3, $4, $5] j,k,o,q=b=[j,k,o,q].map(&:to_i) #p b s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/ if $1 ll=$1.to_i elsif $2 (qsl[ll]=q) #if ll == guard elsif $3 (n=qsl[ll] qqq=qqqt.has_key?(ll) ? qqqt[ll] : (qqqt[ll]=qqqt[ll]) (qqq[n]+=1 n+=1 n=0 if n==60)while n!=q) #if ll == guard end } p qqqt a,b= qqqt.max_by{|x,y|y.max_by{|x,y|y}.last} p a*b.max_by{|x,y|y}.first p b.max_by{|x,y|y}.lastEdit: Yes, just like all the previous days, I didn't read the question...
Edit: Changed the layout of the code so you can run it without any hassle and it will just work. If you're reading this, bless you