~rootmos/AoC

fa3aea99f74e23108f52f4f6dac30d67902e5d94 — Gustav Behm 4 months ago 15838ef
Add solution to AoC 2023 #02ab
5 files changed, 224 insertions(+), 1 deletions(-)

M 2023/01.go
A 2023/02.example
A 2023/02.go
A 2023/02.input
M templates/template.go
M 2023/01.go => 2023/01.go +1 -0
@@ 5,6 5,7 @@ import (
	"os"
	"bufio"
	"strings"
	"fmt"
)

func getLines(fn string) (ls []string) {

A 2023/02.example => 2023/02.example +5 -0
@@ 0,0 1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

A 2023/02.go => 2023/02.go +85 -0
@@ 0,0 1,85 @@
package main

import (
	"fmt"
	"log"
	"os"
	"bufio"
	"strings"
)

func getLines(fn string) (ls []string) {
	f, err := os.Open(fn)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	for i := 0; s.Scan(); i++ {
		l := s.Text()
		ls = append(ls, l)
		//log.Printf("%s:%d: %s", fn, i, l)
	}
	return
}

func partA(fn string) {
	var a int
	for _, l := range getLines(fn) {
		xs := strings.Split(l, ":")
		var g int
		fmt.Sscanf(xs[0], "Game %d", &g)
		for _, y := range strings.Split(xs[1], ";") {
			for _, z := range strings.Split(y, ",") {
				var n int
				var c string
				fmt.Sscan(z, &n, &c)
				switch c {
				case "red": if n > 12 { 
					goto skip
				}
				case "green": if n > 13 { 
					goto skip
				}
				case "blue": if n > 14 { 
					goto skip
				}
				}
			}
		}
		a += g
		skip:
	}
	log.Printf("%s: %v", fn, a)
}

func partB(fn string) {
	var s int
	for _, l := range getLines(fn) {
		xs := strings.Split(l, ":")
		var r, g, b int
		for _, y := range strings.Split(xs[1], ";") {
			for _, z := range strings.Split(y, ",") {
				var n int
				var c string
				fmt.Sscan(z, &n, &c)
				switch c {
				case "red": if n > r { r = n }
				case "green": if n > g { g = n }
				case "blue": if n > b { b = n }
				}
			}
		}
		s += r*g*b
	}
	log.Printf("%s: %v", fn, s)
}

func main() {
	partA("02.example") // 8
	partA("02.input") // 2593

	partB("02.example") // 2286
	partB("02.input") // 54699
}

A 2023/02.input => 2023/02.input +100 -0
@@ 0,0 1,100 @@
Game 1: 13 green, 3 red; 4 red, 9 green, 4 blue; 9 green, 10 red, 2 blue
Game 2: 3 red, 8 green, 1 blue; 4 green, 11 blue, 2 red; 3 blue, 2 red, 6 green; 5 green, 15 blue, 1 red; 2 blue, 2 red, 5 green; 12 blue, 7 green, 2 red
Game 3: 1 red, 9 green, 3 blue; 8 green, 4 red, 11 blue; 6 red, 10 blue; 6 green, 6 red, 12 blue; 2 blue, 11 green, 7 red; 12 blue, 9 green, 8 red
Game 4: 7 red, 2 green, 1 blue; 12 green; 12 green
Game 5: 15 red, 3 green, 1 blue; 6 red, 2 blue, 2 green; 3 green, 3 red, 1 blue; 2 blue, 13 red, 5 green; 2 green, 15 red, 2 blue
Game 6: 3 blue, 15 red, 1 green; 8 green, 5 red, 6 blue; 9 green, 5 blue, 6 red; 9 green, 3 blue, 9 red; 10 green, 14 red, 2 blue
Game 7: 6 green, 1 red, 11 blue; 6 green, 1 red, 3 blue; 4 green, 20 blue; 2 red, 5 blue, 4 green; 10 green, 17 blue
Game 8: 10 blue, 9 green, 10 red; 9 green, 1 red; 8 red, 9 green, 9 blue; 5 green, 3 red, 7 blue
Game 9: 7 blue, 1 red; 5 red, 4 green; 4 green, 6 red, 5 blue; 2 green, 4 blue; 3 green, 6 blue, 4 red; 1 green, 3 red, 3 blue
Game 10: 2 red, 2 green, 2 blue; 10 blue, 2 red, 1 green; 2 green, 9 blue, 3 red
Game 11: 8 red, 4 blue, 1 green; 3 red; 1 green; 2 green, 3 blue
Game 12: 10 red, 2 green, 4 blue; 4 red, 2 green; 1 blue, 1 red, 1 green; 10 red, 1 green, 5 blue
Game 13: 20 blue, 9 green, 7 red; 13 red, 13 blue, 16 green; 17 blue, 6 red, 6 green; 1 red, 1 blue, 9 green; 9 blue, 18 green, 7 red
Game 14: 6 blue, 14 red; 9 red, 8 blue; 2 red, 1 green, 8 blue; 3 blue, 1 green, 9 red; 8 blue, 2 green, 1 red
Game 15: 3 red, 1 blue, 5 green; 2 red, 3 green; 5 red, 5 green, 1 blue; 2 green, 6 red; 4 red, 1 blue; 6 red, 1 blue, 1 green
Game 16: 5 blue, 7 red, 2 green; 7 red, 12 blue; 10 blue, 11 green, 5 red; 11 red, 11 blue, 10 green
Game 17: 3 red, 7 blue; 1 blue, 14 green, 4 red; 11 blue, 4 red, 11 green; 18 blue, 5 red, 11 green; 18 blue, 1 red, 8 green
Game 18: 8 green, 2 red, 6 blue; 8 blue, 11 green; 2 red, 11 blue, 9 green
Game 19: 11 red, 9 green, 3 blue; 19 green, 9 red, 2 blue; 19 green, 4 blue, 4 red; 1 green, 11 red; 10 red, 2 green, 4 blue
Game 20: 2 blue, 3 red, 1 green; 1 red, 3 green; 7 blue, 1 green, 4 red; 1 red, 8 blue, 7 green; 6 blue, 3 red; 5 red, 3 blue, 7 green
Game 21: 1 green, 1 blue, 10 red; 1 green, 5 red, 8 blue; 11 red, 4 blue; 6 blue, 6 red
Game 22: 6 blue, 6 green; 8 green, 15 blue; 8 green, 3 blue, 1 red; 11 blue, 2 red, 7 green
Game 23: 1 green, 3 blue, 7 red; 4 red, 1 green, 2 blue; 3 red, 2 blue, 2 green
Game 24: 4 green, 8 blue, 4 red; 2 green, 9 blue; 4 green, 1 red; 2 green, 5 blue, 1 red; 2 blue, 3 red, 3 green; 6 blue
Game 25: 7 blue; 15 blue, 5 red; 6 blue, 12 red; 1 green, 17 red; 13 blue, 5 red; 17 red
Game 26: 1 blue, 3 green, 7 red; 9 red, 4 green, 1 blue; 1 red, 2 green, 1 blue; 11 red, 3 green; 10 red, 4 green, 2 blue; 6 red, 4 green
Game 27: 4 blue, 6 red; 2 blue, 8 red, 1 green; 3 blue, 3 red; 2 red, 1 blue; 1 green, 3 blue, 6 red
Game 28: 1 red, 7 blue, 7 green; 2 green, 1 red, 4 blue; 8 green, 2 red; 2 red, 7 blue, 5 green; 12 green, 5 blue, 2 red; 1 red, 1 green, 2 blue
Game 29: 10 green, 3 red, 6 blue; 9 green, 6 blue, 4 red; 3 red, 2 blue, 17 green
Game 30: 8 blue; 15 blue, 1 red; 10 green, 2 red, 13 blue
Game 31: 10 green, 2 blue, 7 red; 2 green, 1 blue; 1 blue, 15 green, 2 red; 7 green, 2 blue; 3 blue, 6 green, 8 red; 6 red, 1 blue
Game 32: 2 blue, 2 red, 11 green; 10 green, 2 red, 1 blue; 1 green, 2 blue; 2 red, 9 green, 2 blue; 2 blue, 1 green; 5 green, 1 blue, 2 red
Game 33: 8 red, 6 blue; 2 green, 3 red, 2 blue; 1 green, 13 red, 18 blue
Game 34: 7 blue, 5 green; 5 green, 8 blue; 13 blue, 15 red, 2 green
Game 35: 1 blue, 2 green; 9 green; 4 red, 14 green; 1 red, 1 blue, 17 green
Game 36: 2 red, 14 green, 4 blue; 13 green, 3 blue; 1 blue, 7 green, 2 red; 4 blue, 9 green; 1 green, 3 blue, 1 red; 2 red, 4 blue, 10 green
Game 37: 2 blue, 7 green, 5 red; 5 green, 2 blue; 6 blue, 11 red
Game 38: 6 green, 6 red; 9 red, 10 green; 2 blue, 8 green, 8 red
Game 39: 10 red, 3 blue; 5 green, 3 red; 5 red, 7 green
Game 40: 5 red, 14 green, 2 blue; 5 red, 7 blue, 12 green; 2 green, 4 red; 1 red, 16 green, 3 blue; 16 green, 4 red, 7 blue; 9 green, 2 red
Game 41: 4 red, 3 green, 2 blue; 13 green, 6 blue; 2 red, 14 green, 1 blue; 7 blue, 2 red, 14 green
Game 42: 4 red; 1 blue, 5 red; 1 green, 6 red; 1 red, 1 blue; 3 blue, 8 red
Game 43: 7 blue, 16 red, 1 green; 2 red, 6 green, 1 blue; 5 green, 3 red; 5 green, 9 blue, 2 red; 3 red, 9 blue, 4 green; 7 red, 9 blue
Game 44: 2 red, 2 green; 5 red, 1 blue, 8 green; 7 green, 3 blue, 5 red
Game 45: 8 blue, 16 red; 8 blue; 4 blue, 1 green, 8 red
Game 46: 11 green, 9 blue, 1 red; 8 green, 7 blue; 10 blue, 1 red, 1 green; 12 green, 10 blue
Game 47: 3 green, 6 red, 1 blue; 2 blue, 2 green, 12 red; 3 red, 2 green, 1 blue
Game 48: 3 red, 3 green, 3 blue; 3 red, 4 green, 2 blue; 2 green, 7 red, 1 blue; 2 red, 3 blue, 5 green
Game 49: 5 red, 7 blue, 5 green; 10 red, 4 green, 7 blue; 9 red, 17 green; 6 green, 1 red, 2 blue; 7 green, 8 blue, 5 red
Game 50: 2 red, 4 green, 16 blue; 4 blue, 3 red, 8 green; 4 blue, 2 red, 6 green
Game 51: 16 green, 10 red, 14 blue; 8 red, 4 blue, 12 green; 14 green, 7 blue; 6 red, 20 green, 3 blue
Game 52: 1 red, 1 blue, 1 green; 9 green, 9 red; 4 green, 13 red; 7 red, 11 green; 4 red, 1 blue; 8 green, 3 red, 1 blue
Game 53: 4 green, 11 blue; 9 green, 2 red; 6 red, 18 green, 13 blue; 6 red, 2 blue, 14 green
Game 54: 1 green, 1 red, 1 blue; 2 green, 4 blue; 4 blue, 5 green; 3 blue, 1 red, 10 green
Game 55: 8 blue, 2 red, 3 green; 9 red, 11 blue; 1 green, 12 blue, 4 red; 3 green, 17 red; 3 red, 3 green, 15 blue; 7 blue, 7 red, 2 green
Game 56: 3 blue, 13 green; 9 green, 2 blue; 1 red, 2 blue, 16 green
Game 57: 6 blue, 4 red; 3 green, 6 red; 2 red, 3 blue, 3 green; 8 red, 5 blue
Game 58: 4 red, 15 green, 5 blue; 1 red, 16 blue, 14 green; 2 green, 17 blue, 6 red; 20 blue, 3 red, 7 green; 17 green, 1 red, 12 blue
Game 59: 3 blue, 14 red; 5 green, 10 red, 2 blue; 2 blue, 5 red, 6 green
Game 60: 4 red, 1 blue, 1 green; 15 blue; 8 green, 14 blue, 4 red; 9 blue, 3 green, 4 red; 4 green, 2 red, 11 blue; 4 blue, 7 green
Game 61: 5 green, 9 blue, 16 red; 4 blue, 12 green, 4 red; 17 red, 7 green, 5 blue; 19 blue, 12 red, 17 green; 8 green, 13 red
Game 62: 13 green, 1 red, 7 blue; 9 blue, 1 red, 4 green; 14 green, 2 red, 2 blue; 3 green
Game 63: 6 green; 7 red, 3 blue, 8 green; 5 blue, 1 green, 6 red; 6 green, 6 red, 2 blue; 8 green, 2 blue
Game 64: 16 blue, 1 red, 2 green; 4 green, 1 blue, 6 red; 6 green, 2 blue, 2 red; 17 blue; 1 red; 13 blue, 6 green, 1 red
Game 65: 8 red, 3 green, 7 blue; 6 blue, 8 red, 2 green; 2 blue, 3 green, 17 red
Game 66: 2 blue, 3 green, 3 red; 3 red, 2 blue; 5 red, 4 green, 3 blue; 1 blue, 3 green; 2 red, 1 green, 1 blue; 2 blue, 4 green
Game 67: 2 red, 3 blue, 15 green; 2 blue, 2 red, 17 green; 4 blue, 3 red, 2 green; 6 red; 3 red, 8 green
Game 68: 7 red, 1 blue, 12 green; 17 red, 1 green; 10 red, 8 green; 16 red, 5 green, 2 blue; 4 red, 1 blue, 8 green; 8 green, 7 red, 2 blue
Game 69: 17 green, 9 red, 2 blue; 1 blue, 14 green, 3 red; 9 red, 12 green, 2 blue; 11 green, 2 blue, 7 red
Game 70: 1 green, 8 blue, 2 red; 2 red, 10 green, 1 blue; 1 red, 12 green, 6 blue; 9 green, 4 blue, 4 red; 2 red, 6 green; 3 red, 8 green, 6 blue
Game 71: 1 red, 5 blue; 12 blue, 3 red; 3 red, 2 green, 4 blue; 5 blue, 3 green, 1 red
Game 72: 11 red, 6 blue; 1 red, 1 blue, 1 green; 2 blue, 7 red; 18 blue, 3 red; 1 green, 1 blue, 12 red
Game 73: 4 red, 2 blue, 1 green; 3 red; 5 red, 1 blue; 4 blue, 6 red
Game 74: 2 red; 2 red, 5 green; 4 green, 1 red, 1 blue; 1 blue, 5 green, 5 red; 7 red, 1 blue, 3 green; 8 red, 1 blue, 6 green
Game 75: 13 blue, 2 red, 2 green; 2 red, 9 blue; 2 red, 9 blue, 5 green
Game 76: 2 red, 3 green, 18 blue; 2 red, 11 green, 5 blue; 6 green, 8 blue, 2 red; 4 blue; 7 green, 14 blue
Game 77: 5 blue, 8 red, 1 green; 2 blue, 5 green, 12 red; 3 red, 4 blue
Game 78: 1 blue, 2 green, 16 red; 2 red, 3 green; 1 red, 4 green, 2 blue; 11 red; 2 green, 12 red, 2 blue; 11 red, 5 green, 3 blue
Game 79: 10 green, 3 blue, 2 red; 8 red, 3 blue, 8 green; 5 green, 3 red, 11 blue; 9 green, 16 blue
Game 80: 1 red, 4 blue; 6 green, 1 red; 6 green, 3 blue, 1 red; 6 green, 2 red; 7 green, 1 blue; 2 red, 2 blue, 2 green
Game 81: 10 blue, 4 red, 4 green; 5 green, 1 red, 7 blue; 11 blue, 8 green, 2 red; 8 green, 2 red
Game 82: 12 green, 1 red, 3 blue; 6 red, 1 blue; 16 green, 3 red, 4 blue; 8 blue; 7 blue, 7 green, 2 red; 4 red, 19 green
Game 83: 4 red, 4 blue, 3 green; 8 blue, 4 green, 6 red; 6 green, 7 blue, 6 red; 11 red, 6 green, 7 blue
Game 84: 11 red, 2 green, 2 blue; 20 green, 2 blue, 13 red; 15 red, 6 green, 3 blue; 17 green, 7 red
Game 85: 3 blue, 5 green, 2 red; 12 green, 2 blue, 1 red; 7 blue, 6 green, 5 red; 11 red, 2 blue, 17 green; 11 blue, 11 red, 17 green; 18 green, 9 red, 13 blue
Game 86: 1 blue, 14 red; 4 green, 1 blue, 3 red; 2 green, 1 blue, 13 red; 1 green, 1 blue, 10 red
Game 87: 2 red, 5 green, 4 blue; 3 blue, 9 red, 6 green; 7 blue, 9 red, 11 green; 10 green, 11 red, 9 blue; 7 green, 12 red, 4 blue; 5 blue, 1 red, 7 green
Game 88: 11 red, 1 green; 9 blue, 4 green, 7 red; 10 red, 4 green, 1 blue; 4 green, 1 red, 1 blue; 10 blue, 1 red, 3 green; 2 green, 12 blue, 11 red
Game 89: 3 green, 3 blue; 1 red, 7 green, 9 blue; 8 red, 11 blue, 11 green; 2 green, 6 blue, 5 red; 5 blue, 9 green
Game 90: 3 blue, 10 red, 2 green; 2 blue; 8 red
Game 91: 2 red, 10 green, 2 blue; 9 blue; 8 green, 5 red, 10 blue; 7 green, 6 blue, 5 red; 1 green, 6 red, 12 blue; 1 red, 4 green, 3 blue
Game 92: 12 blue, 5 red, 2 green; 4 blue, 1 red, 3 green; 6 red, 6 blue; 1 blue, 8 red, 6 green; 6 blue, 3 red, 2 green; 7 green, 4 red, 1 blue
Game 93: 3 blue; 8 blue; 3 blue, 2 red; 2 red, 1 green
Game 94: 5 red, 7 blue, 6 green; 15 red, 7 blue, 4 green; 6 blue, 1 red, 2 green; 7 green, 4 blue, 17 red; 12 red, 5 green, 1 blue
Game 95: 7 blue, 11 red, 9 green; 10 red, 6 blue, 7 green; 6 blue, 6 red, 7 green
Game 96: 2 red, 1 green, 3 blue; 3 blue, 1 green; 1 green, 1 blue; 1 red, 1 blue; 1 green, 1 red, 4 blue
Game 97: 6 red, 1 blue, 7 green; 2 blue, 5 red, 7 green; 8 red, 3 blue, 6 green; 6 green, 1 red, 3 blue; 5 red, 2 blue, 14 green; 3 green, 6 red, 6 blue
Game 98: 9 red, 14 blue; 19 red, 4 blue; 11 red, 17 blue; 14 blue, 1 green, 18 red
Game 99: 1 green, 1 red, 12 blue; 2 green, 4 red, 14 blue; 4 blue, 6 red; 10 red, 2 green, 1 blue
Game 100: 5 red, 9 green, 2 blue; 9 blue, 6 green, 1 red; 8 blue, 7 green, 3 red

M templates/template.go => templates/template.go +33 -1
@@ 2,8 2,40 @@ package main

import (
	"log"
	"os"
	"bufio"
)

func getLines(fn string) (ls []string) {
	f, err := os.Open(fn)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	for i := 0; s.Scan(); i++ {
		l := s.Text()
		ls = append(ls, l)
		//log.Printf("%s:%d: %s", fn, i, l)
	}
	return
}

func partA(fn string) {
	var a int
	log.Printf("%s: %v", fn, a)
}

func partB(fn string) {
	var b int
	log.Printf("%s: %v", fn, b)
}

func main() {
	log.Print("hello")
	partA("02.example")
	//partA("02.input")

	//partB("02.example")
	//partB("02.input")
}