Preparations for 2023

master
Jonas Letzbor 2023-12-01 13:12:28 +01:00
parent 9e0d6fc062
commit 4130343ad6
Signed by: RPJosh
GPG Key ID: 46D72F589702E55A
54 changed files with 682 additions and 31 deletions

View File

@ -5,36 +5,36 @@ import (
"os" "os"
"strconv" "strconv"
"rpjosh.de/adventOfCode2022/internal/day_01" "rpjosh.de/adventOfCode/internal/2023/day_01"
"rpjosh.de/adventOfCode2022/internal/day_02" "rpjosh.de/adventOfCode/internal/2023/day_02"
"rpjosh.de/adventOfCode2022/internal/day_03" "rpjosh.de/adventOfCode/internal/2023/day_03"
"rpjosh.de/adventOfCode2022/internal/day_04" "rpjosh.de/adventOfCode/internal/2023/day_04"
"rpjosh.de/adventOfCode2022/internal/day_05" "rpjosh.de/adventOfCode/internal/2023/day_05"
"rpjosh.de/adventOfCode2022/internal/day_06" "rpjosh.de/adventOfCode/internal/2023/day_06"
"rpjosh.de/adventOfCode2022/internal/day_07" "rpjosh.de/adventOfCode/internal/2023/day_07"
"rpjosh.de/adventOfCode2022/internal/day_08" "rpjosh.de/adventOfCode/internal/2023/day_08"
"rpjosh.de/adventOfCode2022/internal/day_09" "rpjosh.de/adventOfCode/internal/2023/day_09"
"rpjosh.de/adventOfCode2022/internal/day_10" "rpjosh.de/adventOfCode/internal/2023/day_10"
"rpjosh.de/adventOfCode2022/internal/day_11" "rpjosh.de/adventOfCode/internal/2023/day_11"
"rpjosh.de/adventOfCode2022/internal/day_12" "rpjosh.de/adventOfCode/internal/2023/day_12"
"rpjosh.de/adventOfCode2022/internal/day_13" "rpjosh.de/adventOfCode/internal/2023/day_13"
"rpjosh.de/adventOfCode2022/internal/day_14" "rpjosh.de/adventOfCode/internal/2023/day_14"
"rpjosh.de/adventOfCode2022/internal/day_15" "rpjosh.de/adventOfCode/internal/2023/day_15"
"rpjosh.de/adventOfCode2022/internal/day_16" "rpjosh.de/adventOfCode/internal/2023/day_16"
"rpjosh.de/adventOfCode2022/internal/day_17" "rpjosh.de/adventOfCode/internal/2023/day_17"
"rpjosh.de/adventOfCode2022/internal/day_18" "rpjosh.de/adventOfCode/internal/2023/day_18"
"rpjosh.de/adventOfCode2022/internal/day_19" "rpjosh.de/adventOfCode/internal/2023/day_19"
"rpjosh.de/adventOfCode2022/internal/day_20" "rpjosh.de/adventOfCode/internal/2023/day_20"
"rpjosh.de/adventOfCode2022/internal/day_21" "rpjosh.de/adventOfCode/internal/2023/day_21"
"rpjosh.de/adventOfCode2022/internal/day_22" "rpjosh.de/adventOfCode/internal/2023/day_22"
"rpjosh.de/adventOfCode2022/internal/day_23" "rpjosh.de/adventOfCode/internal/2023/day_23"
"rpjosh.de/adventOfCode2022/internal/day_24" "rpjosh.de/adventOfCode/internal/2023/day_24"
"rpjosh.de/adventOfCode2022/internal/day_25" "rpjosh.de/adventOfCode/internal/2023/day_25"
"rpjosh.de/adventOfCode2022/pkg/utils" "rpjosh.de/adventOfCode/pkg/utils"
) )
func main() { func main() {
year := 2022 year := 2023
day := 1 day := 1
if len(os.Args) >= 2 && os.Args[1] != "" { if len(os.Args) >= 2 && os.Args[1] != "" {
var err error var err error

2
go.mod
View File

@ -1,3 +1,3 @@
module rpjosh.de/adventOfCode2022 module rpjosh.de/adventOfCode
go 1.18 go 1.18

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"rpjosh.de/adventOfCode2022/pkg/utils" "rpjosh.de/adventOfCode/pkg/utils"
) )
type Day struct{} type Day struct{}

View File

@ -0,0 +1,154 @@
package day_11
import (
"fmt"
"strings"
"rpjosh.de/adventOfCode/pkg/utils"
)
type Day struct{}
type MonkeyData struct {
operation MonkeyOperation
test MonkeyTest
items []int
inspectedItems int
}
type MonkeyOperation struct {
multiply bool
sameValue bool
value int
}
type MonkeyTest struct {
divValue int
trueThrowTo int
falseThrowTo int
}
func (d *Day) Part1(in string) string {
return d.Perform(in, false, 20)
}
func (d *Day) Part2(in string) string {
return d.Perform(in, true, 10000)
}
func (d *Day) Perform(in string, part2 bool, iterations int) string {
var monkeys []MonkeyData
part2DivValues := 1
for i, val := range strings.Split(in, "\n") {
mongo := i / 7
// Parse monkey data
switch i % 7 {
case 0:
{
monkeys = append(monkeys, MonkeyData{})
}
case 1:
{
// Starting items
items := strings.Split(val[18:], ", ")
//monkeys[mongo].items = make([]int, 10)
for _, item := range items {
monkeys[mongo].items = append(monkeys[mongo].items, utils.ToInt(item))
}
}
case 2:
{
// Operation >19<old
op := val[23:]
monkeys[mongo].operation.multiply = op[0:1] == "*"
monkeys[mongo].operation.sameValue = op[2:] == "old"
if !monkeys[mongo].operation.sameValue {
monkeys[mongo].operation.value = utils.ToInt(op[2:])
}
}
case 3:
{
// Test
monkeys[mongo].test.divValue = utils.ToInt(val[21:])
// That the values are getting not to big,
part2DivValues *= utils.ToInt(val[21:])
}
case 4:
{
// test true
monkeys[mongo].test.trueThrowTo = utils.ToInt(val[29:])
}
case 5:
{
//test false
monkeys[mongo].test.falseThrowTo = utils.ToInt(val[30:])
}
}
}
for i := 0; i < iterations; i++ {
for mongo := range monkeys {
monk := &monkeys[mongo]
// Inspect items
for item := range monk.items {
// Execute operation
operationValue := monk.operation.value
if monk.operation.sameValue {
operationValue = monk.items[item]
}
if monk.operation.multiply {
monk.items[item] = monk.items[item] * operationValue
} else {
monk.items[item] = monk.items[item] + operationValue
}
// Monkey gets bored
if !part2 {
monk.items[item] /= 3
} else {
monk.items[item] = monk.items[item] % part2DivValues
//fmt.Printf("Number: %d\n", monk.items[item])
}
// Is it diviable?
var throwTo int
if monk.items[item]%monk.test.divValue == 0 {
throwTo = monk.test.trueThrowTo
} else {
throwTo = monk.test.falseThrowTo
}
monkeys[throwTo].items = append(monkeys[throwTo].items, monk.items[item])
monk.inspectedItems++
}
// clear items
monk.items = nil
}
//fmt.Printf("\nRound %d\n", i)
//for i, monkey := range monkeys {
// fmt.Printf("Monkey %d: %s\n", i, monkey.items)
//}
}
maxOne := 0
maxTwo := 0
for _, monkey := range monkeys {
if monkey.inspectedItems > maxOne {
maxTwo = maxOne
maxOne = monkey.inspectedItems
} else if monkey.inspectedItems > maxTwo {
maxTwo = monkey.inspectedItems
}
}
return fmt.Sprintf("%d", maxOne*maxTwo)
}

View File

@ -0,0 +1,244 @@
package day_12
import (
"fmt"
"strings"
"time"
)
type Day struct {
heightMap [][]rune
visited [][]bool
currentPosition Position
destinationPosition Position
}
type Position struct {
row int
column int
}
const MaxInt = int(^uint(0) >> 1)
func (d *Day) Part1(in string) string {
// Parse map
for i, val := range strings.Split(in, "\n") {
d.heightMap = append(d.heightMap, make([]rune, len(val)))
d.visited = append(d.visited, make([]bool, len(val)+2))
for c := 0; c < len(val); c++ {
d.heightMap[i][c] = rune(val[c])
d.visited[i][c] = false
strVal := val[c : c+1]
if strVal == "S" {
d.currentPosition = Position{i, c}
min := "a"
d.heightMap[i][c] = rune(min[0])
d.visited[i][c] = true
} else if strVal == "E" {
d.destinationPosition = Position{i, c}
min := "z"
d.heightMap[i][c] = rune(min[0])
}
}
}
d.visited = append(d.visited, make([]bool, 220))
//fmt.Printf("\n%s\n", d.heightMap[0])
i, _ := d.makeOneStep(d.visited, d.currentPosition, 0)
return fmt.Sprintf("%d", i)
}
// Brute force the shortest way :)
// Positions already visited are ignored
func (d *Day) makeOneStep(visited [][]bool, pos Position, stepsMade int) (int, [][]bool) {
currentVal := d.heightMap[pos.row][pos.column]
visited[pos.row][pos.column] = true
// We are arrived
if pos.column == d.destinationPosition.column && pos.row == d.destinationPosition.row {
fmt.Printf("We are here: %d\n", stepsMade)
time.Sleep(50 * time.Millisecond)
return stepsMade, visited
}
minSteps := MaxInt
var minVisited [][]bool = clone(&visited)
if stepsMade == 0 {
//fmt.Printf("\nPossible to right: %t", isStepPossible(d.heightMap[pos.row][pos.column], d.heightMap[pos.row][pos.column+1]))
//fmt.Printf("%d %d\n", d.heightMap[pos.row][pos.column], d.heightMap[pos.row][pos.column+1])
//fmt.Printf("Posistion: %s\n", pos)
//fmt.Printf("%s", visited2[1])
}
//visitedReal := clone(&visited)
//if pos.row >= 18 && pos.row <= 24 && pos.column <= 184 && pos.column >= 12 {
fmt.Printf("Position: %s (%d)\n", pos, stepsMade)
//fmt.Printf("End: %s\n", d.destinationPosition)
//fmt.Printf("Visited: %s", visited[pos.row])
//time.Sleep(50 * time.Millisecond)
//}
//fmt.Printf("Visided: %s\n", visited)
// Make a step to each direction an return the last one
// Because we could get trapped
if d.isNotVisitedAndPossible(pos.column+1, pos.row, visited, currentVal) {
//fmt.Printf("Going to the right :) \n")
steps, vis := d.makeOneStep(visited, Position{column: pos.column + 1, row: pos.row}, stepsMade+1)
if steps <= minSteps {
minVisited = vis
minSteps = steps
//visited = clone(&visitedReal)
//visited[pos.row][pos.column+1] = true
//minVisited = vis
//minVisited = realVisited
} // else {
visited[pos.row][pos.column+1] = true
minVisited[pos.row][pos.column+1] = true
//visited = clone(&visitedReal)
//visited = vis
//visited[pos.row][pos.column] = false
//visited[pos.row][pos.column+1] = true
//visited = realVisited
//}
}
if d.isNotVisitedAndPossible(pos.column-1, pos.row, visited, currentVal) {
//fmt.Printf("Going to the left :) \n")
steps, vis := d.makeOneStep(visited, Position{column: pos.column - 1, row: pos.row}, stepsMade+1)
if steps <= minSteps {
minVisited = vis
minSteps = steps
// Remove visited flag of previous
//visited[pos.row][pos.column-1] = true
//visited[pos.row][pos.column+1] = false
//visited = clone(&visitedReal)
//visited = vis
//minVisited = vis
//minVisited = realVisited
} // else {
visited[pos.row][pos.column-1] = true
minVisited[pos.row][pos.column-1] = true
//visited = clone(&visitedReal)
//visited = vis
//visited[pos.row][pos.column] = false
//visited[pos.row][pos.column-1] = false
//visited = realVisited
//}
}
if d.isNotVisitedAndPossible(pos.column, pos.row+1, visited, currentVal) {
//fmt.Printf("Going to the top :) \n")
steps, vis := d.makeOneStep(visited, Position{column: pos.column, row: pos.row + 1}, stepsMade+1)
if steps <= minSteps {
minVisited = vis
minSteps = steps
//visited = clone(&visitedReal)
//visited[pos.row+1][pos.column] = true
//visited[pos.row][pos.column+1] = false
//if pos.column != 0 {
// visited[pos.row+1][pos.column] = false
//}
//minVisited = vis
//minVisited = realVisited
} // else {
visited[pos.row+1][pos.column] = true
minVisited[pos.row+1][pos.column] = true
//visited = clone(&visitedReal)
//visited[pos.row][pos.column] = false
//visited = vis
//visited = realVisited
//}
}
if d.isNotVisitedAndPossible(pos.column, pos.row-1, visited, currentVal) {
//fmt.Printf("Going to the bottom :) \n")
steps, vis := d.makeOneStep(visited, Position{column: pos.column, row: pos.row - 1}, stepsMade+1)
if steps <= minSteps {
minVisited = vis
minSteps = steps
//vis[pos.row-1][pos.column] = false
//visited[pos.row][pos.column+1] = false
//visited[pos.row+1][pos.column] = false
//visited = clone(&visitedReal)
//visited = vis
//minVisited = vis
//minVisited = realVisited
} // else {
visited[pos.row-1][pos.column] = true
minVisited[pos.row-1][pos.column] = true
//visited = clone(&visitedReal)
//visited[pos.row][pos.column] = false
//visited = vis
//}
}
//fmt.Printf("Steps made: %d\n", minSteps)
// No step was was possible
if minSteps == MaxInt {
return minSteps, visited
}
//minVisited[pos.row-1][pos.column] = true
//minVisited[pos.row+1][pos.column] = true
//minVisited[pos.row][pos.column+1] = true
//minVisited[pos.row][pos.column-1] = true
return minSteps, minVisited
}
func (d *Day) isNotVisitedAndPossible(column int, row int, visited [][]bool, currentPos rune) bool {
return d.isNotVisited(row, column, visited) && isStepPossible(currentPos, d.heightMap[row][column])
}
func (d *Day) isNotVisited(row int, column int, visited [][]bool) bool {
if row < 0 || column < 0 || row >= len(d.heightMap) || column >= len(d.heightMap[row]) {
// We are on standing on the edge
return false
}
// Check if all four directions are already visited
return !visited[row][column] ||
(row+1 != len(visited) && !visited[row+1][column] && isStepPossible(d.heightMap[row][column], d.heightMap[row+1][column])) ||
(column+1 != len(visited[row]) && !visited[row][column+1] && isStepPossible(d.heightMap[row][column], d.heightMap[row][column+1])) ||
(row != 0 && !visited[row-1][column] && isStepPossible(d.heightMap[row][column], d.heightMap[row-1][column])) ||
(column != 0 && !visited[row][column-1] && isStepPossible(d.heightMap[row][column], d.heightMap[row][column-1]))
}
// Can we master the height
func isStepPossible(current rune, next rune) bool {
return next-1 <= current
}
func (d *Day) Part2(in string) string {
return ""
}
func clone(visited *[][]bool) [][]bool {
var s = make([][]bool, len(*visited))
for i := range s {
s[i] = make([]bool, len((*visited)[i]))
copy(s[i], (*visited)[i])
}
//copy(s, *visited)
return s
}

View File

@ -0,0 +1,11 @@
package day_01
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_02
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_03
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_04
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_05
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_06
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_07
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_08
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_09
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_10
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_13
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_14
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_15
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_16
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_17
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_18
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_19
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_20
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_21
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_22
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_23
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_24
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -0,0 +1,11 @@
package day_25
type Day struct{}
func (d *Day) Part1(in string) string {
return ""
}
func (d *Day) Part2(in string) string {
return ""
}

View File

@ -45,7 +45,7 @@ func GetInputData(year int, day int) string {
} }
// Make a request to get the input from advent of code and save it in a file // Make a request to get the input from advent of code and save it in a file
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", year, day) url := fmt.Sprintf("https://adventOfCode.com/%d/day/%d/input", year, day)
// Read cookie session vaue // Read cookie session vaue
session, err := os.ReadFile("./session.txt") session, err := os.ReadFile("./session.txt")

2
run.sh
View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
nodemon --quiet -e go,html,yaml --ignore web/app/ --signal SIGTERM --exec 'go run ./cmd/adventOfCode '$1' '$2' '$3' || exit 1' nodemon --quiet -e go,html,yaml --ignore web/app/ --signal SIGTERM --exec 'clear && go run ./cmd/adventOfCode/ '$1' '$2' '$3' || exit 1'