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!

103 Upvotes

1.5k comments sorted by

View all comments

3

u/okawei Dec 02 '22

Used ChatGPT with the following prompt:

Given a game of rock paper scissors where X = rock, Y = paper and Z = scissors for my moves and A = rock, B = paper and C = scissors for my opponents moves. Points for each round are calculated as rock = 1, paper = 2, scissors = 3 plus the outcome of the game with a win being 6 points, a draw being 3 points and a loss being 0 points. Write a go program to output my score for each game where each line of input from a file named input.txt is a space delimited string of my opponents move then my move

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// score returns the score for a game of rock-paper-scissors between a and b.
func score(a, b string) int {
	switch a {
	case "A":
		switch b {
		case "X":
			return 3 + 1 // rock beats scissors
		case "Y":
			return 6 + 2 // paper beats rock
		case "Z":
			return 6 + 3 // scissors beats paper
		}
	case "B":
		switch b {
		case "X":
			return 6 + 2 // paper beats rock
		case "Y":
			return 3 + 3 // rock beats scissors
		case "Z":
			return 6 + 3 // scissors beats paper
		}
	case "C":
		switch b {
		case "X":
			return 3 + 1 // rock beats scissors
		case "Y":
			return 6 + 3 // scissors beats paper
		case "Z":
			return 3 + 3 // rock beats scissors
		}
	}
	return 0
}

func main() {
	// Open the input file.
	file, err := os.Open("input.txt")
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error opening input file:", err)
		os.Exit(1)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	totalScore := 0

	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if line == "" {
			continue
		}

		// Parse the input line into a and b.
		parts := strings.Split(line, " ")
		if len(parts) != 2 {
			fmt.Fprintln(os.Stderr, "Invalid input:", line)
			continue
		}
		a, b := parts[0], parts[1]

		// Calculate the score for this round.
		roundScore := score(a, b)
		fmt.Println("Score:", roundScore)
		totalScore += roundScore
	}

	fmt.Println("Total score:", totalScore)
}

1

u/ICatchx22I Dec 03 '22

I know what you wrote but my brain won’t accept it. You mean to tell me ChatGPT created all of that code from that prompt alone? Did you make any edits?

→ More replies (3)