New Shared Library

This commit is contained in:
2021-12-16 21:11:39 +00:00
parent 1a2247044a
commit 6111f86a04
24 changed files with 559 additions and 392 deletions

View File

@ -1,16 +0,0 @@
package main
type Board struct {
Values [5][5]boardValue
Hash map[int]location
}
type boardValue struct {
Value int
Visited bool
}
type location struct {
x int
y int
}

View File

@ -1,6 +1,7 @@
package main
import (
"AdventOfCode2021/shared"
"bufio"
"fmt"
"os"
@ -16,8 +17,8 @@ func main() {
fmt.Println(run(boards, nums))
}
func run(boards *[]Board, nums []int) int {
var completedBoards map[*Board]bool = make(map[*Board]bool)
func run(boards *[]shared.Board, nums []int) int {
var completedBoards map[*shared.Board]bool = make(map[*shared.Board]bool)
var bingo []int
var n int
for i := 0; i < len(nums); i++ {
@ -38,10 +39,10 @@ func run(boards *[]Board, nums []int) int {
return returnAnswer(&(*boards)[bingo[0]], n)
}
func loadBoards(content *[]string) (boards *[]Board, nums []int) {
boards = new([]Board)
newBoard := Board{
Hash: make(map[int]location, 25),
func loadBoards(content *[]string) (boards *[]shared.Board, nums []int) {
boards = new([]shared.Board)
newBoard := shared.Board{
Hash: make(map[int]shared.Location, 25),
}
boardNum := 0
row := 0
@ -67,8 +68,8 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
for j := 0; j < len(values); j++ {
value, _ := strconv.Atoi(values[j])
newBoard.Values[row][j] = boardValue{Value: value, Visited: false}
newBoard.Hash[value] = location{x: row, y: j}
newBoard.Values[row][j] = shared.BoardValue{Value: value, Visited: false}
newBoard.Hash[value] = shared.Location{X: row, Y: j}
}
row++
@ -76,8 +77,8 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
boardNum++
i++
*boards = append(*boards, newBoard)
newBoard = Board{
Hash: make(map[int]location, 25),
newBoard = shared.Board{
Hash: make(map[int]shared.Location, 25),
}
row = 0
}
@ -90,13 +91,13 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
return
}
func callNumber(n int, boards *[]Board) (cards []int) {
func callNumber(n int, boards *[]shared.Board) (cards []int) {
for i := 0; i < len(*boards); i++ {
location, present := (*boards)[i].Hash[n]
if present {
//Change the value to visited
(*boards)[i].Values[location.x][location.y].Visited = true
if checkBingo(&(*boards)[i], location.x, location.y) {
(*boards)[i].Values[location.X][location.Y].Visited = true
if checkBingo(&(*boards)[i], location.X, location.Y) {
cards = append(cards, i)
}
}
@ -104,7 +105,7 @@ func callNumber(n int, boards *[]Board) (cards []int) {
return
}
func checkBingo(board *Board, x int, y int) bool {
func checkBingo(board *shared.Board, x int, y int) bool {
checkVertical := true
//check if bingo for a given row and column
@ -128,7 +129,7 @@ func checkBingo(board *Board, x int, y int) bool {
return (checkVertical || checkHorizontal)
}
func returnAnswer(board *Board, n int) (answer int) {
func returnAnswer(board *shared.Board, n int) (answer int) {
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
if !(*board).Values[i][j].Visited {