Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created December 5, 2023 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Micrified/127fb9da51849c4f0ea9952377a962c2 to your computer and use it in GitHub Desktop.
Save Micrified/127fb9da51849c4f0ea9952377a962c2 to your computer and use it in GitHub Desktop.
Advent of Code; Exercise 2; Part 2
package main
import (
"fmt"
"bufio"
"os"
"strings"
"math"
)
func max (a, b int) int {
if a >= b {
return a
}
return b
}
func minPower (line string) int {
minMap := map[string]int{}
rounds := strings.Split(line, ";")
color, n := "", 0
for _, round := range rounds {
set := strings.Split(round, ",")
for _, draw := range set {
fmt.Sscanf(draw, "%d %s", &n, &color)
value, ok := minMap[color]
if !ok {
minMap[color] = n
} else {
minMap[color] = max(value, n)
}
}
}
p := 1
for _, min := range minMap {
p *= min
}
return p
}
func main() {
s, n := bufio.NewScanner(os.Stdin), 0
for i := 1; s.Scan(); i++ {
v := strings.Split(s.Text(), ":")
n += minPower(v[1])
}
fmt.Printf("%d\n", n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment