Working d8p1
This commit is contained in:
parent
574fdf38a4
commit
a17153662e
1
.gitignore
vendored
1
.gitignore
vendored
@ -22,3 +22,4 @@ go.work
|
|||||||
|
|
||||||
# Problem data
|
# Problem data
|
||||||
input*.txt
|
input*.txt
|
||||||
|
inputs/
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"fmt"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"context"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const LEFT = 'L'
|
const LEFT = 'L'
|
||||||
|
|
||||||
type Nodes struct {
|
type Nodes struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
commands []int32
|
commands []int32
|
||||||
singleN []int32
|
singleN []int32
|
||||||
leftN []int32
|
leftN []int32
|
||||||
rightN []int32
|
rightN []int32
|
||||||
steps uint64
|
index int
|
||||||
|
steps uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
@ -32,64 +32,108 @@ func PrintAndWait(x ...any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
//fmt.Printf("Received %c (%b), now I have %b | ", s[i], s[i], tempL)
|
||||||
n.leftN = append(n.leftN, tempL)
|
//fmt.Printf("Received %c (%b), now I have %b\n", r[i], r[i], tempR)
|
||||||
n.rightN = append(n.rightN, tempR)
|
//fmt.Scanln()
|
||||||
|
}
|
||||||
|
n.leftN = append(n.leftN, tempL)
|
||||||
|
n.rightN = append(n.rightN, tempR)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nodes) findNext(direction int32, index int, ctx context.Context) {
|
func (n *Nodes) findNext(myN int32) {
|
||||||
|
//var wg sync.WaitGroup
|
||||||
|
for i := 0; i < len(n.singleN); i++ {
|
||||||
|
if myN^n.singleN[i] == 0 {
|
||||||
|
n.index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file, err := os.Open("./inputs/day08_test_input")
|
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()
|
||||||
// First line, RL commands
|
// First line, RL commands
|
||||||
strCommands := scanner.Text()
|
strCommands := scanner.Text()
|
||||||
// Get every char inside the string just obtained
|
// Get every char inside the string just obtained
|
||||||
for i := 0; i < len(strCommands); i++ {
|
for i := 0; i < len(strCommands); i++ {
|
||||||
n.commands = append(n.commands, int32(strCommands[i]))
|
n.commands = append(n.commands, int32(strCommands[i]))
|
||||||
}
|
}
|
||||||
// One empty line
|
// One empty line
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
// X = (Y, Z)
|
// X = (Y, Z)
|
||||||
// We regex this one
|
// We regex this one
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
tempNodes := repath.FindAllString(scanner.Text(), -1)
|
tempNodes := repath.FindAllString(scanner.Text(), -1)
|
||||||
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 = i % len(n.commands)
|
i := 0
|
||||||
// A function that has the context as an argument
|
// We start from the AAA element
|
||||||
n.findNext(int32(i), j, ctx)
|
START := 'A'
|
||||||
j++
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user