Created
December 4, 2023 12:09
-
-
Save Micrified/8597d90f310ad1589f9e4b16192a318d to your computer and use it in GitHub Desktop.
Day 2 Advent of Code (part 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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