Skip to content

Instantly share code, notes, and snippets.

@Nv7-GitHub
Created December 5, 2021 06:20
Show Gist options
  • Save Nv7-GitHub/63d27397733c252008a61ca517e351e1 to your computer and use it in GitHub Desktop.
Save Nv7-GitHub/63d27397733c252008a61ca517e351e1 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 4
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input.txt
var input string
type Board struct {
Vals [5][5]int
Selected [5][5]bool
}
func (b *Board) Add(val int) bool {
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
if b.Vals[row][col] == val {
b.Selected[row][col] = true
}
}
}
// Check rows
for row := 0; row < 5; row++ {
ok := true
for col := 0; col < 5; col++ {
if !b.Selected[row][col] {
ok = false
break
}
}
if ok {
return true
}
}
// Check cols
for col := 0; col < 5; col++ {
ok := true
for row := 0; row < 5; row++ {
if !b.Selected[row][col] {
ok = false
break
}
}
if ok {
return true
}
}
return false
}
func (b *Board) SumUnmarked() int {
sum := 0
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
if !b.Selected[row][col] {
sum += b.Vals[row][col]
}
}
}
return sum
}
func NewBoard() *Board {
return &Board{
Vals: [5][5]int{},
Selected: [5][5]bool{},
}
}
func main() {
chunks := strings.Split(input, "\n\n")
inps := strings.Split(chunks[0], ",")
boards := make([]*Board, len(chunks[1:]))
for i, inp := range chunks[1:] {
boards[i] = NewBoard()
for row, val := range strings.Split(inp, "\n") {
nums := strings.Fields(val)
for col, num := range nums {
var err error
boards[i].Vals[row][col], err = strconv.Atoi(strings.TrimSpace(num))
if err != nil {
panic(err)
}
}
}
}
// Go through guesses
for _, inp := range inps {
num, err := strconv.Atoi(strings.TrimSpace(inp))
if err != nil {
panic(err)
}
for _, board := range boards {
if board.Add(num) {
// Calculate score
score := board.SumUnmarked()
fmt.Println(score * num)
return
}
}
}
}
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input.txt
var input string
type Board struct {
Vals [5][5]int
Selected [5][5]bool
}
func (b *Board) Add(val int) bool {
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
if b.Vals[row][col] == val {
b.Selected[row][col] = true
}
}
}
// Check rows
for row := 0; row < 5; row++ {
ok := true
for col := 0; col < 5; col++ {
if !b.Selected[row][col] {
ok = false
break
}
}
if ok {
return true
}
}
// Check cols
for col := 0; col < 5; col++ {
ok := true
for row := 0; row < 5; row++ {
if !b.Selected[row][col] {
ok = false
break
}
}
if ok {
return true
}
}
return false
}
func (b *Board) SumUnmarked() int {
sum := 0
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
if !b.Selected[row][col] {
sum += b.Vals[row][col]
}
}
}
return sum
}
func NewBoard() *Board {
return &Board{
Vals: [5][5]int{},
Selected: [5][5]bool{},
}
}
func main() {
chunks := strings.Split(input, "\n\n")
inps := strings.Split(chunks[0], ",")
boards := make([]*Board, len(chunks[1:]))
for i, inp := range chunks[1:] {
boards[i] = NewBoard()
for row, val := range strings.Split(inp, "\n") {
nums := strings.Fields(val)
for col, num := range nums {
var err error
boards[i].Vals[row][col], err = strconv.Atoi(strings.TrimSpace(num))
if err != nil {
panic(err)
}
}
}
}
// Go through guesses
finished := make(map[int]bool)
for _, inp := range inps {
num, err := strconv.Atoi(strings.TrimSpace(inp))
if err != nil {
panic(err)
}
for i, board := range boards {
if board.Add(num) {
finished[i] = true
if len(finished) == len(boards) {
fmt.Println(num)
score := board.SumUnmarked()
fmt.Println(score * num)
return
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment