Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created December 4, 2023 12:09
Show Gist options
  • Save Micrified/8597d90f310ad1589f9e4b16192a318d to your computer and use it in GitHub Desktop.
Save Micrified/8597d90f310ad1589f9e4b16192a318d to your computer and use it in GitHub Desktop.
Day 2 Advent of Code (part 1)
package main
import (
"fmt"
"bufio"
"os"
"strings"
)
func gamePossible (line string, constraints map[string]int) bool {
rounds := strings.Split(line, ";")
label, count := "", 0
for _, round := range rounds {
games := strings.Split(round, ",")
for _, game := range games {
fmt.Sscanf(game, "%d %s", &count, &label)
if count > constraints[label] {
return false
}
}
}
return true
}
func main() {
s, n := bufio.NewScanner(os.Stdin), 0
constraints := map[string]int {
"red" : 12,
"green" : 13,
"blue" : 14,
}
for i := 1; s.Scan(); i++ {
v := strings.Split(s.Text(), ":")
if gamePossible(v[1], constraints) {
n += i
}
}
fmt.Printf("%d\n", n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment