Compare commits
53 Commits
bacc1855ab
...
day10
| Author | SHA1 | Date | |
|---|---|---|---|
| e0e4bbc958 | |||
| 5ca089afb4 | |||
| fb896a8d20 | |||
| 5bebd71c4e | |||
| 6d24500c69 | |||
| d17308873e | |||
| c6150871a3 | |||
| ec4a4243ca | |||
| 64de63ddbe | |||
| a17153662e | |||
| 852a805114 | |||
| 574fdf38a4 | |||
| a879d3bb88 | |||
| f48bae3bc3 | |||
| 745d95771c | |||
| c9df0e8fb8 | |||
| a692b4da4b | |||
| 62d8af2d1b | |||
| c159403427 | |||
| d900defb9b | |||
| 7ec430c8b5 | |||
| 255f39620e | |||
| 75ec54c0d2 | |||
| 32a84c2f3c | |||
| 2778e38f8b | |||
| 8a2afac023 | |||
| c16d3362fd | |||
| ad945bd41e | |||
| a2b9dfbcbe | |||
| 8c6d00683e | |||
| be12e11f5e | |||
| 4b51738c60 | |||
| 6946c978c6 | |||
| 3fc767f046 | |||
| 35dc086519 | |||
| 7686ffd788 | |||
| 96b4441bfc | |||
| 36b2f6feb3 | |||
| 8871be0bc9 | |||
| 76da133e30 | |||
| 7a8c20acc6 | |||
| c45c84ba8b | |||
| d11426e518 | |||
| 1c2e835ebc | |||
| a609bf72a9 | |||
| 8c6b9b3dfe | |||
| 9925c227a4 | |||
| e5c255d759 | |||
| 1ca62d7dbc | |||
| 8a91af0713 | |||
| 7a92f12c01 | |||
| 5ad191354c | |||
| d386fc7d10 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,3 +22,4 @@ go.work
|
|||||||
|
|
||||||
# Problem data
|
# Problem data
|
||||||
input*.txt
|
input*.txt
|
||||||
|
inputs/
|
||||||
|
|||||||
@@ -4,5 +4,13 @@ An attempt to start programming again.
|
|||||||
|
|
||||||
- [Day 1](./day01/trebuchet.go)
|
- [Day 1](./day01/trebuchet.go)
|
||||||
- [Day 2](./day02/lottacubes.go)
|
- [Day 2](./day02/lottacubes.go)
|
||||||
|
- [Day 3](./day03/engine-schema.go)
|
||||||
|
- [Day 4](./day04/scratchcards.go)
|
||||||
|
- [Day 5](./day05/seeds.go)
|
||||||
|
- [Day 6](./day06/race.go)
|
||||||
|
- [Day 7](./day07/cards.go)
|
||||||
|
- [Day 8](./day08/charpath.go)
|
||||||
|
- [Day 9](./day09/oasis.go)
|
||||||
|
|
||||||
|
|
||||||
This repo gets automatically mirrored to [Github](https://github.com/Doddophonique/aoc2023).
|
This repo gets automatically mirrored to [Github](https://github.com/Doddophonique/aoc2023).
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -16,13 +15,90 @@ func check(e error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintAndWait[T any](x T) {
|
func PrintAndWait(x ...any) {
|
||||||
fmt.Print(x)
|
fmt.Print(x)
|
||||||
fmt.Scanln()
|
fmt.Scanln()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MatchNumbers(lines []string, index int, ast []int, total *int) {
|
||||||
|
// Regex that finds the numbers in a row
|
||||||
|
renum := regexp.MustCompile("[0-9]+")
|
||||||
|
// Gather numbers in prev line
|
||||||
|
prevLine := renum.FindAllStringIndex(lines[index-1], -1)
|
||||||
|
// Gather numbers in this line
|
||||||
|
thisLine := renum.FindAllStringIndex(lines[index], -1)
|
||||||
|
// Gather numbers in next line
|
||||||
|
nextLine := renum.FindAllStringIndex(lines[index+1], -1)
|
||||||
|
// Calculate the number of numbers in three lines
|
||||||
|
totalNumbers := len(prevLine) + len(thisLine) + len(nextLine)
|
||||||
|
|
||||||
|
// Now we create a big array with all the indexes
|
||||||
|
allIndexes := prevLine
|
||||||
|
for i := range thisLine {
|
||||||
|
allIndexes = append(allIndexes, thisLine[i])
|
||||||
|
}
|
||||||
|
for i := range nextLine {
|
||||||
|
allIndexes = append(allIndexes, nextLine[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we create a big array with all the numbers
|
||||||
|
// We start from the previous line
|
||||||
|
allNumbers := renum.FindAllString(lines[index-1], -1)
|
||||||
|
thisNums := renum.FindAllString(lines[index], -1)
|
||||||
|
for i := range thisNums {
|
||||||
|
allNumbers = append(allNumbers, thisNums[i])
|
||||||
|
}
|
||||||
|
nextNums := renum.FindAllString(lines[index+1], -1)
|
||||||
|
for i := range nextNums {
|
||||||
|
allNumbers = append(allNumbers, nextNums[i])
|
||||||
|
}
|
||||||
|
// When we start, we have zero matches
|
||||||
|
matches := 0
|
||||||
|
// We will stop when we encounter two numbers
|
||||||
|
twoNums := [2]int{0, 0}
|
||||||
|
_ = twoNums
|
||||||
|
// Cycling through all numbers, but stopping at two matches
|
||||||
|
for i := 0; i < totalNumbers && matches < 2; i++ {
|
||||||
|
if (ast[0] >= allIndexes[i][0] - 1 && ast[0] <= allIndexes[i][1]) {
|
||||||
|
matches += 1
|
||||||
|
num, _ := strconv.Atoi(allNumbers[i])
|
||||||
|
twoNums[matches - 1] = num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(matches == 2) {
|
||||||
|
tempGears := twoNums[0] * twoNums[1]
|
||||||
|
*total += tempGears
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckGears(lines []string) {
|
||||||
|
total := 0
|
||||||
|
totalPoint := &total
|
||||||
|
// Regex that finds the numbers in a row
|
||||||
|
//renum := regexp.MustCompile("[0-9]+")
|
||||||
|
// Regex that finds the asterisks in a row
|
||||||
|
resym := regexp.MustCompile("[*]")
|
||||||
|
// For every line starting from the second
|
||||||
|
for i := 0; i < len(lines) - 1; i++ {
|
||||||
|
// Take the index of the asterisks
|
||||||
|
asteriskIndex := resym.FindAllStringIndex(lines[i], - 1)
|
||||||
|
// For every index we get
|
||||||
|
for j := range asteriskIndex {
|
||||||
|
MatchNumbers(lines, i, asteriskIndex[j], totalPoint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// firstLineNums := renum.FindAllStringIndex(lines[index], -1)
|
||||||
|
// firstLineSymbolsIndex := resym.FindAllStringIndex(lines[index], -1)
|
||||||
|
// secondLineSymbolsIndex := resym.FindAllStringIndex(lines[index + 1], -1)
|
||||||
|
// For every *number index range*, check if in the same line there is a
|
||||||
|
// symbol on (first - 1) or (last + 1), check in other lines if there is
|
||||||
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
|
// can break as you just need one symbol
|
||||||
|
fmt.Printf("Total of gears is: %d\n", total)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file, err := os.Open("./inputs/day03_test_input")
|
file, err := os.Open("./inputs/day03_input")
|
||||||
check(err)
|
check(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
@@ -32,17 +108,18 @@ func main() {
|
|||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
var lines []string
|
var lines []string
|
||||||
|
var totalSum int = 0
|
||||||
|
|
||||||
// Read the whole file into an array of strings
|
// Read the whole file into an array of strings
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
lines = append(lines, scanner.Text())
|
lines = append(lines, scanner.Text())
|
||||||
}
|
}
|
||||||
numLines := len(lines)
|
numLines := len(lines)
|
||||||
|
numbers := make([][]int, numLines)
|
||||||
|
|
||||||
// The 2D array of numbers will hold all the numbers to easily
|
// The 2D array of numbers will hold all the numbers to easily
|
||||||
// match them with corresponding strings in the file using the index
|
// match them with corresponding strings in the file using the index
|
||||||
numbers := make([][]int, numLines)
|
// For every line in the file, create an array of numbers
|
||||||
// For every line in the file, cerate an array of numbers
|
|
||||||
for i := 0; i < numLines; i++ {
|
for i := 0; i < numLines; i++ {
|
||||||
tempNums := renum.FindAllString(lines[i], -1)
|
tempNums := renum.FindAllString(lines[i], -1)
|
||||||
for j := 0; j < len(tempNums); j++ {
|
for j := 0; j < len(tempNums); j++ {
|
||||||
@@ -50,19 +127,94 @@ func main() {
|
|||||||
numbers[i] = append(numbers[i], num)
|
numbers[i] = append(numbers[i], num)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Line [0] needs to check only line 1 for symbols.
|
||||||
// We store the index of a symbol on the line it appears
|
Similarly, the last line needs to check only
|
||||||
symbolsIndex := make([][]int, numLines)
|
(last line index - 1) for symbols.
|
||||||
symbols := make([][]string, numLines)
|
So we first check the line [0], then start a loop
|
||||||
_ = symbolsIndex
|
from 1 to (last line index - 1)
|
||||||
// For every line
|
*/
|
||||||
for i := 0; i < numLines; i++ {
|
firstLineNums := renum.FindAllStringIndex(lines[0], -1)
|
||||||
// We put all the symbols in a string
|
firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1)
|
||||||
tempSymbols := resym.FindAllString(lines[i], -1)
|
secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1)
|
||||||
// We associate symbols with an index
|
// For every *number index range*, check if in the same line there is a
|
||||||
tempSymbolsIndex := resym.FindAllStringIndex(lines[i], -1)
|
// symbol on (first - 1) or (last + 1), check in other lines if there is
|
||||||
// Add symbols and indexes to the arrays
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
symbols = append(symbols, tempSymbols)
|
// can break as you just need one symbol
|
||||||
//symbolsIndex = append(symbolsIndex, tempSymbolsIndex)
|
for i := range firstLineNums {
|
||||||
|
for j := range firstLineSymbolsIndex {
|
||||||
|
if firstLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1 &&
|
||||||
|
(firstLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
|
totalSum += numbers[0][i]
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for j := range secondLineSymbolsIndex {
|
||||||
|
if (secondLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1) &&
|
||||||
|
(secondLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
|
totalSum += numbers[0][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now we loop from 1 to (last index - i)
|
||||||
|
for i := 1; i < len(lines) - 1; i++ {
|
||||||
|
// We need to check the current line against an interval of three lines
|
||||||
|
// breaking the loop for a single number as soon as we find a match
|
||||||
|
// (we don't want duplicate matches)
|
||||||
|
currentLineNums := renum.FindAllStringIndex(lines[i], -1)
|
||||||
|
previousLineIndex := resym.FindAllStringIndex(lines[i - 1], -1)
|
||||||
|
currentLineIndex := resym.FindAllStringIndex(lines[i], -1)
|
||||||
|
nextLineIndex := resym.FindAllStringIndex(lines[i + 1], -1)
|
||||||
|
OuterLoop:
|
||||||
|
for k := range currentLineNums {
|
||||||
|
for j := range previousLineIndex {
|
||||||
|
if previousLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
previousLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range currentLineIndex {
|
||||||
|
if currentLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
currentLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range nextLineIndex {
|
||||||
|
if nextLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
nextLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now we need to loop the last line and confront it with previous
|
||||||
|
// and itself
|
||||||
|
lastLineNums := renum.FindAllStringIndex(lines[len(lines) - 1], -1)
|
||||||
|
lastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 1], -1)
|
||||||
|
notLastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 2], -1)
|
||||||
|
// For every *number index range*, check if in the same line there is a
|
||||||
|
// symbol on (last - 1) or (last + 1), check in other lines if there is
|
||||||
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
|
// can break as you just need one symbol
|
||||||
|
for i := range lastLineNums {
|
||||||
|
for j := range lastLineSymbolsIndex {
|
||||||
|
if lastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1 &&
|
||||||
|
(lastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range notLastLineSymbolsIndex {
|
||||||
|
if (notLastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1) &&
|
||||||
|
(notLastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("The total sum is: %d\n", totalSum)
|
||||||
|
CheckGears(lines)
|
||||||
|
}
|
||||||
|
|||||||
1
day04/inputs
Symbolic link
1
day04/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
115
day04/scratchcards.go
Normal file
115
day04/scratchcards.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
// "strconv"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SplitSets(s []string) ([]string, []string) {
|
||||||
|
|
||||||
|
// Split the two sets into winning and my numbers
|
||||||
|
tempwinningNumbers := s[0]
|
||||||
|
tempMyNumbers := s[1]
|
||||||
|
// Regex to populate a string with numbers
|
||||||
|
renum := regexp.MustCompile("[0-9]+")
|
||||||
|
myNumbers := renum.FindAllString(tempMyNumbers, -1)
|
||||||
|
winningNumbers := renum.FindAllString(tempwinningNumbers, -1)
|
||||||
|
|
||||||
|
return myNumbers, winningNumbers
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindMatches(myNum []string, winNum []string) int {
|
||||||
|
matches := 0
|
||||||
|
for i := range myNum {
|
||||||
|
for j := range winNum {
|
||||||
|
if (myNum[i] == winNum[j]) {
|
||||||
|
matches += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalcTickets(s []string, index int, tpr []int ) {
|
||||||
|
|
||||||
|
myNumbers, winningNumbers := SplitSets(s)
|
||||||
|
matches := FindMatches(myNumbers, winningNumbers)
|
||||||
|
if (matches > 0) {
|
||||||
|
for j := 0; j < tpr[index]; j++ {
|
||||||
|
for i := index; i < index + matches; i++ {
|
||||||
|
tpr[i+1] += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalcScore (s []string, t *int) {
|
||||||
|
|
||||||
|
myNumbers, winningNumbers := SplitSets(s)
|
||||||
|
matches := FindMatches(myNumbers, winningNumbers)
|
||||||
|
if(matches > 0) {
|
||||||
|
tempTotal := 1
|
||||||
|
for i := 0; i < matches - 1; i++ {
|
||||||
|
tempTotal *= 2
|
||||||
|
}
|
||||||
|
*t += tempTotal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("./inputs/day04_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// The total is a simple sum
|
||||||
|
var total int = 0
|
||||||
|
var totalPoint *int = &total
|
||||||
|
// To keep track of the amount of tickets, an array as long
|
||||||
|
// as the number of lines
|
||||||
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
ticketsPerRow := make([]int, len(lines))
|
||||||
|
tprPoint := ticketsPerRow[0:len(lines)]
|
||||||
|
|
||||||
|
for i := range ticketsPerRow {
|
||||||
|
ticketsPerRow[i] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan every line
|
||||||
|
for i := 0; i < len(lines); i++ {
|
||||||
|
// e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||||
|
cardAndNumbers := strings.Split(lines[i], ":")
|
||||||
|
// At this point, cardAndNumbers has "Card N"
|
||||||
|
// We don't need this information (yet?)
|
||||||
|
allNumbers := strings.Split(cardAndNumbers[1], "|")
|
||||||
|
CalcScore(allNumbers, totalPoint)
|
||||||
|
CalcTickets(allNumbers, i, tprPoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
numTickets := 0
|
||||||
|
for i := range ticketsPerRow {
|
||||||
|
numTickets += ticketsPerRow[i]
|
||||||
|
}
|
||||||
|
fmt.Printf("The scratchcards are worth %d points.\n", total)
|
||||||
|
fmt.Printf("In total, I have %d scratchcards.", numTickets)
|
||||||
|
}
|
||||||
1
day05/inputs
Symbolic link
1
day05/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
158
day05/seeds.go
Normal file
158
day05/seeds.go
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
// "strings"
|
||||||
|
"bufio"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Minimum struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
minimum int
|
||||||
|
}
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMaps(scanner *bufio.Scanner, re *regexp.Regexp) [][]int {
|
||||||
|
// Scan until there is an empty line
|
||||||
|
var tempNums int = 0
|
||||||
|
tempArray := make([][]int, 0)
|
||||||
|
for i := 0; scanner.Scan() && scanner.Text() != ""; i++ {
|
||||||
|
tempString := re.FindAllString(scanner.Text(), -1)
|
||||||
|
temp := make([]int, 0)
|
||||||
|
for j := range tempString {
|
||||||
|
tempNums, _ = strconv.Atoi(tempString[j])
|
||||||
|
temp = append(temp, tempNums)
|
||||||
|
}
|
||||||
|
tempArray = append(tempArray, temp)
|
||||||
|
}
|
||||||
|
// Prepare for next line
|
||||||
|
scanner.Scan()
|
||||||
|
return tempArray
|
||||||
|
}
|
||||||
|
|
||||||
|
func (min *Minimum) ParallelMinimum(start, finish int, atrocity [][][]int, wg *sync.WaitGroup) {
|
||||||
|
// We check the array one by one, need a temp array because
|
||||||
|
// SeedToLocation wants it
|
||||||
|
tempNum := make([]int, 1)
|
||||||
|
tempMin := []int{0}
|
||||||
|
_ = tempMin
|
||||||
|
for i := 0; i < finish; i++ {
|
||||||
|
tempNum[0] = start + i
|
||||||
|
tempMin := SeedToLocation(tempNum, atrocity)
|
||||||
|
// Need to modify a shared variable, lock
|
||||||
|
min.mu.Lock()
|
||||||
|
if tempMin[0] < min.minimum {
|
||||||
|
min.minimum = tempMin[0]
|
||||||
|
}
|
||||||
|
min.mu.Unlock()
|
||||||
|
}
|
||||||
|
// We finished with the Goroutine
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SeedToLocation(seeds []int, atrocity [][][]int) []int {
|
||||||
|
tempRes := seeds
|
||||||
|
for i := range atrocity {
|
||||||
|
tempRes = NextResource(tempRes, atrocity[i])
|
||||||
|
}
|
||||||
|
return tempRes
|
||||||
|
}
|
||||||
|
|
||||||
|
func NextResource(previous []int, resource [][]int) []int {
|
||||||
|
tempRes := make([]int, 0)
|
||||||
|
// [0] is dest, [1] is source, [2] is range
|
||||||
|
for i := range previous {
|
||||||
|
for j := range resource {
|
||||||
|
if previous[i] >= resource[j][1] &&
|
||||||
|
previous[i] <= (resource[j][1] + resource[j][2] - 1) {
|
||||||
|
tempRes = append(tempRes, previous[i] + (resource[j][0] - resource[j][1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If we didn't add an element to the array
|
||||||
|
if len(tempRes) == i {
|
||||||
|
tempRes = append(tempRes, previous[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tempRes
|
||||||
|
}
|
||||||
|
func main () {
|
||||||
|
file, err := os.Open("./inputs/day05_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
min := Minimum{
|
||||||
|
minimum: math.MaxInt,
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Regex that finds the numbers in a row
|
||||||
|
renum := regexp.MustCompile("[0-9]+")
|
||||||
|
|
||||||
|
var seeds []int
|
||||||
|
var soils, fertilizers, waters, lights, temperatures,
|
||||||
|
humidities, locations [][]int
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
// We know that the seeds only have one row
|
||||||
|
scanner.Scan()
|
||||||
|
// Put all the numbers in an array of strings
|
||||||
|
seedsNums := renum.FindAllString(scanner.Text(), -1)
|
||||||
|
// Extract every number from the string
|
||||||
|
for i := 0; i < len(seedsNums); i++ {
|
||||||
|
num, _ := strconv.Atoi(seedsNums[i])
|
||||||
|
seeds = append(seeds, num)
|
||||||
|
}
|
||||||
|
// We know we have an empty string and just a title, skip them
|
||||||
|
scanner.Scan()
|
||||||
|
scanner.Scan()
|
||||||
|
// Should be possible to just pass the scanner
|
||||||
|
soils = GetMaps(scanner, renum)
|
||||||
|
fertilizers = GetMaps(scanner, renum)
|
||||||
|
waters = GetMaps(scanner, renum)
|
||||||
|
lights = GetMaps(scanner, renum)
|
||||||
|
temperatures = GetMaps(scanner, renum)
|
||||||
|
humidities = GetMaps(scanner, renum)
|
||||||
|
locations = GetMaps(scanner, renum)
|
||||||
|
|
||||||
|
tempRes := make([]int, 0)
|
||||||
|
// Actually insane behaviour
|
||||||
|
monster := [][][]int{
|
||||||
|
soils, fertilizers, waters,
|
||||||
|
lights, temperatures, humidities,
|
||||||
|
locations,
|
||||||
|
}
|
||||||
|
// Send the seeds, receive
|
||||||
|
tempRes = SeedToLocation(seeds, monster)
|
||||||
|
|
||||||
|
minimum := math.MaxInt
|
||||||
|
for i := range tempRes {
|
||||||
|
if tempRes[i] < minimum {
|
||||||
|
minimum = tempRes[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Minimum of first part: %d\n", minimum)
|
||||||
|
|
||||||
|
// Actual madness
|
||||||
|
for i := 0; i < len(seeds); i += 2 {
|
||||||
|
wg.Add(1)
|
||||||
|
go min.ParallelMinimum(seeds[i], seeds[i+1], monster, &wg)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
//tempRes = SeedToLocation(allSeeds, monster)
|
||||||
|
fmt.Printf("Minimum of second part: %d\n", min.minimum)
|
||||||
|
}
|
||||||
1
day06/inputs
Symbolic link
1
day06/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
126
day06/race.go
Normal file
126
day06/race.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
// "strings"
|
||||||
|
"bufio"
|
||||||
|
// "math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Race struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
total int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Race) WeRaceBoys(time, distance int, wg *sync.WaitGroup) {
|
||||||
|
// As we just need to find the minimum necessary and then subtract it from
|
||||||
|
// the maximum, probably a good idea starting form the middle
|
||||||
|
tempTime := 0
|
||||||
|
for i := int(time/2); i > 0; i-- {
|
||||||
|
tempDist := i * (time - i)
|
||||||
|
if tempDist <= distance {
|
||||||
|
tempTime = i + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the minimum is tempTime, then the maximum is time - tempTime
|
||||||
|
// time - 2*tempTime is the number of possible victories
|
||||||
|
r.mu.Lock()
|
||||||
|
r.total *= (time - (2 * tempTime - 1))
|
||||||
|
r.mu.Unlock()
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func MultRaceDist(time, dist []string) ([]int, []int){
|
||||||
|
tempT, tempD := make([]int, 0), make([]int, 0)
|
||||||
|
for i := range time {
|
||||||
|
num, _ := strconv.Atoi(time[i])
|
||||||
|
tempT = append(tempT, num)
|
||||||
|
num, _ = strconv.Atoi(dist[i])
|
||||||
|
tempD = append(tempD, num)
|
||||||
|
}
|
||||||
|
return tempT, tempD
|
||||||
|
}
|
||||||
|
|
||||||
|
func SingleRaceDist(time, dist []string) (int, int) {
|
||||||
|
strT, strD := "", ""
|
||||||
|
numT, numD := 0, 0
|
||||||
|
// Create two big strings
|
||||||
|
for i := range time {
|
||||||
|
strT += time[i]
|
||||||
|
strD += dist[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
numT, _ = strconv.Atoi(strT)
|
||||||
|
numD, _ = strconv.Atoi(strD)
|
||||||
|
|
||||||
|
return numT, numD
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
file, err := os.Open("./inputs/day06_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Struct for multiple races
|
||||||
|
rMult := Race{
|
||||||
|
total: 1,
|
||||||
|
}
|
||||||
|
// Struct for a single race
|
||||||
|
rSing := Race{
|
||||||
|
total: 1,
|
||||||
|
}
|
||||||
|
_ = &rSing
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Regex that finds the numbers in a row
|
||||||
|
renum := regexp.MustCompile("[0-9]+")
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
time, distance := make([]int, 0), make([]int, 0)
|
||||||
|
tempStrings := make([]string, 0)
|
||||||
|
// Generic, would work for more rows
|
||||||
|
for scanner.Scan() {
|
||||||
|
tempStrings = append(tempStrings, scanner.Text())
|
||||||
|
}
|
||||||
|
// Now populate time and distance
|
||||||
|
timeStr := renum.FindAllString(tempStrings[0], - 1)
|
||||||
|
distStr := renum.FindAllString(tempStrings[1], - 1)
|
||||||
|
|
||||||
|
time, distance = MultRaceDist(timeStr, distStr)
|
||||||
|
|
||||||
|
// E.g.: if I hold the button for 1ms and then release it, it will travel at
|
||||||
|
// 1mm/ms fo the remaining amount of seconds.
|
||||||
|
// We can skip the holding down 0 and tMAX ms.
|
||||||
|
// Once we find the MIN amount of ms necessary to win, the limit is
|
||||||
|
// MAX - MIN
|
||||||
|
wg.Add(len(time))
|
||||||
|
for i := 0; i < len(time); i++ {
|
||||||
|
go rMult.WeRaceBoys(time[i], distance[i], &wg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Silly implementation of the single race
|
||||||
|
singT, singD := SingleRaceDist(timeStr, distStr)
|
||||||
|
wg.Add(1)
|
||||||
|
go rSing.WeRaceBoys(singT, singD, &wg)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Printf("Multiple races result: %d.\n", rMult.total)
|
||||||
|
fmt.Printf("Single race result: %d.\n", rSing.total)
|
||||||
|
}
|
||||||
366
day07/cards.go
Normal file
366
day07/cards.go
Normal file
@@ -0,0 +1,366 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var mapSeedsFirst = map[string]int{
|
||||||
|
"A": 12,
|
||||||
|
"K": 11,
|
||||||
|
"Q": 10,
|
||||||
|
"J": 9,
|
||||||
|
"T": 8,
|
||||||
|
"9": 7,
|
||||||
|
"8": 6,
|
||||||
|
"7": 5,
|
||||||
|
"6": 4,
|
||||||
|
"5": 3,
|
||||||
|
"4": 2,
|
||||||
|
"3": 1,
|
||||||
|
"2": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
var mapSeedsSecond = map[string]int{
|
||||||
|
"A": 12,
|
||||||
|
"K": 11,
|
||||||
|
"Q": 10,
|
||||||
|
"T": 9,
|
||||||
|
"9": 8,
|
||||||
|
"8": 7,
|
||||||
|
"7": 6,
|
||||||
|
"6": 5,
|
||||||
|
"5": 4,
|
||||||
|
"4": 3,
|
||||||
|
"3": 2,
|
||||||
|
"2": 1,
|
||||||
|
"J": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
ranks []int
|
||||||
|
// 0: High card, 1: One pair, 2: Two pair, 3: Three of a kind
|
||||||
|
// 4: Full house, 5: Four of a kind, 6: Five of a kind
|
||||||
|
typeOfHand [7][]string
|
||||||
|
indexOfHand [7][]int
|
||||||
|
baseThirteen [7][]int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) ChangeBase(hType, index int, mapSeeds map[string]int, wg *sync.WaitGroup) {
|
||||||
|
// Starting from the first char [0], we create the base13 num
|
||||||
|
chars := len(g.typeOfHand[hType][index])
|
||||||
|
baseTN := g.typeOfHand[hType][index]
|
||||||
|
decNum := 0
|
||||||
|
for i := 0; i < chars; i++ {
|
||||||
|
// This should be refactored to be a bit more legible
|
||||||
|
// It just computes N * 13^i and adds it over
|
||||||
|
decNum += int(float64(mapSeeds[string(baseTN[i])]) * math.Pow(13, float64(chars-i)))
|
||||||
|
}
|
||||||
|
g.baseThirteen[hType][index] = decNum
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) AnalyzeMap(cards string, index, mapSize, offset int) {
|
||||||
|
// The offset defaults to 0 for the regular game
|
||||||
|
// it is +1 for a game with Jokers\
|
||||||
|
mapSeed := mapSeedsFirst
|
||||||
|
if offset != 0 {
|
||||||
|
mapSeed = mapSeedsSecond
|
||||||
|
}
|
||||||
|
switch mapSize {
|
||||||
|
// Five of a kind
|
||||||
|
case 1:
|
||||||
|
g.mu.Lock()
|
||||||
|
g.typeOfHand[6] = append(g.typeOfHand[6], cards)
|
||||||
|
g.indexOfHand[6] = append(g.indexOfHand[6], index)
|
||||||
|
g.mu.Unlock()
|
||||||
|
// Four of a kind || Full House
|
||||||
|
case 2:
|
||||||
|
i := FullOrFour(cards, offset, mapSeed)
|
||||||
|
g.mu.Lock()
|
||||||
|
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
||||||
|
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
||||||
|
g.mu.Unlock()
|
||||||
|
// Three of a kind || Two pair
|
||||||
|
case 3:
|
||||||
|
i := ThreeOrTwo(cards, offset, mapSeed)
|
||||||
|
g.mu.Lock()
|
||||||
|
g.typeOfHand[i] = append(g.typeOfHand[i], cards)
|
||||||
|
g.indexOfHand[i] = append(g.indexOfHand[i], index)
|
||||||
|
g.mu.Unlock()
|
||||||
|
// One pair
|
||||||
|
case 4:
|
||||||
|
g.mu.Lock()
|
||||||
|
g.typeOfHand[1] = append(g.typeOfHand[1], cards)
|
||||||
|
g.indexOfHand[1] = append(g.indexOfHand[1], index)
|
||||||
|
g.mu.Unlock()
|
||||||
|
// High card
|
||||||
|
case 5:
|
||||||
|
g.mu.Lock()
|
||||||
|
g.typeOfHand[0] = append(g.typeOfHand[0], cards)
|
||||||
|
g.indexOfHand[0] = append(g.indexOfHand[0], index)
|
||||||
|
g.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) DetermineType(cards string, index int, wg *sync.WaitGroup) {
|
||||||
|
// We create a map and we check the length. Depending on the length, we
|
||||||
|
// insert the string in a specific type
|
||||||
|
m := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
m[key] = mapSeedsFirst[key]
|
||||||
|
}
|
||||||
|
// Now, depending on the number of elements in the map, we can assign
|
||||||
|
// append cards to a specific rank
|
||||||
|
mapSize := len(m)
|
||||||
|
g.AnalyzeMap(cards, index, mapSize, 0)
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) SecondGame(cards string, index int, wg *sync.WaitGroup) {
|
||||||
|
// If I don't have Js, run the standard DetermineType
|
||||||
|
m := make(map[string]int)
|
||||||
|
n := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
// We need to track the number of Js
|
||||||
|
m[key] += 1
|
||||||
|
// This is to track the type of hand, knowing the number of Js
|
||||||
|
n[key] = mapSeedsSecond[key]
|
||||||
|
}
|
||||||
|
mapSize := len(n)
|
||||||
|
switch m["J"] {
|
||||||
|
case 0:
|
||||||
|
// We have a hand without Js
|
||||||
|
wg.Add(1)
|
||||||
|
g.DetermineType(cards, index, wg)
|
||||||
|
case 1:
|
||||||
|
// If there is a J, J can be use is not adding information, therefore -1
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 1)
|
||||||
|
case 2:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 2)
|
||||||
|
case 3:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 3)
|
||||||
|
case 4:
|
||||||
|
g.AnalyzeMap(cards, index, (mapSize - 1), 4)
|
||||||
|
case 5:
|
||||||
|
wg.Add(1)
|
||||||
|
g.DetermineType(cards, index, wg)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ThreeOrTwo(cards string, offset int, mapSeed map[string]int) int {
|
||||||
|
m := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
m[key] += 1
|
||||||
|
}
|
||||||
|
// If we are in the second game, remove J
|
||||||
|
if mapSeed["J"] == 0 {
|
||||||
|
m["J"] = 0
|
||||||
|
}
|
||||||
|
// m[i] returns 0 if the element is not in the map. I take advantage
|
||||||
|
// of that
|
||||||
|
tempNum := 0
|
||||||
|
for i := range mapSeed {
|
||||||
|
if m[i] > tempNum {
|
||||||
|
tempNum = m[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If an element has 3 values, we have a three of a kind
|
||||||
|
if tempNum+offset == 3 {
|
||||||
|
return 3
|
||||||
|
/// If an element has 2 values, we have a two pair
|
||||||
|
} else if tempNum+offset == 2 {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
PrintAndWait("This has run for ", cards)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
func FullOrFour(cards string, offset int, mapSeed map[string]int) int {
|
||||||
|
m := make(map[string]int)
|
||||||
|
for i := 0; i < len(cards); i++ {
|
||||||
|
key := string(cards[i])
|
||||||
|
m[key] += 1
|
||||||
|
}
|
||||||
|
// If we are in the second game, remove J
|
||||||
|
if mapSeed["J"] == 0 {
|
||||||
|
m["J"] = 0
|
||||||
|
}
|
||||||
|
// m[i] returns 0 if the element is not in the map. I take advantage
|
||||||
|
// of that
|
||||||
|
// Better to save the maximum number
|
||||||
|
tempNum := 0
|
||||||
|
for i := range mapSeed {
|
||||||
|
if m[i] > tempNum {
|
||||||
|
tempNum = m[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If an element has four values, we have a Four of a kind
|
||||||
|
if tempNum+offset == 4 {
|
||||||
|
return 5
|
||||||
|
/// If an element has 3 values, we have a Full House
|
||||||
|
} else if tempNum+offset == 3 {
|
||||||
|
return 4
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.golangprograms.com/golang-program-for-implementation-of-quick-sort.html
|
||||||
|
// I need to learn how this shing works
|
||||||
|
func quicksort(a, h []int, hType int) []int {
|
||||||
|
if len(a) < 2 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
left, right := 0, len(a)-1
|
||||||
|
|
||||||
|
pivot := 0
|
||||||
|
|
||||||
|
a[pivot], a[right] = a[right], a[pivot]
|
||||||
|
h[pivot], h[right] = h[right], h[pivot]
|
||||||
|
|
||||||
|
for i, _ := range a {
|
||||||
|
if a[i] < a[right] {
|
||||||
|
a[left], a[i] = a[i], a[left]
|
||||||
|
h[left], h[i] = h[i], h[left]
|
||||||
|
left++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a[left], a[right] = a[right], a[left]
|
||||||
|
h[left], h[right] = h[right], h[left]
|
||||||
|
|
||||||
|
quicksort(a[:left], h[:left], hType)
|
||||||
|
quicksort(a[left+1:], h[left+1:], hType)
|
||||||
|
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
file, err := os.Open("./inputs/day07_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Struct for regular game
|
||||||
|
g := Game{}
|
||||||
|
// Struct for the Joker game
|
||||||
|
jo := Game{}
|
||||||
|
|
||||||
|
// Variable where we store every line in the file
|
||||||
|
lines := make([]string, 0)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
// Array of strings, set of cards
|
||||||
|
var cards []string
|
||||||
|
// Array of int, bet
|
||||||
|
var bet []int
|
||||||
|
// Now, split the lines
|
||||||
|
for i := 0; i < len(lines); i++ {
|
||||||
|
tempString := strings.Split(lines[i], " ")
|
||||||
|
cards = append(cards, tempString[0])
|
||||||
|
tempNum, _ := strconv.Atoi(tempString[1])
|
||||||
|
bet = append(bet, tempNum)
|
||||||
|
}
|
||||||
|
// Rank will be from 1 to len(lines)
|
||||||
|
g.ranks = make([]int, len(lines))
|
||||||
|
jo.ranks = make([]int, len(lines))
|
||||||
|
// What do we know for sure? 5 identical seeds are the highest ranks,
|
||||||
|
// 5 completely different seeds are the lowest ranks.
|
||||||
|
// We can iterate for every set of cards, and do different things
|
||||||
|
// if the map we build has one element, five elements or the worst
|
||||||
|
// case (two, three or four elements).
|
||||||
|
//
|
||||||
|
// Two identical: 4oK or FH
|
||||||
|
// Three identical: 3oK or 22
|
||||||
|
// Four identical: 12
|
||||||
|
//
|
||||||
|
// With this, I put every typeOfHand in a different array
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2 * len(lines))
|
||||||
|
for i := 0; i < len(lines); i++ {
|
||||||
|
g.DetermineType(cards[i], i, &wg)
|
||||||
|
jo.SecondGame(cards[i], i, &wg)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
// Now that g.typeOfHand has every type of hand separated, the rank
|
||||||
|
// is determined by the first card(s). I can convert every number
|
||||||
|
// to a base 13 representation and sort.
|
||||||
|
// In g.indexOfHand I have the index of the corresponding type.
|
||||||
|
|
||||||
|
// For every type of hand
|
||||||
|
for i := range g.typeOfHand {
|
||||||
|
// As many wait groups as the element we will iterate
|
||||||
|
wg.Add(len(g.typeOfHand[i]))
|
||||||
|
wg.Add(len(jo.typeOfHand[i]))
|
||||||
|
// We also need to initialize the array g.baseThirteen so we can
|
||||||
|
// keep the same index
|
||||||
|
g.baseThirteen[i] = make([]int, len(g.typeOfHand[i]))
|
||||||
|
jo.baseThirteen[i] = make([]int, len(jo.typeOfHand[i]))
|
||||||
|
// For every element in a single type of hand
|
||||||
|
for j := range g.typeOfHand[i] {
|
||||||
|
g.ChangeBase(i, j, mapSeedsFirst, &wg)
|
||||||
|
}
|
||||||
|
for j := range jo.typeOfHand[i] {
|
||||||
|
jo.ChangeBase(i, j, mapSeedsSecond, &wg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// A sort of some kind. Important is to also move the index with the number as
|
||||||
|
// well.
|
||||||
|
for i := range g.baseThirteen {
|
||||||
|
quicksort(g.baseThirteen[i], g.indexOfHand[i], i)
|
||||||
|
quicksort(jo.baseThirteen[i], jo.indexOfHand[i], i)
|
||||||
|
}
|
||||||
|
|
||||||
|
curRank := 1
|
||||||
|
rank := 0
|
||||||
|
// Iter every array
|
||||||
|
for i := range g.typeOfHand {
|
||||||
|
for j := range g.typeOfHand[i] {
|
||||||
|
index := g.indexOfHand[i][j]
|
||||||
|
rank += curRank * bet[index]
|
||||||
|
curRank++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Rank: %d\n", rank)
|
||||||
|
|
||||||
|
curRank = 1
|
||||||
|
rank = 0
|
||||||
|
// Iter every array
|
||||||
|
for i := range jo.typeOfHand {
|
||||||
|
for j := range jo.typeOfHand[i] {
|
||||||
|
index := jo.indexOfHand[i][j]
|
||||||
|
rank += curRank * bet[index]
|
||||||
|
curRank++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Rank: %d\n", rank)
|
||||||
|
|
||||||
|
}
|
||||||
1
day07/inputs
Symbolic link
1
day07/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
233
day08/charpath.go
Normal file
233
day08/charpath.go
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const LEFT = 'L'
|
||||||
|
|
||||||
|
type Nodes struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
commands []int32
|
||||||
|
singleN []int32
|
||||||
|
leftN []int32
|
||||||
|
rightN []int32
|
||||||
|
index int
|
||||||
|
steps uint64
|
||||||
|
allSteps []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://siongui.github.io/2017/06/03/go-find-lcm-by-gcd/
|
||||||
|
// greatest common divisor (GCD) via Euclidean algorithm
|
||||||
|
func GCD(a, b int) int {
|
||||||
|
for b != 0 {
|
||||||
|
t := b
|
||||||
|
b = a % b
|
||||||
|
a = t
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
// find Least Common Multiple (LCM) via GCD
|
||||||
|
func LCM(a, b int, integers ...int) int {
|
||||||
|
result := a * b / GCD(a, b)
|
||||||
|
|
||||||
|
for i := 0; i < len(integers); i++ {
|
||||||
|
result = LCM(result, integers[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Nodes) toByteSingle(s string) {
|
||||||
|
// I've just received something like AAA
|
||||||
|
var temp int32
|
||||||
|
for i := len(s) - 1; i >= 0; i-- {
|
||||||
|
a := int32(s[i])
|
||||||
|
temp += a << ((len(s) - 1 - i) * 8)
|
||||||
|
}
|
||||||
|
n.singleN = append(n.singleN, temp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Nodes) toByteDuet(s, r string) {
|
||||||
|
// I've just received something like AAA BBB
|
||||||
|
var tempL, tempR int32
|
||||||
|
for i := len(s) - 1; i >= 0; i-- {
|
||||||
|
tempL += int32(s[i]) << ((len(s) - 1 - i) * 8)
|
||||||
|
tempR += int32(r[i]) << ((len(s) - 1 - i) * 8)
|
||||||
|
}
|
||||||
|
n.leftN = append(n.leftN, tempL)
|
||||||
|
n.rightN = append(n.rightN, tempR)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Nodes) findNext(myN int32) int {
|
||||||
|
//var wg sync.WaitGroup
|
||||||
|
ind := 0
|
||||||
|
for i := 0; i < len(n.singleN); i++ {
|
||||||
|
if myN^n.singleN[i] == 0 {
|
||||||
|
n.mu.Lock()
|
||||||
|
n.index = i
|
||||||
|
n.mu.Unlock()
|
||||||
|
ind = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ind
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Nodes) findAll(ind int, sp []int, wg *sync.WaitGroup) {
|
||||||
|
index := 0
|
||||||
|
// We only go from the start
|
||||||
|
matching := n.rightN[sp[ind]]
|
||||||
|
if n.commands[0]^LEFT == 0 {
|
||||||
|
matching = n.leftN[sp[ind]]
|
||||||
|
}
|
||||||
|
index = n.findNext(matching)
|
||||||
|
n.allSteps[ind]++
|
||||||
|
i := 0
|
||||||
|
for {
|
||||||
|
// Every step is in a single direction. For every step, we may need to
|
||||||
|
// scan len(n.singleN) elements.
|
||||||
|
// Circular loop
|
||||||
|
index = n.findNext(matching)
|
||||||
|
// Increment i after finding the match
|
||||||
|
i++
|
||||||
|
i = i % len(n.commands)
|
||||||
|
// By default, we will assume we are on the right
|
||||||
|
matching = n.rightN[index]
|
||||||
|
//PrintAndWait()
|
||||||
|
// If we are not, we are in the left
|
||||||
|
if n.commands[i]^LEFT == 0 {
|
||||||
|
matching = n.leftN[index]
|
||||||
|
}
|
||||||
|
n.allSteps[ind]++
|
||||||
|
// If we find XXZ, end
|
||||||
|
temp := matching & 255
|
||||||
|
if temp ^ 'Z' == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//fmt.Printf("I started from %d, matched at %d, taking %d steps.\n", sp[ind], index, n.allSteps[ind] )
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func timer(name string) func() {
|
||||||
|
start := time.Now()
|
||||||
|
return func() {
|
||||||
|
fmt.Printf("%s took %v\n", name, time.Since(start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer timer ("main")()
|
||||||
|
file, err := os.Open("./inputs/day08_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
// Struct with my node
|
||||||
|
n := Nodes{}
|
||||||
|
// Prepare the regex
|
||||||
|
repath := regexp.MustCompile("([A-Z]{3})")
|
||||||
|
// Build the END
|
||||||
|
var END = 'Z'
|
||||||
|
END += ('Z' << 8)
|
||||||
|
END += ('Z' << 16)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
scanner.Scan()
|
||||||
|
// First line, RL commands
|
||||||
|
strCommands := scanner.Text()
|
||||||
|
// Get every char inside the string just obtained
|
||||||
|
for i := 0; i < len(strCommands); i++ {
|
||||||
|
n.commands = append(n.commands, int32(strCommands[i]))
|
||||||
|
}
|
||||||
|
// One empty line
|
||||||
|
scanner.Scan()
|
||||||
|
// X = (Y, Z)
|
||||||
|
// We regex this one
|
||||||
|
for scanner.Scan() {
|
||||||
|
tempNodes := repath.FindAllString(scanner.Text(), -1)
|
||||||
|
n.toByteSingle(tempNodes[0])
|
||||||
|
n.toByteDuet(tempNodes[1], tempNodes[2])
|
||||||
|
}
|
||||||
|
// We start from 0, we find the match
|
||||||
|
// Let's start an infinite loop
|
||||||
|
// Circular index
|
||||||
|
i := 0
|
||||||
|
// We start from the AAA element
|
||||||
|
START := 'A'
|
||||||
|
START += ('A' << 8)
|
||||||
|
START += ('A' << 16)
|
||||||
|
matching := START
|
||||||
|
// Store where AAA is
|
||||||
|
n.findNext(matching)
|
||||||
|
// By default, we will assume we are on the right
|
||||||
|
// If we are not, we are in the left
|
||||||
|
matching = n.rightN[n.index]
|
||||||
|
if n.commands[i]^LEFT == 0 {
|
||||||
|
matching = n.leftN[n.index]
|
||||||
|
}
|
||||||
|
n.steps++
|
||||||
|
// Infinite loop
|
||||||
|
for {
|
||||||
|
// Every step is in a single direction. For every step, we may need to
|
||||||
|
// scan len(n.singleN) elements.
|
||||||
|
// Circular loop
|
||||||
|
n.findNext(matching)
|
||||||
|
// Increment i after finding the match
|
||||||
|
i++
|
||||||
|
i = i % len(n.commands)
|
||||||
|
// By default, we will assume we are on the right
|
||||||
|
matching = n.rightN[n.index]
|
||||||
|
//PrintAndWait()
|
||||||
|
// If we are not, we are in the left
|
||||||
|
if n.commands[i]^LEFT == 0 {
|
||||||
|
matching = n.leftN[n.index]
|
||||||
|
}
|
||||||
|
n.steps++
|
||||||
|
// If we find ZZZ, end
|
||||||
|
if matching^END == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("\nSteps: %d\n", n.steps)
|
||||||
|
// Now, for the main event
|
||||||
|
// Let's get ready to rumble
|
||||||
|
startPoints := make([]int, 0)
|
||||||
|
for i := 0; i < len(n.singleN); i++ {
|
||||||
|
// Lets remove all bytes except last 8
|
||||||
|
temp := n.singleN[i] & 255
|
||||||
|
if (temp ^ 'A') == 0 {
|
||||||
|
startPoints = append(startPoints, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now, from the starting points, we should go and match until
|
||||||
|
// we find a path that ends in Z
|
||||||
|
n.allSteps = make([]int, len(startPoints))
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < len(startPoints); i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go n.findAll(i, startPoints, &wg)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
result := 1
|
||||||
|
for i := 0; i < len(n.allSteps); i++ {
|
||||||
|
result = LCM(result, n.allSteps[i])
|
||||||
|
}
|
||||||
|
fmt.Printf("Steps: %d\n", result)
|
||||||
|
}
|
||||||
1
day08/inputs
Symbolic link
1
day08/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
1
day09/inputs
Symbolic link
1
day09/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
149
day09/oasis.go
Normal file
149
day09/oasis.go
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import(
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"bufio"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Parallel code, global vars
|
||||||
|
type Series struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
numStore [][]int
|
||||||
|
total uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
// use defer timer("funcname")() when the function you want to
|
||||||
|
// test starts
|
||||||
|
func timer(name string) func() {
|
||||||
|
start := time.Now()
|
||||||
|
return func() {
|
||||||
|
fmt.Printf("%s took %v\n", name, time.Since(start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PredictValueBack(numbers []int) []int {
|
||||||
|
// Are we finished? By default, true
|
||||||
|
temp := true
|
||||||
|
for i := 0; i < len(numbers); i++ {
|
||||||
|
if numbers[i] != 0 {
|
||||||
|
temp = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check to end recursion
|
||||||
|
if temp == true {
|
||||||
|
return numbers
|
||||||
|
}
|
||||||
|
|
||||||
|
newNums := make([]int, len(numbers) - 1)
|
||||||
|
for i := 0; i < len(numbers) - 1; i++ {
|
||||||
|
newNums[i] = numbers[i + 1] - numbers[i]
|
||||||
|
}
|
||||||
|
myNums := PredictValueBack(newNums)
|
||||||
|
addValue := newNums[0] - myNums[0]
|
||||||
|
// We need to append at the start
|
||||||
|
newNums = append([]int{addValue}, newNums...)
|
||||||
|
return newNums
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ser *Series) CallPredictBack(numbers []int, wg *sync.WaitGroup) {
|
||||||
|
tempNum := PredictValueBack(numbers)
|
||||||
|
ser.mu.Lock()
|
||||||
|
ser.total += uint64(numbers[0] - tempNum[0])
|
||||||
|
ser.mu.Unlock()
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func PredictValue(numbers []int) []int {
|
||||||
|
// Are we finished? By default, true
|
||||||
|
temp := true
|
||||||
|
for i := 0; i < len(numbers); i++ {
|
||||||
|
if numbers[i] != 0 {
|
||||||
|
temp = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check to end recursion
|
||||||
|
if temp == true {
|
||||||
|
return numbers
|
||||||
|
}
|
||||||
|
|
||||||
|
newNums := make([]int, len(numbers) - 1)
|
||||||
|
for i := 0; i < len(numbers) - 1; i++ {
|
||||||
|
newNums[i] = numbers[i + 1] - numbers[i]
|
||||||
|
}
|
||||||
|
myNums := PredictValue(newNums)
|
||||||
|
addValue := myNums[len(myNums) - 1]
|
||||||
|
newNums = append(newNums, newNums[len(newNums) - 1] + addValue)
|
||||||
|
return newNums
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ser *Series) CallPredict(numbers []int, wg *sync.WaitGroup) {
|
||||||
|
tempNum := PredictValue(numbers)
|
||||||
|
lt := len(tempNum) - 1
|
||||||
|
ln := len(numbers) - 1
|
||||||
|
ser.mu.Lock()
|
||||||
|
ser.total += uint64(numbers[ln] + tempNum[lt])
|
||||||
|
ser.mu.Unlock()
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer timer("main")()
|
||||||
|
file, err := os.Open("./inputs/day09_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
renum := regexp.MustCompile("(\\-[0-9]+|[0-9]+)")
|
||||||
|
|
||||||
|
ser := Series{ total: 0, }
|
||||||
|
|
||||||
|
lines := make([]string, 0)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
ser.numStore = make([][]int, len(lines))
|
||||||
|
for i := 0; i < len(lines); i++ {
|
||||||
|
temp := renum.FindAllString(lines[i], -1)
|
||||||
|
for j := 0; j < len(temp); j++ {
|
||||||
|
num, err := strconv.Atoi(temp[j])
|
||||||
|
check(err)
|
||||||
|
ser.numStore[i] = append(ser.numStore[i], num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now I have a 2D array with all the numbers, I can start RECURSING
|
||||||
|
wg.Add(len(ser.numStore))
|
||||||
|
for i := 0; i < len(ser.numStore); i++ {
|
||||||
|
go ser.CallPredict(ser.numStore[i], &wg)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Printf("%d\n", ser.total)
|
||||||
|
|
||||||
|
ser.total = 0
|
||||||
|
wg.Add(len(ser.numStore))
|
||||||
|
for i := 0; i < len(ser.numStore); i++ {
|
||||||
|
go ser.CallPredictBack(ser.numStore[i], &wg)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Printf("%d\n", ser.total)
|
||||||
|
|
||||||
|
}
|
||||||
1
day10/inputs
Symbolic link
1
day10/inputs
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
191
day10/maze.go
Normal file
191
day10/maze.go
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Parallel code, global vars
|
||||||
|
type Maze struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
direction [][]rune
|
||||||
|
steps int
|
||||||
|
}
|
||||||
|
|
||||||
|
var fromWhere = map[rune]int8{
|
||||||
|
'N': 0,
|
||||||
|
'S': 1,
|
||||||
|
'W': 2,
|
||||||
|
'E': 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
var mapDirections = map[rune]int8{
|
||||||
|
'|': 0, // Vertical
|
||||||
|
'-': 1, // Horizontal
|
||||||
|
'L': 2, // North to East
|
||||||
|
'J': 3, // North to West
|
||||||
|
'7': 4, // South to West
|
||||||
|
'F': 5, // South to East
|
||||||
|
'.': 6, // Ground
|
||||||
|
'S': 7, // Start
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
// use defer timer("funcname")() when the function you want to
|
||||||
|
// test starts
|
||||||
|
func timer(name string) func() {
|
||||||
|
start := time.Now()
|
||||||
|
return func() {
|
||||||
|
fmt.Printf("%s took %v\n", name, time.Since(start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mz *Maze) ChooseFirstDirection(startPoint [2]int) ([2]int, rune) {
|
||||||
|
// For the first step, we don't care about directions, we only need a tile
|
||||||
|
// were we can step on. E.g., if we have a J North of S we CAN'T go. A 7 would
|
||||||
|
// be OK.
|
||||||
|
y := startPoint[0]
|
||||||
|
x := startPoint[1]
|
||||||
|
// If x - 1 (going west), only 1, 2, 5 allowed
|
||||||
|
temp := mapDirections[mz.direction[y][x-1]]
|
||||||
|
if temp == 1 || temp == 2 || temp == 5 {
|
||||||
|
// We came from East, we also return 'E'
|
||||||
|
newSpot := [2]int{y,x-1}
|
||||||
|
return newSpot, 'E'
|
||||||
|
// If x + 1 (going east), only 1, 3, 4 allowed
|
||||||
|
} else if temp := mapDirections[mz.direction[y][x+1]]; temp == 1 || temp == 3 || temp == 4 {
|
||||||
|
// We came from West
|
||||||
|
newSpot := [2]int{y,x+1}
|
||||||
|
return newSpot, 'W'
|
||||||
|
// If y - 1 (going north), only 0, 4, 5 allowed
|
||||||
|
} else if temp := mapDirections[mz.direction[y-1][x]]; temp == 0 || temp == 4 || temp == 5 {
|
||||||
|
// We came from South
|
||||||
|
newSpot := [2]int{y-1,x}
|
||||||
|
return newSpot, 'S'
|
||||||
|
} else {
|
||||||
|
log.Fatal("How is this even possible..?")
|
||||||
|
}
|
||||||
|
s := [2]int{0, 0}
|
||||||
|
return s, 'X'
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mz *Maze) WeGo(loc [2]int, direction rune) ([2]int, rune) {
|
||||||
|
mapDir := fromWhere[direction]
|
||||||
|
y, x := loc[0], loc[1]
|
||||||
|
thisPipe := mapDirections[mz.direction[y][x]]
|
||||||
|
goNorth := [2]int{y-1, x}
|
||||||
|
goSouth := [2]int{y+1, x}
|
||||||
|
goWest := [2]int{y, x-1}
|
||||||
|
goEast := [2]int{y, x+1}
|
||||||
|
switch mapDir {
|
||||||
|
case 0:
|
||||||
|
switch thisPipe {
|
||||||
|
case 0:
|
||||||
|
return goSouth, 'N'
|
||||||
|
case 2:
|
||||||
|
return goEast, 'W'
|
||||||
|
case 3:
|
||||||
|
return goWest, 'E'
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
switch thisPipe {
|
||||||
|
case 0:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 4:
|
||||||
|
return goWest, 'E'
|
||||||
|
case 5:
|
||||||
|
return goEast, 'W'
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
switch thisPipe {
|
||||||
|
case 1:
|
||||||
|
return goEast, 'W'
|
||||||
|
case 3:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 4:
|
||||||
|
return goSouth, 'N'
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
switch thisPipe {
|
||||||
|
case 1:
|
||||||
|
return goWest, 'E'
|
||||||
|
case 2:
|
||||||
|
return goNorth, 'S'
|
||||||
|
case 5:
|
||||||
|
return goSouth, 'N'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s := [2]int{0, 0}
|
||||||
|
return s, 'X'
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mz *Maze) FindPath(startPoint [2]int) {
|
||||||
|
defer timer("FindPath")()
|
||||||
|
y, x := startPoint[0], startPoint[1]
|
||||||
|
nextCheck, direction := mz.ChooseFirstDirection(startPoint)
|
||||||
|
mz.steps++
|
||||||
|
curY, curX := nextCheck[0], nextCheck[1]
|
||||||
|
for {
|
||||||
|
nextCheck, direction = mz.WeGo(nextCheck, direction)
|
||||||
|
curY, curX = nextCheck[0], nextCheck[1]
|
||||||
|
mz.steps++
|
||||||
|
if y == curY && x == curX { break }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) != 2 {
|
||||||
|
fmt.Println("Must provide exactly one argument. It must be a file.")
|
||||||
|
log.Fatal("NOW I WILL SCREAM AND DIE.")
|
||||||
|
}
|
||||||
|
file, err := os.Open(os.Args[1])
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
lines := make([]string, 0)
|
||||||
|
|
||||||
|
mz := Maze{ steps: 0, }
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
numColumns := len(lines)
|
||||||
|
|
||||||
|
mz.direction = make([][]rune, numColumns)
|
||||||
|
// Populate the array of directions
|
||||||
|
for i := 0; i < numColumns; i++ {
|
||||||
|
for j := 0; j < len(lines[i]); j++ {
|
||||||
|
mz.direction[i] = append(mz.direction[i], rune(lines[i][j]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var startPoint [2]int
|
||||||
|
// Search for the starting point
|
||||||
|
for i := 0; i < numColumns; i++ {
|
||||||
|
for j := 0; j < len(lines[i]); j++ {
|
||||||
|
if mapDirections[mz.direction[i][j]] == 7 {
|
||||||
|
startPoint[0] = i
|
||||||
|
startPoint[1] = j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Going west: x - 1 | Going east: x + 1
|
||||||
|
// Going north: y - 1 | Going south: y + 1
|
||||||
|
mz.FindPath(startPoint)
|
||||||
|
fmt.Printf("Number of steps: %d", mz.steps / 2)
|
||||||
|
}
|
||||||
1000
inputs/day01_input
1000
inputs/day01_input
File diff suppressed because it is too large
Load Diff
@@ -1,100 +0,0 @@
|
|||||||
Game 1: 4 red, 1 green, 15 blue; 6 green, 2 red, 10 blue; 7 blue, 6 green, 4 red; 12 blue, 10 green, 3 red
|
|
||||||
Game 2: 3 green, 18 blue; 14 green, 4 red, 2 blue; 3 red, 14 green, 15 blue
|
|
||||||
Game 3: 12 green, 2 blue; 9 green; 1 red, 11 blue, 4 green
|
|
||||||
Game 4: 4 blue, 8 green, 5 red; 6 red, 7 blue, 9 green; 2 green, 2 red, 2 blue; 2 green, 6 blue, 9 red; 10 red, 9 green
|
|
||||||
Game 5: 12 red, 1 green, 7 blue; 13 red, 16 blue; 16 blue, 10 red; 4 blue; 16 blue, 7 red; 1 blue, 7 red
|
|
||||||
Game 6: 17 blue, 2 red; 5 blue, 6 green, 2 red; 5 green, 5 blue; 5 green, 12 blue, 4 red
|
|
||||||
Game 7: 2 red, 1 blue, 10 green; 8 red, 14 green, 9 blue; 15 red, 1 blue, 6 green; 9 blue, 3 green, 10 red; 7 blue, 13 red, 4 green
|
|
||||||
Game 8: 1 green, 2 blue; 7 red, 2 blue, 1 green; 1 red, 2 green; 4 red, 1 blue; 11 red, 2 green, 2 blue; 1 blue, 2 green, 11 red
|
|
||||||
Game 9: 11 green, 11 blue, 6 red; 2 green, 3 blue, 2 red; 2 red, 11 blue, 14 green; 5 green, 7 red, 7 blue; 7 green, 1 red, 12 blue; 1 red, 8 green, 7 blue
|
|
||||||
Game 10: 2 red, 8 green, 7 blue; 10 red, 5 green, 2 blue; 4 red, 8 green, 16 blue; 10 blue, 3 green, 15 red
|
|
||||||
Game 11: 2 blue, 2 green, 5 red; 1 green, 3 red, 3 blue; 11 green, 1 red, 2 blue
|
|
||||||
Game 12: 8 blue, 11 green, 14 red; 10 green, 13 red, 2 blue; 1 red, 6 green, 4 blue; 13 red, 11 green, 6 blue
|
|
||||||
Game 13: 15 red, 17 green, 1 blue; 12 red, 1 blue, 1 green; 2 red, 1 blue, 14 green
|
|
||||||
Game 14: 6 green, 11 red, 3 blue; 6 green, 2 blue; 2 green, 10 red, 8 blue; 2 red; 1 green, 9 red; 3 blue, 1 green, 3 red
|
|
||||||
Game 15: 11 blue, 11 green, 4 red; 3 green, 10 blue; 2 red, 9 green, 9 blue
|
|
||||||
Game 16: 2 blue, 11 green; 1 red, 1 blue, 11 green; 12 green, 1 blue, 1 red; 3 blue, 14 green, 1 red; 14 green, 4 blue; 2 blue, 12 green
|
|
||||||
Game 17: 1 red, 2 blue, 4 green; 4 blue, 3 green; 1 green, 1 red, 6 blue; 1 red, 7 blue; 2 green
|
|
||||||
Game 18: 3 red, 3 blue, 7 green; 2 blue, 2 red, 2 green; 4 red, 12 green; 5 green, 2 blue, 4 red; 3 red
|
|
||||||
Game 19: 15 red, 7 blue, 10 green; 5 green, 8 red; 9 green, 8 red; 5 red, 10 green
|
|
||||||
Game 20: 15 blue, 6 green, 11 red; 13 red, 9 blue, 1 green; 15 blue, 10 red, 11 green
|
|
||||||
Game 21: 15 red, 4 green; 11 red, 2 blue, 4 green; 5 blue, 2 green, 4 red; 4 red, 5 blue; 6 red, 3 blue, 1 green
|
|
||||||
Game 22: 4 green, 4 red, 13 blue; 3 red, 7 blue, 9 green; 12 blue, 13 green, 5 red
|
|
||||||
Game 23: 20 green, 4 red; 6 blue, 9 red, 7 green; 6 green
|
|
||||||
Game 24: 1 green, 3 blue, 6 red; 1 green, 1 blue, 2 red; 3 blue, 5 red, 1 green
|
|
||||||
Game 25: 2 red, 9 blue, 2 green; 2 green, 1 red, 5 blue; 3 red, 1 green, 3 blue; 8 blue, 2 green, 3 red; 12 blue, 3 red; 1 blue, 2 green, 1 red
|
|
||||||
Game 26: 2 blue, 5 green, 20 red; 2 blue, 6 red, 9 green; 3 red, 2 blue, 5 green
|
|
||||||
Game 27: 17 blue, 2 red, 14 green; 15 green, 16 blue, 2 red; 13 blue, 13 green; 1 red, 7 green, 3 blue; 1 blue, 2 green
|
|
||||||
Game 28: 5 blue, 6 red, 3 green; 7 red, 19 green; 11 blue, 13 green
|
|
||||||
Game 29: 1 blue, 8 red, 7 green; 1 green, 1 red; 8 red, 7 green, 1 blue; 7 green, 2 red; 1 blue, 7 red; 1 blue, 2 red, 5 green
|
|
||||||
Game 30: 3 red, 17 blue; 11 red, 3 blue, 8 green; 7 green, 12 blue, 10 red; 5 blue, 2 green
|
|
||||||
Game 31: 14 blue, 7 green; 12 green, 14 blue, 2 red; 17 blue, 2 red, 8 green; 2 red, 3 blue, 11 green; 9 green, 4 blue; 1 red, 3 green, 1 blue
|
|
||||||
Game 32: 15 red, 1 blue, 10 green; 15 green, 10 red, 1 blue; 2 red, 6 green, 1 blue
|
|
||||||
Game 33: 10 green, 1 red, 16 blue; 11 blue, 14 green, 3 red; 14 green, 13 blue; 17 blue, 2 red, 3 green
|
|
||||||
Game 34: 8 red, 7 blue, 8 green; 3 green, 1 red; 1 red, 1 green, 5 blue; 6 red, 8 green, 2 blue; 7 red, 8 blue, 3 green
|
|
||||||
Game 35: 5 blue, 19 red; 2 blue, 11 red, 1 green; 16 red, 10 blue; 7 green, 3 blue, 6 red; 3 green, 18 red, 5 blue; 8 blue, 5 red
|
|
||||||
Game 36: 9 red, 6 green, 10 blue; 9 red, 15 green, 6 blue; 6 red, 1 blue, 14 green
|
|
||||||
Game 37: 7 green, 8 red, 2 blue; 3 blue, 5 red, 16 green; 1 green, 1 red, 3 blue
|
|
||||||
Game 38: 5 green, 5 red, 3 blue; 10 blue, 19 red, 9 green; 2 red, 3 blue, 11 green
|
|
||||||
Game 39: 15 red, 11 blue, 5 green; 11 green, 2 red, 6 blue; 2 blue, 3 green, 6 red; 15 red, 3 blue, 13 green
|
|
||||||
Game 40: 7 green, 4 red, 1 blue; 6 blue, 6 green, 2 red; 2 blue, 3 red, 1 green; 1 blue, 3 red, 3 green; 2 red, 5 green, 3 blue
|
|
||||||
Game 41: 10 blue, 8 green, 9 red; 7 blue, 9 red, 2 green; 10 blue, 4 red, 5 green
|
|
||||||
Game 42: 8 blue, 13 green, 14 red; 8 blue, 1 green, 11 red; 4 red, 6 green, 3 blue; 14 green, 4 red, 2 blue
|
|
||||||
Game 43: 2 red, 10 green, 19 blue; 5 blue, 4 green, 9 red; 9 green, 9 red, 2 blue
|
|
||||||
Game 44: 6 red, 2 green, 3 blue; 2 blue, 12 red, 6 green; 1 red, 10 blue; 12 red, 6 green, 2 blue; 14 red, 13 green, 3 blue; 10 green, 9 blue, 11 red
|
|
||||||
Game 45: 2 blue, 1 red, 1 green; 1 green, 1 blue; 2 green, 2 blue
|
|
||||||
Game 46: 7 green, 1 red; 1 green, 4 blue, 1 red; 3 blue, 4 green, 1 red; 1 red, 4 green; 1 blue, 12 green, 1 red; 16 green, 1 blue
|
|
||||||
Game 47: 4 blue, 8 green, 3 red; 6 red, 1 green, 3 blue; 16 green, 4 blue, 1 red; 4 blue, 8 red
|
|
||||||
Game 48: 1 blue, 9 red, 8 green; 8 green, 2 blue, 6 red; 2 green; 4 blue, 5 red; 1 blue, 9 red, 9 green; 1 red, 1 blue, 3 green
|
|
||||||
Game 49: 3 green, 2 blue; 7 blue, 4 red; 20 green, 5 red, 13 blue; 20 green, 1 red, 6 blue
|
|
||||||
Game 50: 3 red, 3 green; 3 green, 3 red; 2 blue, 10 red; 3 blue, 5 green; 14 red, 2 green, 2 blue; 7 red, 2 green
|
|
||||||
Game 51: 3 green, 3 blue, 2 red; 4 green, 16 red, 3 blue; 1 blue, 3 red; 9 red, 1 blue, 4 green
|
|
||||||
Game 52: 6 red, 18 green, 7 blue; 2 blue, 1 red, 5 green; 8 blue, 6 red, 1 green; 1 red, 1 blue; 6 red, 3 green, 10 blue
|
|
||||||
Game 53: 1 blue, 10 red, 3 green; 13 red, 2 green, 1 blue; 1 green, 2 red
|
|
||||||
Game 54: 4 blue, 6 green, 2 red; 5 blue, 6 red, 2 green; 6 blue, 4 green, 8 red; 13 red, 10 blue, 1 green; 5 red, 5 green, 9 blue
|
|
||||||
Game 55: 4 green, 18 red, 4 blue; 9 blue, 7 green, 16 red; 5 red, 6 blue, 14 green; 13 green, 11 red, 9 blue; 6 blue, 13 green, 1 red; 10 blue, 12 red, 14 green
|
|
||||||
Game 56: 8 green, 5 blue, 10 red; 10 green, 7 red, 12 blue; 11 red, 12 blue, 1 green; 4 blue, 6 red, 10 green; 17 blue, 8 green, 2 red
|
|
||||||
Game 57: 1 green, 2 red; 2 green, 5 red, 1 blue; 13 red, 3 green, 4 blue; 3 blue, 13 red, 9 green
|
|
||||||
Game 58: 1 red, 7 blue, 4 green; 2 green, 1 blue, 1 red; 1 green, 11 blue; 12 blue; 1 blue, 5 green, 1 red; 3 green, 11 blue, 1 red
|
|
||||||
Game 59: 5 green, 3 blue, 17 red; 2 red, 9 green; 1 blue, 4 green
|
|
||||||
Game 60: 5 red, 5 green, 1 blue; 2 red, 2 blue, 6 green; 2 red, 3 blue, 3 green
|
|
||||||
Game 61: 2 green, 3 blue, 4 red; 17 green, 1 blue; 1 green, 6 red, 4 blue; 3 blue, 9 green, 3 red; 18 green, 7 red, 2 blue
|
|
||||||
Game 62: 5 red; 3 blue, 9 green; 3 red, 13 blue, 10 green; 14 green, 1 red, 2 blue; 7 blue, 13 green
|
|
||||||
Game 63: 12 blue, 5 green; 5 green, 1 red, 1 blue; 4 red, 7 green, 9 blue; 8 blue, 2 green, 7 red
|
|
||||||
Game 64: 3 blue, 11 green; 5 blue, 2 red, 5 green; 17 green, 5 blue, 1 red; 4 red, 3 blue, 4 green
|
|
||||||
Game 65: 2 red, 1 blue, 2 green; 7 green, 2 red, 1 blue; 2 blue, 7 green, 1 red; 3 blue, 8 green, 3 red
|
|
||||||
Game 66: 4 red, 12 blue, 1 green; 20 blue, 3 green, 2 red; 11 blue, 1 green
|
|
||||||
Game 67: 12 blue, 10 red, 13 green; 19 green, 4 red, 7 blue; 12 red, 9 blue, 13 green
|
|
||||||
Game 68: 2 blue, 17 green; 12 green, 2 red; 5 red, 2 green, 4 blue; 4 blue
|
|
||||||
Game 69: 17 blue, 3 red, 1 green; 4 green, 8 blue, 8 red; 4 green, 7 red, 1 blue; 8 red, 1 green, 11 blue; 13 blue, 10 red, 9 green; 14 blue, 5 green, 6 red
|
|
||||||
Game 70: 1 red, 2 blue, 4 green; 13 blue, 3 red, 2 green; 6 green, 8 blue
|
|
||||||
Game 71: 5 red, 7 green, 1 blue; 11 green, 4 red, 1 blue; 1 red, 12 green, 10 blue; 1 red, 7 blue, 12 green
|
|
||||||
Game 72: 9 blue, 4 green, 1 red; 6 green, 4 blue; 8 green, 5 blue, 1 red
|
|
||||||
Game 73: 1 blue, 10 green, 14 red; 4 green; 2 blue, 9 red, 4 green; 2 blue, 13 green; 13 green, 13 red; 7 red, 5 green, 2 blue
|
|
||||||
Game 74: 3 red, 1 blue, 3 green; 4 green, 1 blue, 1 red; 2 blue, 10 green, 1 red; 1 blue, 3 red, 1 green
|
|
||||||
Game 75: 1 red, 1 blue, 1 green; 2 red, 1 green, 4 blue; 2 red, 4 blue; 1 blue, 1 red
|
|
||||||
Game 76: 4 green, 2 blue, 6 red; 7 green, 1 red; 8 green, 4 red
|
|
||||||
Game 77: 8 green, 7 blue, 5 red; 6 red, 14 green, 7 blue; 8 green, 7 blue; 1 red, 8 green, 8 blue
|
|
||||||
Game 78: 6 red, 3 blue, 3 green; 7 blue, 10 red; 5 green, 10 blue, 1 red; 3 green, 11 blue, 4 red; 14 red, 9 blue, 2 green; 16 red, 2 green, 12 blue
|
|
||||||
Game 79: 1 green; 5 green; 11 green, 3 blue, 2 red; 3 blue
|
|
||||||
Game 80: 2 green, 2 red; 1 blue, 1 green, 1 red; 1 blue, 1 green, 2 red; 2 red; 5 green
|
|
||||||
Game 81: 10 blue, 2 red, 9 green; 4 red, 12 blue, 5 green; 7 green, 4 blue, 6 red; 1 red, 13 green, 14 blue; 13 green, 11 blue
|
|
||||||
Game 82: 4 blue, 2 green; 7 blue, 3 green, 5 red; 1 red, 4 blue, 3 green; 5 blue, 1 red, 6 green; 6 green, 4 red; 11 blue, 3 red, 5 green
|
|
||||||
Game 83: 12 green; 5 red, 8 green; 11 red, 14 green, 1 blue; 9 green, 4 red
|
|
||||||
Game 84: 5 blue, 1 red; 16 blue, 5 green; 1 red, 9 blue, 3 green; 11 blue; 1 green, 2 blue; 1 red, 7 blue, 4 green
|
|
||||||
Game 85: 17 red, 5 blue; 18 blue, 2 red, 2 green; 18 blue, 2 green, 8 red
|
|
||||||
Game 86: 4 red, 1 blue, 11 green; 6 blue, 7 green, 1 red; 3 green, 4 blue; 2 red, 7 blue, 2 green
|
|
||||||
Game 87: 4 red, 5 blue; 1 green, 15 red, 1 blue; 11 blue, 12 red
|
|
||||||
Game 88: 11 green, 3 red, 1 blue; 6 green, 1 blue, 1 red; 1 blue, 3 green; 2 blue, 4 green, 2 red
|
|
||||||
Game 89: 2 green; 1 red, 2 green, 3 blue; 4 blue, 1 red, 10 green; 4 blue, 5 green; 6 blue, 1 red, 10 green
|
|
||||||
Game 90: 15 red, 7 green, 17 blue; 7 blue, 1 red; 7 green, 6 red, 3 blue
|
|
||||||
Game 91: 2 blue, 17 red, 6 green; 1 green, 1 blue, 6 red; 6 red, 4 blue; 10 green, 14 red, 1 blue; 7 blue, 10 green, 10 red; 16 red, 11 green, 9 blue
|
|
||||||
Game 92: 1 green, 8 blue, 4 red; 4 green, 4 red, 4 blue; 1 green, 7 red, 4 blue
|
|
||||||
Game 93: 11 blue, 12 red, 1 green; 9 blue, 2 green, 5 red; 7 red, 5 blue, 2 green
|
|
||||||
Game 94: 7 blue, 10 green; 9 green, 9 blue, 2 red; 1 red, 5 green, 4 blue
|
|
||||||
Game 95: 1 green, 1 blue, 2 red; 6 red; 1 blue; 1 green, 1 blue, 6 red
|
|
||||||
Game 96: 1 blue, 1 red, 2 green; 4 red, 13 green, 1 blue; 1 blue, 13 green, 5 red; 7 green, 4 red
|
|
||||||
Game 97: 10 blue, 5 red, 5 green; 4 red, 8 green, 2 blue; 5 red, 2 green, 15 blue; 2 red, 1 green, 4 blue; 2 red, 14 blue; 14 blue, 4 green
|
|
||||||
Game 98: 11 red, 8 green, 9 blue; 3 blue, 1 green, 14 red; 10 blue, 2 red, 4 green; 7 blue, 11 red, 3 green; 5 red, 12 blue, 4 green; 7 green, 7 blue, 8 red
|
|
||||||
Game 99: 3 green, 2 blue, 1 red; 15 red, 8 blue, 7 green; 18 red, 12 blue, 2 green
|
|
||||||
Game 100: 11 red, 1 blue, 2 green; 3 red, 3 green; 1 blue, 8 red, 4 green; 5 green, 5 blue, 1 red; 2 green, 1 red, 6 blue; 2 green, 8 red, 1 blue
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
...............307............130..................969...601...186.........................................312....628..........878..........
|
|
||||||
......479#../..*..............................#.....*......*............../309.....484........................*......-..........+.....89....
|
|
||||||
...........726..352...461..69..............435.....390...625....................................459.........152...-....580............*.....
|
|
||||||
.......................*.......454*674.448......65................257....104*762....&..............*269.........558.&.....*907.........652..
|
|
||||||
.....................164.....-..............532*.....................................484......108........955.........252..........321.......
|
|
||||||
.......270....*..............470.......................632....................*...................$............352...................*......
|
|
||||||
820........782.347...................*838..*371..475.......607...557.......201.29..............814....=.....15*....893..............143.....
|
|
||||||
...*..796...........449..138......991.....2........*....28....*.....*588.............................353..................*.................
|
|
||||||
582...../..333.............@..224............%....4.....*.....399........883..#859.........*341...........583*40.......366.951......170*814.
|
|
||||||
...............................*....*.....252......................551.....*.......91...374.....665.804*..........938...........694.........
|
|
||||||
...549.769..............527..466.169.712.......898.......408......*............-...*............*.......912.......*.......771%.../..........
|
|
||||||
.....*...=.875*342......$........................@........-.....846.........610..829.934*508...79............&691..48...................483.
|
|
||||||
....402.............................423-..............................465.........................342.....................@.................
|
|
||||||
..............837..................................264..................................*823.180....@.........%.........538............634..
|
|
||||||
..........807*....990+.................453............*277........657..379...........200.......*.............296..............767..885......
|
|
||||||
744...611................@........+.............500........866.....*......*..717............656..80...254................&775...%..*........
|
|
||||||
.......%.......*.2....878..793.97..143.413.&856......329*...*...732......878./.........191...............*..........+..............584......
|
|
||||||
.........535.506..............*.........-.................368.......................#.+........*.........429.......363......................
|
|
||||||
..564....*................966........................189......=............/24....939......26...547.......................193*241...........
|
|
||||||
.....*......................-..........................*...204........156......7............*................/.717*..................296....
|
|
||||||
..826.....924.......................956..............920..................259.-......$......286........6..955.........................=..457
|
|
||||||
......124*......................48.....=...435..............12.....886....*........411..........*170..%............90..-295.624.............
|
|
||||||
...........................290....*375.............747.....................175..........................653........*...........*............
|
|
||||||
..........642*157...........*.............................218.945..949&...................864+....720..*...........883.786-.....434..-..&...
|
|
||||||
...118.................@.....309...586............698.152....*..............250......*954........*......932...414...................239.39..
|
|
||||||
......@..999..574.......302..................53..=......*......933.69..............21.........538.............@................143..........
|
|
||||||
........*........+...........267..587*........*...........25......*..........241......289.........436.........................*.............
|
|
||||||
.......448.............437....*.......334...605......../..=...........689...%.........*...25.........*....97........171.780....813..836.....
|
|
||||||
......................*........169...............427...94.........389..$........360............/....692.....*......*......*.........%.......
|
|
||||||
......641...-..679.541..56.606...............490*.............871..........757.................612........64.....256...*...281..............
|
|
||||||
...........419...*......*..*..........................................*705...-.......................*.........@.......121..................
|
|
||||||
.......*.......786....738...28.........83.550........980...........915...............475..@.........524...=..145.349.................331....
|
|
||||||
....215.922.......................766.%....*.............993.................-.968..*.....301............205............................*...
|
|
||||||
............775..............273..*.........168.....504...$.....179.....+..466...*....352........850................771-.........-.......729
|
|
||||||
..699@........................*..174...............#........573*.......191.......567..*....925*....$......825..................543....&.....
|
|
||||||
........#....................939.......137...#..........474......../.................975........$.....219*............935............94.....
|
|
||||||
.....508...547....+.455..347.....593....&...56...140.......*246.198.....@....41...........46.476...........539...........+.15...............
|
|
||||||
...............210..........-.......*...............*773.............336....*..............+..........847.....*.............*.....957.......
|
|
||||||
...435...........................736...987.....573........32*............135.....60*405.........802*6...$......252....@...208.129*..........
|
|
||||||
.............187....353...............*..........*..504......276.........................#.........................838.............604......
|
|
||||||
..202...79....$........*..82-.............../.580......*..............181.......288......925.939....292......158..................*.........
|
|
||||||
.....................746.................381.......211..272...........*.....890....*215.........=...........*............953....22..........
|
|
||||||
.........................957......#..................*......../.......240....*............18.................294...766...............*......
|
|
||||||
.......483..434$........*.........184......#.....&..561.......354.........445....*626.......*174......................*54....603..432.457...
|
|
||||||
......#..........916.231..*178..........967...902........895..................135.............................207...............*...........
|
|
||||||
..941.....600...*...................31...............*.../............138*559.......19.803..........396.........&..377.....261...904........
|
|
||||||
...........*...146....#........626....*............809......199................@........*..............+.....#.......*.275*............%....
|
|
||||||
...........181.......459.486.....*.....912....787..........*..................847.....160.433.728.........901......404................899...
|
|
||||||
..........................*.....97...........*..............252..............................*.......+.........451........434..*............
|
|
||||||
...621...294..10.........913..........5..318.349.......749.......457......594........................326.........*..........%.884........494
|
|
||||||
.....*...*...-.........................$...........157.@........*..........*....&..951....524..869#..............405.................416....
|
|
||||||
....864.631....=......958..........................*....../.....953.......690..144..$.......*..........................204.&....%.......+...
|
|
||||||
750.............868....*.....430............882...98.......655..........................98.961...674........*.........*....955..530.........
|
|
||||||
...*..................459.......*929..........$...................281....-...............*......*.....38...688........878...................
|
|
||||||
.779....494.................+........-725............978..............134......2.......465....685.929*............759............351..291...
|
|
||||||
.....51*.......244....173..222..233........153*......*....123..............*.....324......................#.456.#...%...............-..@....
|
|
||||||
.........&....*.......$.............................542...*......77.......288...*....*51.......624......68....*.888....507..................
|
|
||||||
...213.367.953................................-667.......225.......*285.......506.177....*32.....*..........43........../...................
|
|
||||||
...#.................753.%........506.................................................131.....793...-.....-....................917$.147.....
|
|
||||||
.........275.............372.....$..........$........679.407..674..205#....406............647.....297..642.........621...............*......
|
|
||||||
.........*...................................173........*.....................*721........*.......................*....#207..........533....
|
|
||||||
.........68..............&.........274............322*...................707............280...............188..272..+...........223.........
|
|
||||||
.....553..................289.534.....%...830.........927..................*.176............*.............*..........217.........*..........
|
|
||||||
...................$..-.......-..........*................796*..........606...%...756...-.994....252.694..351.793$........930..230.....611..
|
|
||||||
.841%..659.237..356...116.............683...860...............494................$....127..............*....................*.........*.....
|
|
||||||
......#....................558..#891........*........418............&.............................22..323.....540...92....792.........408...
|
|
||||||
.................231*780....*.........*452.82........*........940..709....................662....*..............*...@...........432.........
|
|
||||||
....456.91...............972.....=.523.............949.837#........................823....*....627..............880....511.............203..
|
|
||||||
...$......@.........190.......162........508..........................158..861..........533.............749............................=....
|
|
||||||
......................*.............117..@....608....967.10............../........................&........$....................800.........
|
|
||||||
..752/...988.....82..281....894........*......-..................533.............692...............524.............=....623......@....939...
|
|
||||||
.........*...217*..............*.......51.........400...............&........200..@......435..484&........2*648.95..416.....................
|
|
||||||
.........319..................788....................*..279.......*.....................*...........321.........*..........464...286*....311
|
|
||||||
................733...457............*.............65...*.........620...205....457.782..513..342..........561.670.160..899.*.........607....
|
|
||||||
...............*.....+............397.....613/..........938.171...............*....................869......$.........*.....589.............
|
|
||||||
.............85..............=324.................189.........#.......808..578..-.........170*........*..&..........461.....................
|
|
||||||
......................992.............555........=...............375.....=.....599............336...835.282.....714.........................
|
|
||||||
...@......622.........%...............-....%....................*.....................705.287................../....955.426..140............
|
|
||||||
..202.....*...365.........$8....636*....148....................720............648......+...*................%........*...................220
|
|
||||||
.........223...+....................554........629...........*......785..............&.....460....*...#..848...24.948....961............*...
|
|
||||||
....................274..........................*...........995...........92.823.....958........185.983...............=..*....566....33....
|
|
||||||
.............146#....+....865.307................797...411................./..............577.......................245....935..............
|
|
||||||
....150...................*......*..560.................+...+298...............*....213.......984.......103.811.......................&.....
|
|
||||||
.......*..297..-905.822.643.....452....*333............................527..911.......*.........=.........*....*...............834.76.410...
|
|
||||||
..........+........../.......*...............774.....................................228..764-...........806..621.............*.....=.......
|
|
||||||
.......83.......753.......664.36..371..132.........341.......347...267........@813.............341*322..............237%.....134......$.....
|
|
||||||
....66*...630......*.238.............$.$......609.....*673.........*...920-...............%................937*873................844.807...
|
|
||||||
.........*......410.....#.........90..........%....-............789.........316.........755.932..$....................*921....363..=........
|
|
||||||
.........524........-.........52..........422...349......................15....*............*....550.........384..9.22........*.............
|
|
||||||
..............752....809..........$........*............#......397.......*..147....666.....83................*..............747......@474...
|
|
||||||
.......995......*............898.753.....305...........306......*......344............#....................985./................206=........
|
|
||||||
......*......172...............*...........................-..603..........*771...870...260....609..............588.....+503................
|
|
||||||
.....475.....................412.634*480..155....$824...845........409..367..........@..........*..498...............................893....
|
|
||||||
317......737.654.............................*97............781...*..............#......184..252..*....@.564*347......312*..............*...
|
|
||||||
............*.............658..........91.............402..*.....929.....346.....912......=......825.412.........908..........&..........500
|
|
||||||
.889..%.............471*...*...........*.........685-...+..208.........................*..................*291..*.............926.826.......
|
|
||||||
.......647...@..........84.929........518.375.........+.............%...............139................467......813.690...........*.........
|
|
||||||
...........276...................165.........%......778............412....594.619...............768........401..........195*861..70...$.....
|
|
||||||
......................1.....584.........+.......954...........299@..............*..............*......./../............................759..
|
|
||||||
........886..356.&784.......*........414.............................702.......128...+315.....302.....283....+.......309*231.......316......
|
|
||||||
..........$.............811....................*.............237....*.....................426.....713.......638.442............957*.........
|
|
||||||
....572...........491&..*......46............41.362.............*..361.700...883..478.......%.......*............./.....245.................
|
|
||||||
655*.......720...................*............................211.........*....*....*.........502.862.........*............+.......137......
|
|
||||||
.....302....*...............762..668.....446.............................552.....488.......................415.622.113.437........%.........
|
|
||||||
.190*.......362...581......#...............*...803.14.............&61.................$......972...479......................................
|
|
||||||
......760............=................478..627....*.......34*..........................51......=....%.....828............................531
|
|
||||||
........*.....764#...................*..............529............................356...........*.........-...................996*271..*...
|
|
||||||
220...58............721...508.872.634...........300*.........507.....290...........*..........356.780...36.....553.......................875
|
|
||||||
...............292.@......*............833.71...................*305.......382.....273................../.............998.........679.......
|
|
||||||
.........616..*......../..524.885.....-......*..527*538...794......../.....=....$........=....................812........*..........*.+297..
|
|
||||||
....579....*...629....666.................982...................467...468........292.....67........398...........*..283..225......550.......
|
|
||||||
241*.....415.....................832............................&...........&.................481..*.....444*826.....*......................
|
|
||||||
.....*..........................*.......$...920......600-..323#............930....591....357...*...653..............182.............816..575
|
|
||||||
..178.157...............186=..568.../...134....#....................292............*.....+....438..........+824...........859.814..#........
|
|
||||||
..........*209...462................606......%......405............*........*..334.623................*...................+...@.............
|
|
||||||
.......784......*............................632...@.......*....597........320.........219............210...........805.....................
|
|
||||||
595............664............&..%847.................$.749.434.....*894..........757...*..................$813......*.......&.%....278.191.
|
|
||||||
............................311.....................156.........244...............*....163....$...................706.....893..115...*......
|
|
||||||
.......451-...215...474.............610.687......................*..............535........424..........727*411.....................314.....
|
|
||||||
..............*................82.....*./....-.....482*307.....502.......595...................490..............590....*406.............309.
|
|
||||||
.............97.............................860...................................................*.......395...#...656.....................
|
|
||||||
......732........516......486......662*............*739...........=....579.............754......821..718...&..............609...753..103.62.
|
|
||||||
.908...*...........*.........*...........*824...197.....$......227...........956............331.......*......390.................*....*.....
|
|
||||||
....*...210......284.........821......148................644....................*....533.......*.....280....*.......-..........904.....748..
|
|
||||||
....368.......$.........542....................................*.411...........982...*....958.671........975.........610....................
|
|
||||||
...........820...358$....&..298........$....62...@....983..+..49.....254............784...*......................947.....................776
|
|
||||||
.....766.......................*......68.....#..477...$...428....209*.......*...265.......191......*........268...=....81...618#........=...
|
|
||||||
........*847.....661.474......624.........................................223...#....*...........74...................*.....................
|
|
||||||
.................*......%.574.....................976.475*618.@.....=..27.........975.280.....*......=...............13.....757-.......112..
|
|
||||||
.....721.........748.........*......392&.........../..........636.911......................359.......857.656....72-.....................*...
|
|
||||||
........*...................405..40...........87+..............................%89..............237.........#...................83....632...
|
|
||||||
........108...............................................172.54*.................................&...................%........*............
|
|
||||||
............736........3....727.....=.40...................*.........877.........=191.......................+...139.903........151.....%....
|
|
||||||
...725...56..%..410...$..@.....*...2...*....969.............207...../.......831........977.........158....946...*...................967.....
|
|
||||||
.....-....*......+......346..163......8........*..........@..................*.....509*.....458...*..............834...636@.....304......357
|
|
||||||
...-.......790......308........................453......396...........871..712..*.......625...%.426......777.423............................
|
|
||||||
..810.................%....355.421.....36.........................151...........935.774*........................#.......619.390.............
|
|
||||||
.............360*201..........*.......%...794...-..70......./24..*..........................765......*303.167.........*..&....-.........*...
|
|
||||||
.........*.......................273......%...198..*..............967................724=...*.....776........@.120.696......=.........37.301
|
|
||||||
......333.588.........205..........................572......................................673.........876..................893............
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
467..114..
|
|
||||||
...*......
|
|
||||||
..35..633.
|
|
||||||
......#...
|
|
||||||
617*......
|
|
||||||
.....+.58.
|
|
||||||
..592.....
|
|
||||||
......755.
|
|
||||||
...$.*....
|
|
||||||
.664.598..
|
|
||||||
42
template.go
Normal file
42
template.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import()
|
||||||
|
|
||||||
|
// Parallel code, global vars
|
||||||
|
type Nodes struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
variable type
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x...)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
// use defer timer("funcname")() when the function you want to
|
||||||
|
// test starts
|
||||||
|
func timer(name string) func() {
|
||||||
|
start := time.Now()
|
||||||
|
return func() {
|
||||||
|
fmt.Printf("%s took %v\n", name, time.Since(start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("./inputs")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
lines := make([]string, 0)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user