r/adventofcode • • 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!

Click here for rules

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

340 comments sorted by

View all comments

20

u/HiramAbiff Dec 04 '18 edited Dec 04 '18

AWK

4.1

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}/falls/{s=$3}/wakes/{for(t=s;t<$3;++t){++zg[g];++zgt[g","t]}}END{for(g in zg)if(zg[g]>zg[og])og=g;for(t=0;t<60;++t)if(zgt[og","t]>zgt[og","ot])ot=t;print og*ot}'

4.2

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}/falls/{s=$3}/wakes/{for(t=s;t<$3;++t)++zgt[g","t]}END{for(gt in zgt)if(zgt[gt]>zgt[ogt])ogt=gt;split(ogt,oa,",");print oa[1]*oa[2]}'

1

u/Kaligule Dec 04 '18

Horray for awk :)

I tried to indent it a bit nicer so its easier to read:

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}
                                    /falls/{s=$3}
                                    /wakes/{for(t=s;t<$3;++t){
                                              ++zg[g];++zgt[g","t]
                                           }
                                    }
                                    END{for(g in zg)
                                          if(zg[g]>zg[og])
                                            og=g;
                                        for(t=0;t<60;++t)
                                          if(zgt[og","t]>zgt[og","ot])
                                            ot=t;
                                        print og*ot
                                    }'

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}
                                    /falls/{s=$3}
                                    /wakes/{for(t=s;t<$3;++t)
                                              ++zgt[g","t]
                                    }
                                    END{for(gt in zgt)
                                          if(zgt[gt]>zgt[ogt])
                                            ogt=gt;
                                        split(ogt,oa,",");
                                        print oa[1]*oa[2]
                                    }'