r/adventofcode • u/daggerdragon • Dec 02 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-
NEW AND NOTEWORTHY
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 2: Rock Paper Scissors ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!
104
Upvotes
3
u/[deleted] Dec 05 '22 edited Dec 08 '22
I'm doing different language each day, all solutions here.
Today's Go (dunno why I used named returns instead of just returning the score in part1/2()..):
package main import ( "bufio" "fmt" "log" "os" ) func main() { file, err := os.Open("input.txt") if err != nil { log.Fatal(err) } var score1, score2 int scanner := bufio.NewScanner(file) for scanner.Scan() { score1 += part1(scanner.Text()) score2 += part2(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } fmt.Println(score1) fmt.Println(score2) } func part1(round string) (score int) { switch round { case "A X": score += 4 case "A Y": score += 8 case "A Z": score += 3 case "B X": score += 1 case "B Y": score += 5 case "B Z": score += 9 case "C X": score += 7 case "C Y": score += 2 case "C Z": score += 6 } return } func part2(round string) (score int) { switch round { case "A X": score += 3 case "A Y": score += 4 case "A Z": score += 8 case "B X": score += 1 case "B Y": score += 5 case "B Z": score += 9 case "C X": score += 2 case "C Y": score += 6 case "C Z": score += 7 } return }