From 2bdb340f9126ae5922280a1e0978383c5a0bfa9b Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 13 Dec 2022 18:41:39 +0100 Subject: hashing --- day13.go | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day13.txt | 23 ++++++++++ hashbydata.go | 24 ++++++++++ 3 files changed, 186 insertions(+) create mode 100644 day13.go create mode 100644 day13.txt create mode 100644 hashbydata.go diff --git a/day13.go b/day13.go new file mode 100644 index 0000000..6d92ba5 --- /dev/null +++ b/day13.go @@ -0,0 +1,139 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" +) + +type ( + t interface{} + empty struct{} +) + +type packet struct { + data []t +} + +func intOrList(v t) int { + switch v.(type) { + case int: + return 0 + case []t: + return 1 + default: + return -1 + } +} + +type continuation struct { + i int + ok bool + a, b []t +} + +func comparePackets(p1, p2 *packet, comp func(a, b int) bool) (int, bool) { + var cp func(i int, ok bool, a, b []t) (int, bool) + + var ctn []continuation + cp = func(i int, ok bool, a, b []t) (int, bool) { + if !ok { + return i, false + } + if al, bl := i >= len(a), i >= len(b); al || bl { + if bl && !al { + return i, false + } + return i, ok + } + at, bt := intOrList(a), intOrList(b) + if at == 1 && bt == 1 { + } else if at == 1 && bt == 0 { + } else if at == 0 && bt == 1 { + } else if at == 0 && bt == 0 { + } else { + } + + return i, true + } + ctn = append(ctn, continuation{0, true, p1.data, p2.data}) + for len(ctn) > 0 { + } + return cp(0, true, p1.data, p2.data) +} + +func parsePacket(b []byte) *packet { + var parseList func(list []t, b []byte) []t + + parseList = func(list []t, b []byte) []t { + for i := 0; i < len(b); i++ { + switch { + case b[0] == '[' && b[1] == ']': + list = append(list, []t{}) + return list + case b[i] == '[': + j := 1 + subchildren := 1 + for subchildren > 0 { + if b[i+j] == '[' { + subchildren += 1 + } + if b[i+j] == ']' { + subchildren = subchildren - 1 + } + j++ + } + list = append(list, parseList([]t{}, b[i+1:i+j-1])) + i = i + j + default: + j := 0 + for ; i+j < len(b); j++ { + stop := false + switch b[i+j] { + case ']': + stop = true + case ',': + stop = true + } + if stop { + break + } + } + s := string(b[i : i+j]) + if len(s) == 0 { + s = string(b[len(b)-1]) + } + num, err := strconv.Atoi(s) + if err != nil { + log.Fatal("This is no a number!", i, j, s) + } + list = append(list, num) + i = i + j + } + } + return list + } + + return &packet{data: parseList([]t{}, b[1:len(b)-1])} +} + +func main() { + f, err := os.Open("day13.txt") + if err != nil { + log.Fatal("Could not read input file") + } + + s := bufio.NewScanner(f) + packets := []*packet{} + for s.Scan() { + if len(s.Bytes()) > 0 { + packets = append(packets, parsePacket(s.Bytes())) + } + } + for i := 0; i < len(packets); i += 2 { + fmt.Println(packets[i]) + fmt.Println(comparePackets(packets[i], packets[i+1], func(a, b int) bool { return a < b })) + } +} diff --git a/day13.txt b/day13.txt new file mode 100644 index 0000000..af73fbb --- /dev/null +++ b/day13.txt @@ -0,0 +1,23 @@ +[1,1,3,1,1] +[1,1,5,1,1] + +[[1],[2,3,4]] +[[1],4] + +[9] +[[8,7,6]] + +[[4,4],4,4] +[[4,4],4,4,4] + +[7,7,7,7] +[7,7,7] + +[] +[3] + +[[[]]] +[[]] + +[1,[2,[3,[4,[5,6,7]]]],8,9] +[1,[2,[3,[4,[5,6,0]]]],8,9] diff --git a/hashbydata.go b/hashbydata.go new file mode 100644 index 0000000..83874f3 --- /dev/null +++ b/hashbydata.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +type ( + empty struct{} + t interface{} + set map[t]empty +) + +func main() { + m := make(map[t]string) + fmt.Println(fmt.Sprintf("%+v", m)) + m[struct{ x string }{x: "hello there"}] = "hello there" + fmt.Println(fmt.Sprintf("%+v", m)) + + m[struct{ x string }{x: "hello there"}] = "goodbye" + fmt.Println(fmt.Sprintf("%+v", m)) + + ptr := &struct{ x string }{x: "hello there"} + pv := *ptr + m[pv] = "ptr" + fmt.Println(fmt.Sprintf("%+v", m)) +} -- cgit v1.2.3