From 574fdf38a402d1732f2074558880d5302b85d18c Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Fri, 8 Dec 2023 23:46:47 +0100 Subject: [PATCH] Non-working, archival purpose --- day07/cards.go | 2 +- day08/charpath.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++ day08/inputs | 1 + 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 day08/charpath.go create mode 120000 day08/inputs diff --git a/day07/cards.go b/day07/cards.go index 4cd409c..67ce34b 100644 --- a/day07/cards.go +++ b/day07/cards.go @@ -259,7 +259,7 @@ func quicksort(a, h []int, hType int) []int { func main() { - file, err := os.Open("./inputs/day7_try") + file, err := os.Open("./inputs/day07_input") check(err) defer file.Close() diff --git a/day08/charpath.go b/day08/charpath.go new file mode 100644 index 0000000..c30a8c2 --- /dev/null +++ b/day08/charpath.go @@ -0,0 +1,95 @@ +package main + +import( + "fmt" + "bufio" + "os" + "sync" + "regexp" + "context" +) + +const LEFT = 'L' + +type Nodes struct { + mu sync.Mutex + commands []int32 + singleN []int32 + leftN []int32 + rightN []int32 + steps uint64 +} + +func check(e error) { + if e != nil { + panic(e) + } +} + +func PrintAndWait(x ...any) { + fmt.Print(x...) + fmt.Scanln() +} + +func (n *Nodes) toByteSingle(s string) { + // I've just received something like AAA + var temp int32 + for i := 0; i < len(s); i++ { + a := int32(s[i]) + temp += a << (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 := 0; i < len(s); i++ { + tempL += int32(s[i]) << (i*8) + tempR += int32(s[i]) << (i*8) + } + n.leftN = append(n.leftN, tempL) + n.rightN = append(n.rightN, tempR) +} + +func (n *Nodes) findNext(direction int32, index int, ctx context.Context) { + +} + +func main() { + file, err := os.Open("./inputs/day08_test_input") + check(err) + defer file.Close() + // Struct with my node + n := Nodes{} + // Prepare the regex + repath := regexp.MustCompile("([A-Z]{3})") + + 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]) + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + // We start from 0, we find the match + for i, j := 0, 0; ; i++ { + // We do a circular loop + i = i % len(n.commands) + // A function that has the context as an argument + n.findNext(int32(i), j, ctx) + j++ + } +} diff --git a/day08/inputs b/day08/inputs new file mode 120000 index 0000000..a80bb2b --- /dev/null +++ b/day08/inputs @@ -0,0 +1 @@ +../inputs \ No newline at end of file