finished 2nd day
This commit is contained in:
parent
c17546495d
commit
15c31708d1
1 changed files with 59 additions and 16 deletions
65
day2/day2.go
65
day2/day2.go
|
@ -24,7 +24,14 @@ type Draw struct {
|
||||||
type Game struct {
|
type Game struct {
|
||||||
id int
|
id int
|
||||||
draws []Draw
|
draws []Draw
|
||||||
|
meta Meta
|
||||||
|
}
|
||||||
|
|
||||||
|
type Meta struct {
|
||||||
possible bool
|
possible bool
|
||||||
|
red_min int
|
||||||
|
green_min int
|
||||||
|
blue_min int
|
||||||
}
|
}
|
||||||
|
|
||||||
func readInput() string {
|
func readInput() string {
|
||||||
|
@ -49,22 +56,39 @@ func parseGameLine(line string, limits Limitations) Game {
|
||||||
|
|
||||||
id, _ := strconv.Atoi(strings.Split(game_prefix, " ")[1])
|
id, _ := strconv.Atoi(strings.Split(game_prefix, " ")[1])
|
||||||
draws := []Draw{}
|
draws := []Draw{}
|
||||||
possible := true
|
meta := Meta{}
|
||||||
|
red_min, green_min, blue_min := 0, 0, 0
|
||||||
|
meta.possible = true
|
||||||
for _, v := range draws_splitted {
|
for _, v := range draws_splitted {
|
||||||
draw := Draw{}
|
red, _ := strconv.Atoi(strings.Split(getColors(v, "red"), " ")[0])
|
||||||
draw.red, _ = strconv.Atoi(strings.Split(getColors(v, "red"), " ")[0])
|
green, _ := strconv.Atoi(strings.Split(getColors(v, "green"), " ")[0])
|
||||||
draw.green, _ = strconv.Atoi(strings.Split(getColors(v, "green"), " ")[0])
|
blue, _ := strconv.Atoi(strings.Split(getColors(v, "blue"), " ")[0])
|
||||||
draw.blue, _ = strconv.Atoi(strings.Split(getColors(v, "blue"), " ")[0])
|
if (red > limits.red) || (green > limits.green) || (blue > limits.blue) {
|
||||||
if (draw.red > limits.red) || (draw.green > limits.green) || (draw.blue > limits.blue) {
|
meta.possible = false
|
||||||
possible = false
|
}
|
||||||
|
if red_min < red {
|
||||||
|
red_min = red
|
||||||
|
}
|
||||||
|
if green_min < green {
|
||||||
|
green_min = green
|
||||||
|
}
|
||||||
|
if blue_min < blue {
|
||||||
|
blue_min = blue
|
||||||
|
}
|
||||||
|
draw := Draw{
|
||||||
|
red: red,
|
||||||
|
blue: blue,
|
||||||
|
green: green,
|
||||||
}
|
}
|
||||||
draws = append(draws, draw)
|
draws = append(draws, draw)
|
||||||
}
|
}
|
||||||
|
meta.blue_min = blue_min
|
||||||
|
meta.red_min = red_min
|
||||||
|
meta.green_min = green_min
|
||||||
game := Game{
|
game := Game{
|
||||||
id: id,
|
id: id,
|
||||||
draws: draws,
|
draws: draws,
|
||||||
possible: possible,
|
meta: meta,
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Println(game)
|
// fmt.Println(game)
|
||||||
|
@ -84,18 +108,37 @@ func part1(lines []string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
game := parseGameLine(l, limits)
|
game := parseGameLine(l, limits)
|
||||||
if game.possible {
|
if game.meta.possible {
|
||||||
games = append(games, game)
|
games = append(games, game)
|
||||||
sum_ids += game.id
|
sum_ids += game.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(sum_ids)
|
fmt.Println("Sum ids:", sum_ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(lines []string) {
|
||||||
|
var games []Game
|
||||||
|
|
||||||
|
for _, l := range lines {
|
||||||
|
if len(l) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
game := parseGameLine(l, Limitations{})
|
||||||
|
games = append(games, game)
|
||||||
|
}
|
||||||
|
sum := 0
|
||||||
|
for _, g := range games {
|
||||||
|
sum += (g.meta.red_min * g.meta.green_min * g.meta.blue_min)
|
||||||
|
}
|
||||||
|
fmt.Println(sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
input := readInput()
|
input := readInput()
|
||||||
lines := strings.Split(input, "\n")
|
lines := strings.Split(input, "\n")
|
||||||
|
fmt.Println("---PART 1---")
|
||||||
part1(lines)
|
part1(lines)
|
||||||
|
fmt.Println("---PART 2---")
|
||||||
|
part2(lines)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue