From 92a9400eefc059915de3f6ccc6d617669b4a6648 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sat, 4 Dec 2021 22:42:13 +0000 Subject: [PATCH] day 4b complete --- day4/day 4b/Board.go | 16 +++++ day4/day 4b/go.mod | 3 + day4/day 4b/main.go | 162 ++++++++++++++++++++++++++++++++++++++++++ day4/day 4b/testInput | 19 +++++ 4 files changed, 200 insertions(+) create mode 100644 day4/day 4b/Board.go create mode 100644 day4/day 4b/go.mod create mode 100644 day4/day 4b/main.go create mode 100644 day4/day 4b/testInput diff --git a/day4/day 4b/Board.go b/day4/day 4b/Board.go new file mode 100644 index 0000000..4210456 --- /dev/null +++ b/day4/day 4b/Board.go @@ -0,0 +1,16 @@ +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 +} diff --git a/day4/day 4b/go.mod b/day4/day 4b/go.mod new file mode 100644 index 0000000..e938e2e --- /dev/null +++ b/day4/day 4b/go.mod @@ -0,0 +1,3 @@ +module PWD + +go 1.17 diff --git a/day4/day 4b/main.go b/day4/day 4b/main.go new file mode 100644 index 0000000..83cce5e --- /dev/null +++ b/day4/day 4b/main.go @@ -0,0 +1,162 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "regexp" + "strconv" + "strings" +) + +func main() { + content := returnContent("../input") + //content := returnContent("testInput") + boards, nums := loadBoards(content) + fmt.Println(run(boards, nums)) +} + +func run(boards *[]Board, nums []int) int { + var completedBoards map[*Board]bool = make(map[*Board]bool) + var bingo []int + var n int + for i := 0; i < len(nums); i++ { + bingo = callNumber(nums[i], boards) + + for X := 0; X < len(bingo); X++ { + _, present := completedBoards[&(*boards)[bingo[X]]] + if !present { + n = nums[i] + fmt.Println("found value", bingo, n, returnAnswer(&(*boards)[bingo[X]], n)) + } + completedBoards[&(*boards)[bingo[X]]] = true + } + + } + + //return the answer + 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), + } + boardNum := 0 + row := 0 + + for i := 0; i < len(*content); i++ { + + if i == 0 { + //add nums + numlist := strings.Split((*content)[i], ",") + + for i := 0; i < len(numlist); i++ { + num, _ := strconv.Atoi(numlist[i]) + nums = append(nums, num) + } + + i++ + } else { + + if (*content)[i] != "" { + + regex := regexp.MustCompile("([0-9]+)") + values := regex.FindAllString((*content)[i], 5) + + 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} + } + row++ + + if row == 5 { + boardNum++ + i++ + *boards = append(*boards, newBoard) + newBoard = Board{ + Hash: make(map[int]location, 25), + } + row = 0 + } + + } + + } + + } + return +} + +func callNumber(n int, boards *[]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) { + cards = append(cards, i) + } + } + } + return +} + +func checkBingo(board *Board, x int, y int) bool { + checkVertical := true + //check if bingo for a given row and column + + //Check row + for i := 0; i < 5; i++ { + if !(*board).Values[x][i].Visited { + checkVertical = false + break + } + } + + checkHorizontal := true + //Check column + for i := 0; i < 5; i++ { + if !(*board).Values[i][y].Visited { + checkHorizontal = false + break + } + } + + return (checkVertical || checkHorizontal) +} + +func returnAnswer(board *Board, n int) (answer int) { + for i := 0; i < 5; i++ { + for j := 0; j < 5; j++ { + if !(*board).Values[i][j].Visited { + answer += (*board).Values[i][j].Value + } + } + } + + answer = answer * n + return +} + +func returnContent(path string) *[]string { + //read file and return it as an array of integers + + file, err := os.Open(path) + var content []string + + if err != nil { + fmt.Println("Unlucky, the file didn't open") + return &content + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + content = append(content, scanner.Text()) + } + + return &content +} diff --git a/day4/day 4b/testInput b/day4/day 4b/testInput new file mode 100644 index 0000000..49d17bc --- /dev/null +++ b/day4/day 4b/testInput @@ -0,0 +1,19 @@ +7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 + +22 13 17 11 0 + 8 2 23 4 24 +21 9 14 16 7 + 6 10 3 18 5 + 1 12 20 15 19 + + 3 15 0 2 22 + 9 18 13 17 5 +19 8 7 25 23 +20 11 10 24 4 +14 21 16 12 6 + +14 21 17 24 4 +10 16 15 9 19 +18 8 23 26 20 +22 11 13 6 5 + 2 0 12 3 7 \ No newline at end of file