New Shared Library

This commit is contained in:
Luke Else 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 package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -16,7 +17,7 @@ func main() {
fmt.Println(run(boards, nums)) fmt.Println(run(boards, nums))
} }
func run(boards *[]Board, nums []int) int { func run(boards *[]shared.Board, nums []int) int {
var bingo int var bingo int
var n int var n int
for i := 0; i < len(nums); i++ { for i := 0; i < len(nums); i++ {
@ -32,10 +33,10 @@ func run(boards *[]Board, nums []int) int {
return returnAnswer(&(*boards)[bingo], n) return returnAnswer(&(*boards)[bingo], n)
} }
func loadBoards(content *[]string) (boards *[]Board, nums []int) { func loadBoards(content *[]string) (boards *[]shared.Board, nums []int) {
boards = new([]Board) boards = new([]shared.Board)
newBoard := Board{ newBoard := shared.Board{
Hash: make(map[int]location, 25), Hash: make(map[int]shared.Location, 25),
} }
boardNum := 0 boardNum := 0
row := 0 row := 0
@ -61,8 +62,8 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
for j := 0; j < len(values); j++ { for j := 0; j < len(values); j++ {
value, _ := strconv.Atoi(values[j]) value, _ := strconv.Atoi(values[j])
newBoard.Values[row][j] = boardValue{Value: value, Visited: false} newBoard.Values[row][j] = shared.BoardValue{Value: value, Visited: false}
newBoard.Hash[value] = location{x: row, y: j} newBoard.Hash[value] = shared.Location{X: row, Y: j}
} }
row++ row++
@ -70,8 +71,8 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
boardNum++ boardNum++
i++ i++
*boards = append(*boards, newBoard) *boards = append(*boards, newBoard)
newBoard = Board{ newBoard = shared.Board{
Hash: make(map[int]location, 25), Hash: make(map[int]shared.Location, 25),
} }
row = 0 row = 0
} }
@ -84,13 +85,13 @@ func loadBoards(content *[]string) (boards *[]Board, nums []int) {
return return
} }
func callNumber(n int, boards *[]Board) int { func callNumber(n int, boards *[]shared.Board) int {
for i := 0; i < len(*boards); i++ { for i := 0; i < len(*boards); i++ {
location, present := (*boards)[i].Hash[n] location, present := (*boards)[i].Hash[n]
if present { if present {
//Change the value to visited //Change the value to visited
(*boards)[i].Values[location.x][location.y].Visited = true (*boards)[i].Values[location.X][location.Y].Visited = true
if checkBingo(&(*boards)[i], location.x, location.y) { if checkBingo(&(*boards)[i], location.X, location.Y) {
return i return i
} }
} }
@ -98,7 +99,7 @@ func callNumber(n int, boards *[]Board) int {
return -1 return -1
} }
func checkBingo(board *Board, x int, y int) bool { func checkBingo(board *shared.Board, x int, y int) bool {
checkVertical := true checkVertical := true
//check if bingo for a given row and column //check if bingo for a given row and column
@ -122,7 +123,7 @@ func checkBingo(board *Board, x int, y int) bool {
return checkVertical || checkHorizontal 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 i := 0; i < 5; i++ {
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
if !(*board).Values[i][j].Visited { if !(*board).Values[i][j].Visited {

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

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"math" "math"
@ -13,7 +14,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
list := mergeSort((*content), 0, len(*content)-1) list := shared.MergeSort((*content), 0, len(*content)-1)
position := list[len(list)/2] position := list[len(list)/2]
var cost float64 var cost float64
@ -48,43 +49,3 @@ func returnContent(path string) *[]int {
return &content return &content
} }
//sorting algorithm
func mergeSort(nums []int, start int, end int) []int {
if start == end {
return []int{nums[start]}
}
var mid int = ((end - start) / 2) + start
//Assign values back into Left and right
left := mergeSort(nums, start, mid)
right := mergeSort(nums, mid+1, end)
var combined []int
//Pointers for new array
leftPointer, rightPointer := 0, 0
for leftPointer <= len(left)-1 || rightPointer <= len(right)-1 {
if leftPointer == len(left) {
addValue(&combined, right[rightPointer], &rightPointer)
} else if rightPointer == len(right) {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
if left[leftPointer] <= right[rightPointer] {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
addValue(&combined, right[rightPointer], &rightPointer)
}
}
}
return combined
}
func addValue(nums *[]int, value int, pointer *int) {
*nums = append(*nums, value)
*pointer++
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"math" "math"
@ -13,7 +14,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
crabs := mergeSort(*content, 0, len(*content)-1) crabs := shared.MergeSort(*content, 0, len(*content)-1)
min, max := crabs[0], crabs[len(crabs)-1] min, max := crabs[0], crabs[len(crabs)-1]
@ -76,43 +77,3 @@ func returnContent(path string) *[]int {
return &content return &content
} }
//sorting algorithm
func mergeSort(nums []int, start int, end int) []int {
if start == end {
return []int{nums[start]}
}
var mid int = ((end - start) / 2) + start
//Assign values back into Left and right
left := mergeSort(nums, start, mid)
right := mergeSort(nums, mid+1, end)
var combined []int
//Pointers for new array
leftPointer, rightPointer := 0, 0
for leftPointer <= len(left)-1 || rightPointer <= len(right)-1 {
if leftPointer == len(left) {
addValue(&combined, right[rightPointer], &rightPointer)
} else if rightPointer == len(right) {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
if left[leftPointer] <= right[rightPointer] {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
addValue(&combined, right[rightPointer], &rightPointer)
}
}
}
return combined
}
func addValue(nums *[]int, value int, pointer *int) {
*nums = append(*nums, value)
*pointer++
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -10,34 +11,34 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
var bracketMap map[byte]byte = make(map[byte]byte) var bracketMap map[string]string = make(map[string]string)
var bracketCost map[byte]int = make(map[byte]int) var bracketCost map[string]int = make(map[string]int)
answer := 0 answer := 0
bracketMap[')'] = '(' bracketMap[")"] = "("
bracketMap[']'] = '[' bracketMap["]"] = "["
bracketMap['}'] = '{' bracketMap["}"] = "{"
bracketMap['>'] = '<' bracketMap[">"] = "<"
bracketCost[')'] = 3 bracketCost[")"] = 3
bracketCost[']'] = 57 bracketCost["]"] = 57
bracketCost['}'] = 1197 bracketCost["}"] = 1197
bracketCost['>'] = 25137 bracketCost[">"] = 25137
for _, row := range *content { for _, row := range *content {
var stack Stack = Stack{} var stack shared.Stack = shared.Stack{}
for _, char := range row { for _, char := range row {
brack, found := bracketMap[char] brack, found := bracketMap[string(char)]
if !found { if !found {
//If it is an opening bracket //If it is an opening bracket
stack.Push(char) stack.Push(string(char))
} else { } else {
if brack == stack.Peek().char { if brack == stack.Peek().Value {
stack.Pop() stack.Pop()
} else { } else {
answer += bracketCost[char] answer += bracketCost[string(char)]
fmt.Println("Illegal,", char, "found", answer) fmt.Println("Illegal,", char, "found", answer)
break break
} }

View File

@ -1,39 +0,0 @@
package main
type Stack struct {
head *StackNode
}
func (s *Stack) Push(item byte) {
if s.head == nil {
s.head = &StackNode{
char: item,
next: nil,
}
} else {
new := StackNode{
char: item,
next: s.head,
}
s.head = &new
}
}
func (s *Stack) Pop() byte {
node := s.Peek()
s.head = node.next
return node.char
}
func (s *Stack) Peek() (node *StackNode) {
node = s.head
if node == nil {
node = &StackNode{}
}
return
}
type StackNode struct {
char byte
next *StackNode
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -10,43 +11,43 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
var bracketMap map[byte]byte = make(map[byte]byte) var bracketMap map[string]string = make(map[string]string)
var reverseBracketMap map[byte]byte = make(map[byte]byte) var reverseBracketMap map[string]string = make(map[string]string)
var bracketCost map[byte]int = make(map[byte]int) var bracketCost map[string]int = make(map[string]int)
bracketMap[')'] = '(' bracketMap[")"] = "("
bracketMap[']'] = '[' bracketMap["]"] = "["
bracketMap['}'] = '{' bracketMap["}"] = "{"
bracketMap['>'] = '<' bracketMap[">"] = "<"
reverseBracketMap['('] = ')' reverseBracketMap["("] = ")"
reverseBracketMap['['] = ']' reverseBracketMap["["] = "]"
reverseBracketMap['{'] = '}' reverseBracketMap["{"] = "}"
reverseBracketMap['<'] = '>' reverseBracketMap["<"] = ">"
bracketCost[')'] = 1 bracketCost[")"] = 1
bracketCost[']'] = 2 bracketCost["]"] = 2
bracketCost['}'] = 3 bracketCost["}"] = 3
bracketCost['>'] = 4 bracketCost[">"] = 4
//reduce list to required incomplete set //reduce list to required incomplete set
stackList := []Stack{} stackList := []shared.Stack{}
autoCompletes := []int{} autoCompletes := []int{}
for _, row := range *content { for _, row := range *content {
stack := Stack{} stack := shared.Stack{}
keep := true keep := true
for _, char := range row { for _, char := range row {
brack, found := bracketMap[char] brack, found := bracketMap[string(char)]
if !found { if !found {
//If it is an opening bracket //If it is an opening bracket
stack.Push(char) stack.Push(string(char))
} else { } else {
if brack == stack.Peek().char { if brack == stack.Peek().Value {
stack.Pop() stack.Pop()
} else { } else {
keep = false keep = false
@ -62,7 +63,7 @@ func main() {
} }
emptyStackNode := StackNode{} emptyStackNode := shared.Node{}
for _, stack := range stackList { for _, stack := range stackList {
autocomplete := 0 autocomplete := 0
@ -73,7 +74,7 @@ func main() {
autoCompletes = append(autoCompletes, autocomplete) autoCompletes = append(autoCompletes, autocomplete)
} }
autoCompletes = mergeSort(autoCompletes, 0, len(autoCompletes)-1) autoCompletes = shared.MergeSort(autoCompletes, 0, len(autoCompletes)-1)
mid := autoCompletes[len(autoCompletes)/2] mid := autoCompletes[len(autoCompletes)/2]
@ -81,16 +82,6 @@ func main() {
} }
// func removeElement(slice *[][]byte, i int) (new *[][]byte) {
// new = &[][]byte{}
// for j, row := range *slice {
// if i != j {
// *new = append(*new, row)
// }
// }
// return
// }
func returnContent(path string) *[][]byte { func returnContent(path string) *[][]byte {
//read file and return it as an array of integers //read file and return it as an array of integers

View File

@ -1,39 +0,0 @@
package main
type Stack struct {
head *StackNode
}
func (s *Stack) Push(item byte) {
if s.head == nil {
s.head = &StackNode{
char: item,
next: nil,
}
} else {
new := StackNode{
char: item,
next: s.head,
}
s.head = &new
}
}
func (s *Stack) Pop() byte {
node := s.Peek()
s.head = node.next
return node.char
}
func (s *Stack) Peek() (node *StackNode) {
node = s.head
if node == nil {
node = &StackNode{}
}
return
}
type StackNode struct {
char byte
next *StackNode
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -11,7 +12,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
octopuses := map[Coordinate]int{} octopuses := map[shared.Coordinate]int{}
answer := 0 answer := 0
@ -22,12 +23,12 @@ func main() {
for y, row := range *content { for y, row := range *content {
width = len(row) width = len(row)
for x, val := range row { for x, val := range row {
octopuses[Coordinate{X: x, Y: y}] = val octopuses[shared.Coordinate{X: x, Y: y}] = val
} }
} }
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
flashers := map[Coordinate]bool{} flashers := map[shared.Coordinate]bool{}
for coord := range octopuses { for coord := range octopuses {
octopuses[coord]++ octopuses[coord]++
@ -63,49 +64,6 @@ func main() {
fmt.Println(answer) fmt.Println(answer)
} }
//Coordinate Class
type Coordinate struct {
X int
Y int
}
func (c *Coordinate) Neighbours(gridWidth int, gridHeight int, diagonal bool) (out []Coordinate) {
spaceLeft := c.X > 0
spaceRight := c.X < gridWidth-1
spaceUp := c.Y > 0
spaceDown := c.Y < gridHeight-1
if spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y})
}
if spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y})
}
if spaceUp {
out = append(out, Coordinate{c.X, c.Y - 1})
}
if spaceDown {
out = append(out, Coordinate{c.X, c.Y + 1})
}
if diagonal {
if spaceUp && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y - 1})
}
if spaceUp && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y - 1})
}
if spaceDown && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y + 1})
}
if spaceDown && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y + 1})
}
}
return
}
func returnContent(path string) *[][]int { func returnContent(path string) *[][]int {
//read file and return it as an array of integers //read file and return it as an array of integers

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -11,7 +12,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
octopuses := map[Coordinate]int{} octopuses := map[shared.Coordinate]int{}
answer := 0 answer := 0
@ -22,14 +23,14 @@ func main() {
for y, row := range *content { for y, row := range *content {
width = len(row) width = len(row)
for x, val := range row { for x, val := range row {
octopuses[Coordinate{X: x, Y: y}] = val octopuses[shared.Coordinate{X: x, Y: y}] = val
} }
} }
i := 0 i := 0
for { for {
i++ i++
flashers := map[Coordinate]bool{} flashers := map[shared.Coordinate]bool{}
for coords, energy := range octopuses { for coords, energy := range octopuses {
octopuses[coords] = energy + 1 octopuses[coords] = energy + 1
} }
@ -61,7 +62,7 @@ func main() {
allFlashed := true allFlashed := true
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
if !flashers[Coordinate{ if !flashers[shared.Coordinate{
X: x, X: x,
Y: y, Y: y,
}] { }] {
@ -79,49 +80,6 @@ func main() {
fmt.Println(answer) fmt.Println(answer)
} }
//Coordinate Class
type Coordinate struct {
X int
Y int
}
func (c *Coordinate) Neighbours(gridWidth int, gridHeight int, diagonal bool) (out []Coordinate) {
spaceLeft := c.X > 0
spaceRight := c.X < gridWidth-1
spaceUp := c.Y > 0
spaceDown := c.Y < gridHeight-1
if spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y})
}
if spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y})
}
if spaceUp {
out = append(out, Coordinate{c.X, c.Y - 1})
}
if spaceDown {
out = append(out, Coordinate{c.X, c.Y + 1})
}
if diagonal {
if spaceUp && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y - 1})
}
if spaceUp && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y - 1})
}
if spaceDown && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y + 1})
}
if spaceDown && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y + 1})
}
}
return
}
func returnContent(path string) *[][]int { func returnContent(path string) *[][]int {
//read file and return it as an array of integers //read file and return it as an array of integers

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -12,7 +13,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
sheet := make(map[Coordinate]bool) sheet := make(map[shared.Coordinate]bool)
answer := 0 answer := 0
for _, line := range *content { for _, line := range *content {
@ -41,7 +42,7 @@ func main() {
x, _ := strconv.Atoi(coordinates[0]) x, _ := strconv.Atoi(coordinates[0])
y, _ := strconv.Atoi(coordinates[1]) y, _ := strconv.Atoi(coordinates[1])
sheet[Coordinate{X: x, Y: y}] = true sheet[shared.Coordinate{X: x, Y: y}] = true
} }
} }
@ -54,8 +55,8 @@ func main() {
} }
func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { func FoldX(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
folded = make(map[Coordinate]bool) folded = make(map[shared.Coordinate]bool)
for mark := range sheet { for mark := range sheet {
x := mark.X x := mark.X
if x > foldPoint { if x > foldPoint {
@ -63,13 +64,13 @@ func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool
x = 2*foldPoint - x x = 2*foldPoint - x
} }
folded[Coordinate{X: x, Y: mark.Y}] = true folded[shared.Coordinate{X: x, Y: mark.Y}] = true
} }
return return
} }
func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { func FoldY(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
folded = make(map[Coordinate]bool) folded = make(map[shared.Coordinate]bool)
for mark := range sheet { for mark := range sheet {
y := mark.Y y := mark.Y
if y > foldPoint { if y > foldPoint {
@ -77,16 +78,11 @@ func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool
y = 2*foldPoint - y y = 2*foldPoint - y
} }
folded[Coordinate{X: mark.X, Y: y}] = true folded[shared.Coordinate{X: mark.X, Y: y}] = true
} }
return return
} }
type Coordinate struct {
X int
Y int
}
func returnContent(path string) *[]string { func returnContent(path string) *[]string {
//read file and return it as an array of integers //read file and return it as an array of integers

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
@ -12,7 +13,7 @@ func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("../testInput") //content := returnContent("../testInput")
sheet := make(map[Coordinate]bool) sheet := make(map[shared.Coordinate]bool)
//var answer string //var answer string
for _, line := range *content { for _, line := range *content {
@ -37,14 +38,14 @@ func main() {
x, _ := strconv.Atoi(coordinates[0]) x, _ := strconv.Atoi(coordinates[0])
y, _ := strconv.Atoi(coordinates[1]) y, _ := strconv.Atoi(coordinates[1])
sheet[Coordinate{X: x, Y: y}] = true sheet[shared.Coordinate{X: x, Y: y}] = true
} }
} }
for y := 0; y < 8; y++ { for y := 0; y < 8; y++ {
for x := 0; x < 200; x++ { for x := 0; x < 200; x++ {
if sheet[Coordinate{X: x, Y: y}] { if sheet[shared.Coordinate{X: x, Y: y}] {
fmt.Print("#") fmt.Print("#")
} else { } else {
fmt.Print(" ") fmt.Print(" ")
@ -55,8 +56,8 @@ func main() {
} }
func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { func FoldX(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
folded = make(map[Coordinate]bool) folded = make(map[shared.Coordinate]bool)
for mark := range sheet { for mark := range sheet {
x := mark.X x := mark.X
if x > foldPoint { if x > foldPoint {
@ -64,13 +65,13 @@ func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool
x = 2*foldPoint - x x = 2*foldPoint - x
} }
folded[Coordinate{X: x, Y: mark.Y}] = true folded[shared.Coordinate{X: x, Y: mark.Y}] = true
} }
return return
} }
func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { func FoldY(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
folded = make(map[Coordinate]bool) folded = make(map[shared.Coordinate]bool)
for mark := range sheet { for mark := range sheet {
y := mark.Y y := mark.Y
if y > foldPoint { if y > foldPoint {
@ -78,16 +79,11 @@ func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool
y = 2*foldPoint - y y = 2*foldPoint - y
} }
folded[Coordinate{X: mark.X, Y: y}] = true folded[shared.Coordinate{X: mark.X, Y: y}] = true
} }
return return
} }
type Coordinate struct {
X int
Y int
}
func returnContent(path string) *[]string { func returnContent(path string) *[]string {
//read file and return it as an array of integers //read file and return it as an array of integers

107
day16/16a/main.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
)
func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
fmt.Println(content)
startingString := (*content)[0]
pairs := make(map[string]string)
count := make(map[string]int)
//Create map of pair values
for i := 2; i < len(*content); i++ {
split := strings.Split((*content)[i], " -> ")
pairs[split[0]] = split[1]
}
//Fill initial list
list := LinkedList{Head: &Node{Value: string(startingString[0]), Next: nil}}
current := list.Head
for i := 1; i < len(startingString); i++ {
node := &Node{Value: string(startingString[i]), Next: nil}
count[string(startingString[i])]++
current.Next = node
current = node
}
//Run iterations on list
iterations := 10
for i := 1; i <= iterations; i++ {
current = list.Head
for current.Next != nil {
value := pairs[current.Value+current.Next.Value]
list.InsertItem(current, current.Next, value)
count[value]++
current = current.Next.Next
}
list.PrintList()
}
//determine min and max
min := math.MaxInt
max := 0
for _, value := range count {
if value > max {
max = value
} else if value < min {
min = value
}
}
fmt.Println(max - min)
}
type LinkedList struct {
Head *Node
}
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) {
first.Next = &Node{Value: value, Next: second}
}
func (l *LinkedList) PrintList() {
list := []string{}
current := l.Head
for current != nil {
list = append(list, current.Value)
current = current.Next
}
fmt.Println(list)
}
type Node struct {
Value string
Next *Node
}
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
}

107
day16/16b/main.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
)
func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
fmt.Println(content)
startingString := (*content)[0]
pairs := make(map[string]string)
count := make(map[string]int)
//Create map of pair values
for i := 2; i < len(*content); i++ {
split := strings.Split((*content)[i], " -> ")
pairs[split[0]] = split[1]
}
//Fill initial list
list := LinkedList{Head: &Node{Value: string(startingString[0]), Next: nil}}
current := list.Head
for i := 1; i < len(startingString); i++ {
node := &Node{Value: string(startingString[i]), Next: nil}
count[string(startingString[i])]++
current.Next = node
current = node
}
//Run iterations on list
iterations := 40
for i := 1; i <= iterations; i++ {
current = list.Head
for current.Next != nil {
value := pairs[current.Value+current.Next.Value]
list.InsertItem(current, current.Next, value)
count[value]++
current = current.Next.Next
}
fmt.Println(i)
}
//determine min and max
min := math.MaxInt
max := 0
for _, value := range count {
if value > max {
max = value
} else if value < min {
min = value
}
}
fmt.Println(max - min)
}
type LinkedList struct {
Head *Node
}
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) {
first.Next = &Node{Value: value, Next: second}
}
func (l *LinkedList) PrintList() {
list := []string{}
current := l.Head
for current != nil {
list = append(list, current.Value)
current = current.Next
}
fmt.Println(list)
}
type Node struct {
Value string
Next *Node
}
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
}

102
day16/input Normal file
View File

@ -0,0 +1,102 @@
SHHBNFBCKNHCNOSHHVFF
CK -> N
VP -> B
CF -> S
FO -> V
VC -> S
BV -> V
NP -> P
SN -> C
KN -> V
NF -> P
SB -> C
PC -> B
OB -> V
NS -> O
FH -> S
NK -> S
HO -> V
NV -> O
FV -> O
FB -> S
PS -> S
FN -> K
HS -> O
CB -> K
HV -> P
NH -> C
BO -> B
FF -> N
PO -> F
BB -> N
PN -> C
BP -> C
HN -> K
CO -> P
BF -> H
BC -> S
CV -> B
VV -> F
FS -> B
BN -> P
VK -> S
PV -> V
PP -> B
PH -> N
SS -> O
SK -> S
NC -> P
ON -> F
NB -> N
CC -> N
SF -> H
PF -> H
OV -> O
KH -> C
CP -> V
PK -> O
KC -> K
KK -> C
KF -> B
HP -> C
FK -> H
BH -> K
VN -> H
OO -> S
SC -> K
SP -> B
KO -> V
KV -> F
HK -> N
FP -> N
NN -> B
VS -> O
HC -> K
BK -> N
KS -> K
VB -> O
OH -> F
KB -> F
KP -> H
HB -> N
NO -> N
OF -> O
BS -> H
VO -> H
SH -> O
SV -> K
HF -> C
CS -> F
FC -> N
VH -> H
OP -> K
OK -> H
PB -> K
HH -> S
OC -> V
VF -> B
CH -> K
CN -> C
SO -> P
OS -> O

18
day16/testInput Normal file
View File

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

16
shared/Board.go Normal file
View File

@ -0,0 +1,16 @@
package shared
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
}

