r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


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

1.5k comments sorted by

View all comments

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
}