|
@ -8,7 +8,9 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func validgame(totalred, totalgreen, totalblue int, r io.Reader) bool { |
|
|
var replacer = strings.NewReplacer(",", " ", ";", " ") |
|
|
|
|
|
|
|
|
|
|
|
func maxgame(r io.Reader) (red, green, blue int) { |
|
|
scanner := bufio.NewScanner(r) |
|
|
scanner := bufio.NewScanner(r) |
|
|
scanner.Split(bufio.ScanWords) |
|
|
scanner.Split(bufio.ScanWords) |
|
|
|
|
|
|
|
@ -17,26 +19,26 @@ func validgame(totalred, totalgreen, totalblue int, r io.Reader) bool { |
|
|
break |
|
|
break |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
num, _ := strconv.Atoi(scanner.Text()) |
|
|
n, _ := strconv.Atoi(scanner.Text()) |
|
|
|
|
|
|
|
|
scanner.Scan() |
|
|
scanner.Scan() |
|
|
switch scanner.Text() { |
|
|
switch scanner.Text() { |
|
|
case "red": |
|
|
case "red": |
|
|
if num > totalred { |
|
|
if n > red { |
|
|
return false |
|
|
red = n |
|
|
} |
|
|
} |
|
|
case "green": |
|
|
case "green": |
|
|
if num > totalgreen { |
|
|
if n > green { |
|
|
return false |
|
|
green = n |
|
|
} |
|
|
} |
|
|
case "blue": |
|
|
case "blue": |
|
|
if num > totalblue { |
|
|
if n > blue { |
|
|
return false |
|
|
blue = n |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
return true |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type day02 struct{} |
|
|
type day02 struct{} |
|
@ -48,14 +50,14 @@ func (d day02) solve1(r io.Reader) string { |
|
|
totalblue = 14 |
|
|
totalblue = 14 |
|
|
gameSum = 0 |
|
|
gameSum = 0 |
|
|
scanner = bufio.NewScanner(r) |
|
|
scanner = bufio.NewScanner(r) |
|
|
replacer = strings.NewReplacer(",", " ", ";", " ") |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
for gameN := 1; scanner.Scan(); gameN++ { |
|
|
for gameN := 1; scanner.Scan(); gameN++ { |
|
|
line := scanner.Text() |
|
|
line := scanner.Text() |
|
|
_, game, _ := strings.Cut(line, ": ") |
|
|
_, game, _ := strings.Cut(line, ": ") |
|
|
sr := strings.NewReader(replacer.Replace(game)) |
|
|
sr := strings.NewReader(replacer.Replace(game)) |
|
|
if validgame(totalred, totalgreen, totalblue, sr) { |
|
|
red, green, blue := maxgame(sr) |
|
|
|
|
|
if red < totalred && green < totalgreen && blue < totalblue { |
|
|
gameSum += gameN |
|
|
gameSum += gameN |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -64,5 +66,18 @@ func (d day02) solve1(r io.Reader) string { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (d day02) solve2(r io.Reader) string { |
|
|
func (d day02) solve2(r io.Reader) string { |
|
|
return "" |
|
|
var ( |
|
|
|
|
|
sum = 0 |
|
|
|
|
|
scanner = bufio.NewScanner(r) |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
for scanner.Scan() { |
|
|
|
|
|
line := scanner.Text() |
|
|
|
|
|
_, game, _ := strings.Cut(line, ": ") |
|
|
|
|
|
sr := strings.NewReader(replacer.Replace(game)) |
|
|
|
|
|
red, green, blue := maxgame(sr) |
|
|
|
|
|
sum += (red * green * blue) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d", sum) |
|
|
} |
|
|
} |
|
|