43
shared/Coordinate.go Normal file
View File

@ -0,0 +1,43 @@
package shared
type Coordinate struct {
X int
Y int
}
func (c *Coordinate) Neighbours(gridWidth int, gridHeight int, diagonal bool) (out []Coordinate) {
spaceLeft := c.X > 0
spaceRight := c.X < gridWidth-1
spaceUp := c.Y > 0
spaceDown := c.Y < gridHeight-1
if spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y})
}
if spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y})
}
if spaceUp {
out = append(out, Coordinate{c.X, c.Y - 1})
}
if spaceDown {
out = append(out, Coordinate{c.X, c.Y + 1})
}
if diagonal {
if spaceUp && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y - 1})
}
if spaceUp && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y - 1})
}
if spaceDown && spaceLeft {
out = append(out, Coordinate{c.X - 1, c.Y + 1})
}
if spaceDown && spaceRight {
out = append(out, Coordinate{c.X + 1, c.Y + 1})
}
}
return
}

21
shared/LinkedList.go Normal file
View File

@ -0,0 +1,21 @@
package shared
import "fmt"
type LinkedList struct {
Head *Node
}
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) {
first.Next = &Node{Value: value, Next: second}
}
func (l *LinkedList) PrintList() {
list := []string{}
current := l.Head
for current != nil {
list = append(list, current.Value)
current = current.Next
}
fmt.Println(list)
}

