solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.1 KiB

12 months ago
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 ""
}