github.com/vpayno/adventofcode-2022-golang-workspace@v0.0.0-20230605190011-dbafed5593de/internal/day02/init.go (about)

     1  // Package day02 is the module with the cli logic for the cmd application.
     2  package day02
     3  
     4  // Config holds the application's configuration.
     5  type Config struct {
     6  	appName       string
     7  	inputFileName string
     8  }
     9  
    10  // record ids (columns in input file)
    11  const (
    12  	them int = iota
    13  	goal
    14  )
    15  
    16  // Outcome describes the possible outcomes.
    17  type outcome int
    18  
    19  const (
    20  	loose outcome = 0
    21  	draw  outcome = 3
    22  	win   outcome = 6
    23  )
    24  
    25  var outcomeNames = map[outcome]string{
    26  	loose: "loose",
    27  	draw:  "draw",
    28  	win:   "win",
    29  }
    30  
    31  // Move descrives possible moves.
    32  type move int
    33  
    34  const (
    35  	rock     move = 1
    36  	paper    move = 2
    37  	scissors move = 3
    38  )
    39  
    40  var moveNames = map[move]string{
    41  	rock:     "rock",
    42  	paper:    "paper",
    43  	scissors: "scissors",
    44  }
    45  
    46  var string2move = map[string]move{
    47  	"A": rock,
    48  	"B": paper,
    49  	"C": scissors,
    50  }
    51  
    52  var string2outcome = map[string]outcome{
    53  	"X": loose,
    54  	"Y": draw,
    55  	"Z": win,
    56  }
    57  
    58  // Round describes one round of rock-paper-scissors.
    59  type round struct {
    60  	them move
    61  	goal outcome
    62  }
    63  
    64  func (r *round) yourMove() move {
    65  	var you move
    66  
    67  	switch r.goal {
    68  	case loose:
    69  		switch r.them {
    70  		case rock:
    71  			you = scissors
    72  		case paper:
    73  			you = rock
    74  		case scissors:
    75  			you = paper
    76  		}
    77  	case win:
    78  		switch r.them {
    79  		case rock:
    80  			you = paper
    81  		case paper:
    82  			you = scissors
    83  		case scissors:
    84  			you = rock
    85  		}
    86  	default:
    87  		you = r.them
    88  	}
    89  
    90  	return you
    91  }
    92  
    93  func (r *round) score() int {
    94  	return int(r.goal) + int(r.yourMove())
    95  }
    96  
    97  func (r *round) update(slice []move) {
    98  	r.them = slice[them]
    99  	r.goal = outcome(int(slice[goal]))
   100  }
   101  
   102  type rounds []round
   103  
   104  // Setup creates the applications configuration object.
   105  func Setup(appName string) Config {
   106  
   107  	conf := Config{
   108  		appName:       appName,
   109  		inputFileName: "data/" + appName + "/" + appName + "-input.txt",
   110  	}
   111  
   112  	return conf
   113  }