6
shared/Node.go Normal file
View File

@ -0,0 +1,6 @@
package shared
type Node struct {
Value string
Next *Node
}

View File

@ -1,6 +1,6 @@
package main package shared
func mergeSort(nums []int, start int, end int) []int { func MergeSort(nums []int, start int, end int) []int {
if start == end { if start == end {
return []int{nums[start]} return []int{nums[start]}
} }
@ -8,8 +8,8 @@ func mergeSort(nums []int, start int, end int) []int {
var mid int = ((end - start) / 2) + start var mid int = ((end - start) / 2) + start
//Assign values back into Left and right //Assign values back into Left and right
left := mergeSort(nums, start, mid) left := MergeSort(nums, start, mid)
right := mergeSort(nums, mid+1, end) right := MergeSort(nums, mid+1, end)
var combined []int var combined []int

33
shared/Stack.go Normal file
View File

@ -0,0 +1,33 @@
package shared
type Stack struct {
head *Node
}
func (s *Stack) Push(item string) {
if s.head == nil {
s.head = &Node{
Value: item,
Next: nil,
}
} else {
s.head = &Node{
Value: item,
Next: s.head,
}
}
}
func (s *Stack) Pop() string {
node := s.Peek()
s.head = node.Next
return node.Value
}
func (s *Stack) Peek() (node *Node) {
node = s.head
if node == nil {
node = &Node{}
}
return
}