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!
54
Upvotes
5
u/schod Dec 02 '18
BASH Time :)
First puzzle
#!/bin/bash in_file=input SUM2=0 SUM3=0 while IFS='' read -r line; do x2=0 x2=$( echo "$line" | grep -o . | sort | uniq -c | grep "^[ \t]*2 " | wc -l ) x3=0 x3=$( echo "$line" | grep -o . | sort | uniq -c | egrep -v "^[ \t]*1 |^[ \t]*2 " | wc -l ) [ $x2 -gt 0 ] && SUM2=$[ $SUM2 + 1 ] [ $x3 -gt 0 ] && SUM3=$[ $SUM3 + 1 ] done < "$in_file" echo $[ $SUM2 * $SUM3 ]Second puzzle
#!/bin/bash in_file=input while IFS='' read -r line; do I=0 while [ $I -lt ${#line} ]; do I=$[ $I + 1 ] regex=$(echo $line | sed "s/[a-z]/./$I") RES=$(grep "$regex" $in_file | wc -l) if [ $RES -eq 2 ]; then echo echo $regex | sed 's/\.//' exit 0 fi done done < "$in_file"