day2, part1

This commit is contained in:
efim
2023-12-02 16:16:51 +00:00
parent 7e06806a2b
commit 0f9d7ccbe9
2 changed files with 106 additions and 4 deletions

View File

@@ -17,7 +17,7 @@ var Restriction map[string]int = map[string]int{
type Game struct {
Num int
Sets [3]map[string]int
Sets []map[string]int
}
// now i need to encode the restriction,
@@ -25,7 +25,7 @@ type Game struct {
// scan though input lines, keep only numbers of games that pass the restriction, or rather sum them
func Run() int {
filename := "./day2/example"
filename := "./day2/input"
file, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("error opening file", filename))
@@ -62,6 +62,7 @@ func (g Game)isPossible(restriction map[string]int) bool {
func ReadLine(line string) Game {
result := Game{}
result.Sets = make([]map[string]int, 0)
infoIndex := strings.IndexRune(line, ':')
gameTitle := line[0:infoIndex]
@@ -73,8 +74,8 @@ func ReadLine(line string) Game {
result.Num = titleNum
setsLints := strings.Split(line[infoIndex+1:], ";")
for i, set := range setsLints {
result.Sets[i] = readSet(set)
for _, set := range setsLints {
result.Sets = append(result.Sets, readSet(set))
}
return result
}
@@ -82,6 +83,7 @@ func ReadLine(line string) Game {
// line like this
// 8 green, 6 blue, 20 red
func readSet(setLine string) map[string]int {
log.Printf("> reading set: %s", setLine)
result := make(map[string]int)
itemLines := strings.Split(setLine, ",")
for _, line := range itemLines {