Created
December 5, 2023 09:00
-
-
Save Micrified/127fb9da51849c4f0ea9952377a962c2 to your computer and use it in GitHub Desktop.
Advent of Code; Exercise 2; Part 2
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" | |
"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