advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

main.go (1090B)


      1 package main
      2 
      3 import (
      4 	"bufio"
      5 	"fmt"
      6 	"log"
      7 	"os"
      8 )
      9 
     10 func main() {
     11 	if len(os.Args) > 1 {
     12 		day02(os.Args[1])
     13 	} else if len(os.Args) == 1 {
     14 		fmt.Printf("usage: %s inputfile.txt\n", os.Args[0])
     15 	} else {
     16 		fmt.Println("No input data")
     17 	}
     18 }
     19 
     20 func day02(input_file string) {
     21 	fmt.Printf("day 02 input filename: %s\n", input_file)
     22 	input, err := os.Open(input_file)
     23 	if err != nil {
     24 		log.Fatal(err)
     25 	}
     26 	defer input.Close()
     27 	scanner := bufio.NewScanner(input)
     28 	gameTable := map[string]int{
     29 		"A X": 1 + 3,
     30 		"A Y": 2 + 6,
     31 		"A Z": 3 + 0,
     32 		"B X": 1 + 0,
     33 		"B Y": 2 + 3,
     34 		"B Z": 3 + 6,
     35 		"C X": 1 + 6,
     36 		"C Y": 2 + 0,
     37 		"C Z": 3 + 3,
     38 	}
     39 	riggedTable := map[string]int{
     40 		"A X": 3 + 0,
     41 		"A Y": 1 + 3,
     42 		"A Z": 2 + 6,
     43 		"B X": 1 + 0,
     44 		"B Y": 2 + 3,
     45 		"B Z": 3 + 6,
     46 		"C X": 2 + 0,
     47 		"C Y": 3 + 3,
     48 		"C Z": 1 + 6,
     49 	}
     50 
     51 	score1 := 0
     52 	score2 := 0
     53 	for scanner.Scan() {
     54 		score1 += gameTable[scanner.Text()]
     55 		score2 += riggedTable[scanner.Text()]
     56 	}
     57 	if err = scanner.Err(); err != nil {
     58 		log.Fatal(err)
     59 	}
     60 	fmt.Printf("part 1 score: %d\n", score1)
     61 	fmt.Printf("part 2 score: %d\n", score2)
     62 }