Alexander Avery
1 year ago
2 changed files with 86 additions and 0 deletions
@ -0,0 +1,68 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"bufio" |
||||
|
"fmt" |
||||
|
"io" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
func validgame(totalred, totalgreen, totalblue int, r io.Reader) bool { |
||||
|
scanner := bufio.NewScanner(r) |
||||
|
scanner.Split(bufio.ScanWords) |
||||
|
|
||||
|
for { |
||||
|
if !scanner.Scan() { |
||||
|
break |
||||
|
} |
||||
|
|
||||
|
num, _ := strconv.Atoi(scanner.Text()) |
||||
|
|
||||
|
scanner.Scan() |
||||
|
switch scanner.Text() { |
||||
|
case "red": |
||||
|
if num > totalred { |
||||
|
return false |
||||
|
} |
||||
|
case "green": |
||||
|
if num > totalgreen { |
||||
|
return false |
||||
|
} |
||||
|
case "blue": |
||||
|
if num > totalblue { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
type day02 struct{} |
||||
|
|
||||
|
func (d day02) solve1(r io.Reader) string { |
||||
|
var ( |
||||
|
totalred = 12 |
||||
|
totalgreen = 13 |
||||
|
totalblue = 14 |
||||
|
gameSum = 0 |
||||
|
scanner = bufio.NewScanner(r) |
||||
|
replacer = strings.NewReplacer(",", " ", ";", " ") |
||||
|
) |
||||
|
|
||||
|
for gameN := 1; scanner.Scan(); gameN++ { |
||||
|
line := scanner.Text() |
||||
|
_, game, _ := strings.Cut(line, ": ") |
||||
|
sr := strings.NewReader(replacer.Replace(game)) |
||||
|
if validgame(totalred, totalgreen, totalblue, sr) { |
||||
|
gameSum += gameN |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return fmt.Sprintf("%d", gameSum) |
||||
|
} |
||||
|
|
||||
|
func (d day02) solve2(r io.Reader) string { |
||||
|
return "" |
||||
|
} |
Loading…
Reference in new issue