Compare commits
11 Commits
574fdf38a4
...
day10
| Author | SHA1 | Date | |
|---|---|---|---|
| e0e4bbc958 | |||
| 5ca089afb4 | |||
| fb896a8d20 | |||
| 5bebd71c4e | |||
| 6d24500c69 | |||
| d17308873e | |||
| c6150871a3 | |||
| ec4a4243ca | |||
| 64de63ddbe | |||
| a17153662e | |||
| 852a805114 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,3 +22,4 @@ go.work
|
|||||||
|
|
||||||
# Problem data
|
# Problem data
|
||||||
input*.txt
|
input*.txt
|
||||||
|
inputs/
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ An attempt to start programming again.
|
|||||||
- [Day 5](./day05/seeds.go)
|
- [Day 5](./day05/seeds.go)
|
||||||
- [Day 6](./day06/race.go)
|
- [Day 6](./day06/race.go)
|
||||||
- [Day 7](./day07/cards.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,12 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"time"
|
||||||
"regexp"
|
"regexp"
|
||||||
"context"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const LEFT = 'L'
|
const LEFT = 'L'
|
||||||
@@ -17,7 +17,9 @@ type Nodes struct {
|
|||||||
singleN []int32
|
singleN []int32
|
||||||
leftN []int32
|
leftN []int32
|
||||||
rightN []int32
|
rightN []int32
|
||||||
|
index int
|
||||||
steps uint64
|
steps uint64
|
||||||
|
allSteps []int
|
||||||
}
|
}
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
@@ -31,12 +33,34 @@ func PrintAndWait(x ...any) {
|
|||||||
fmt.Scanln()
|
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) {
|
func (n *Nodes) toByteSingle(s string) {
|
||||||
// I've just received something like AAA
|
// I've just received something like AAA
|
||||||
var temp int32
|
var temp int32
|
||||||
for i := 0; i < len(s); i++ {
|
for i := len(s) - 1; i >= 0; i-- {
|
||||||
a := int32(s[i])
|
a := int32(s[i])
|
||||||
temp += a << (i*8)
|
temp += a << ((len(s) - 1 - i) * 8)
|
||||||
}
|
}
|
||||||
n.singleN = append(n.singleN, temp)
|
n.singleN = append(n.singleN, temp)
|
||||||
}
|
}
|
||||||
@@ -44,26 +68,85 @@ func (n *Nodes) toByteSingle(s string) {
|
|||||||
func (n *Nodes) toByteDuet(s, r string) {
|
func (n *Nodes) toByteDuet(s, r string) {
|
||||||
// I've just received something like AAA BBB
|
// I've just received something like AAA BBB
|
||||||
var tempL, tempR int32
|
var tempL, tempR int32
|
||||||
for i := 0; i < len(s); i++ {
|
for i := len(s) - 1; i >= 0; i-- {
|
||||||
tempL += int32(s[i]) << (i*8)
|
tempL += int32(s[i]) << ((len(s) - 1 - i) * 8)
|
||||||
tempR += int32(s[i]) << (i*8)
|
tempR += int32(r[i]) << ((len(s) - 1 - i) * 8)
|
||||||
}
|
}
|
||||||
n.leftN = append(n.leftN, tempL)
|
n.leftN = append(n.leftN, tempL)
|
||||||
n.rightN = append(n.rightN, tempR)
|
n.rightN = append(n.rightN, tempR)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nodes) findNext(direction int32, index int, ctx context.Context) {
|
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() {
|
func main() {
|
||||||
file, err := os.Open("./inputs/day08_test_input")
|
defer timer ("main")()
|
||||||
|
file, err := os.Open("./inputs/day08_input")
|
||||||
check(err)
|
check(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
// Struct with my node
|
// Struct with my node
|
||||||
n := Nodes{}
|
n := Nodes{}
|
||||||
// Prepare the regex
|
// Prepare the regex
|
||||||
repath := regexp.MustCompile("([A-Z]{3})")
|
repath := regexp.MustCompile("([A-Z]{3})")
|
||||||
|
// Build the END
|
||||||
|
var END = 'Z'
|
||||||
|
END += ('Z' << 8)
|
||||||
|
END += ('Z' << 16)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
@@ -82,14 +165,69 @@ func main() {
|
|||||||
n.toByteSingle(tempNodes[0])
|
n.toByteSingle(tempNodes[0])
|
||||||
n.toByteDuet(tempNodes[1], tempNodes[2])
|
n.toByteDuet(tempNodes[1], tempNodes[2])
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
// We start from 0, we find the match
|
// We start from 0, we find the match
|
||||||
for i, j := 0, 0; ; i++ {
|
// Let's start an infinite loop
|
||||||
// We do a circular 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)
|
i = i % len(n.commands)
|
||||||
// A function that has the context as an argument
|
// By default, we will assume we are on the right
|
||||||
n.findNext(int32(i), j, ctx)
|
matching = n.rightN[n.index]
|
||||||
j++
|
//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
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)
|
||||||
|
}
|
||||||
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