From 11394225ead868425c29df7576cdee0c22d61c5b Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Thu, 19 Jan 2023 20:34:56 +0100 Subject: scratchdir --- day16.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ day16.txt | 46 ++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ hashbydata.go | 24 --------------------- radixsort.go | 48 ------------------------------------------ scratchdir/hashbydata.go | 24 +++++++++++++++++++++ scratchdir/heapsort.go | 37 ++++++++++++++++++++++++++++++++ scratchdir/insertionsort.go | 24 +++++++++++++++++++++ scratchdir/radixsort.go | 48 ++++++++++++++++++++++++++++++++++++++++++ scratchdir/scratch.fnl | 8 +++++++ scratchdir/selectionsort.go | 23 ++++++++++++++++++++ selectionsort.go | 23 -------------------- 12 files changed, 264 insertions(+), 95 deletions(-) create mode 100644 day16.go create mode 100644 day16.txt create mode 100644 go.mod delete mode 100644 hashbydata.go delete mode 100644 radixsort.go create mode 100644 scratchdir/hashbydata.go create mode 100644 scratchdir/heapsort.go create mode 100644 scratchdir/insertionsort.go create mode 100644 scratchdir/radixsort.go create mode 100644 scratchdir/scratch.fnl create mode 100644 scratchdir/selectionsort.go delete mode 100644 selectionsort.go diff --git a/day16.go b/day16.go new file mode 100644 index 0000000..0fedb24 --- /dev/null +++ b/day16.go @@ -0,0 +1,51 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "os" + "regexp" +) + +type Valve struct { + name string +} + +func readValves(f io.Reader) map[string]Valve { + pattern := regexp.MustCompile( + `Valve ([A-Z]+) has flow rate=(\d+); tunnels? leads? to valves? ([A-Z, ]+)`, + ) + + s := bufio.NewScanner(f) + for s.Scan() { + line := s.Text() + valveInfo := pattern.FindAllStringSubmatch(line, 3)[0] + fmt.Println(valveInfo) + } + return nil +} + +/* +AA,0 =================== +||.............\\ \\ +BB,13===CC,2===DD,20 II,0 +...............\\ \\ +...............EE,20 JJ,21 +...............\\ +...............FF,0 +...............\\ +...............GG,0 +...............\\ +...............HH,22 +Probably can do some apriori thing here +*/ +func main() { + fh, err := os.Open("day16.txt") + if err != nil { + log.Fatal("Input file not found") + } + valves := readValves(fh) + fmt.Println(valves) +} diff --git a/day16.txt b/day16.txt new file mode 100644 index 0000000..4d0dc5e --- /dev/null +++ b/day16.txt @@ -0,0 +1,46 @@ +Valve OJ has flow rate=0; tunnels lead to valves EW, IG +Valve BN has flow rate=0; tunnels lead to valves SA, AA +Valve SA has flow rate=5; tunnels lead to valves QK, LP, ZP, BN, VH +Valve RL has flow rate=21; tunnel leads to valve AM +Valve LR has flow rate=19; tunnel leads to valve XZ +Valve VQ has flow rate=0; tunnels lead to valves OW, IG +Valve ZK has flow rate=0; tunnels lead to valves EW, WC +Valve IG has flow rate=16; tunnels lead to valves OJ, VQ +Valve WC has flow rate=22; tunnels lead to valves VD, ZK +Valve EW has flow rate=18; tunnels lead to valves OJ, ZK +Valve FP has flow rate=8; tunnel leads to valve GB +Valve JF has flow rate=23; tunnel leads to valve VD +Valve BL has flow rate=0; tunnels lead to valves AA, ZD +Valve BZ has flow rate=0; tunnels lead to valves QK, JA +Valve KH has flow rate=0; tunnels lead to valves SJ, FC +Valve FU has flow rate=0; tunnels lead to valves FC, MH +Valve ZP has flow rate=0; tunnels lead to valves SA, FC +Valve DZ has flow rate=0; tunnels lead to valves AA, MH +Valve RI has flow rate=0; tunnels lead to valves LP, MH +Valve AE has flow rate=0; tunnels lead to valves FC, AA +Valve JA has flow rate=4; tunnels lead to valves MM, BZ, JR, ZI, QO +Valve XP has flow rate=0; tunnels lead to valves ZD, ZI +Valve GB has flow rate=0; tunnels lead to valves FP, SJ +Valve AM has flow rate=0; tunnels lead to valves ZD, RL +Valve MH has flow rate=3; tunnels lead to valves VJ, DZ, JR, FU, RI +Valve QK has flow rate=0; tunnels lead to valves BZ, SA +Valve AA has flow rate=0; tunnels lead to valves DZ, CZ, BL, AE, BN +Valve MJ has flow rate=0; tunnels lead to valves VN, VH +Valve QO has flow rate=0; tunnels lead to valves CZ, JA +Valve MM has flow rate=0; tunnels lead to valves FC, JA +Valve VN has flow rate=17; tunnels lead to valves FV, MJ +Valve OW has flow rate=0; tunnels lead to valves SJ, VQ +Valve ZI has flow rate=0; tunnels lead to valves XP, JA +Valve VJ has flow rate=0; tunnels lead to valves KJ, MH +Valve KQ has flow rate=0; tunnels lead to valves XZ, KJ +Valve FC has flow rate=12; tunnels lead to valves ZP, MM, KH, AE, FU +Valve LP has flow rate=0; tunnels lead to valves SA, RI +Valve VD has flow rate=0; tunnels lead to valves WC, JF +Valve JR has flow rate=0; tunnels lead to valves MH, JA +Valve VH has flow rate=0; tunnels lead to valves SA, MJ +Valve CZ has flow rate=0; tunnels lead to valves AA, QO +Valve SJ has flow rate=15; tunnels lead to valves KH, FV, GB, OW +Valve FV has flow rate=0; tunnels lead to valves VN, SJ +Valve XZ has flow rate=0; tunnels lead to valves LR, KQ +Valve KJ has flow rate=9; tunnels lead to valves KQ, VJ +Valve ZD has flow rate=13; tunnels lead to valves XP, BL, AM diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a34aec1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module mvinkio.online/aoc + +go 1.19 diff --git a/hashbydata.go b/hashbydata.go deleted file mode 100644 index 83874f3..0000000 --- a/hashbydata.go +++ /dev/null @@ -1,24 +0,0 @@ -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)) -} diff --git a/radixsort.go b/radixsort.go deleted file mode 100644 index 806a5f8..0000000 --- a/radixsort.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "fmt" - "math/rand" -) - -const ( - signedMin = -(1 << 31) - signedMax = (1 << 31) - 1 -) - -func countsort(in []int32, r uint32) { - count := make([]int32, r) - - for i := range in { - count[in[i]]++ - } - - for i := 1; i < len(in)-1; i++ { - count[i] += count[i-1] - } - - var pointer int32 - for i := 0; i < len(count)-1; i++ { - for count[i] > 0 && pointer < count[i] { - in[pointer] = int32(i) - pointer++ - } - } -} - -func radix() { -} - -func input(r *rand.Rand, n int) []int32 { - out := make([]int32, n) - for i := 0; i < n; i++ { - out[i] = int32(r.Intn(signedMax)) - } - return out -} - -func main() { - in := input(rand.New(rand.NewSource(1)), 1<<8) - countsort(in, signedMax) - fmt.Println(in) -} diff --git a/scratchdir/hashbydata.go b/scratchdir/hashbydata.go new file mode 100644 index 0000000..83874f3 --- /dev/null +++ b/scratchdir/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)) +} diff --git a/scratchdir/heapsort.go b/scratchdir/heapsort.go new file mode 100644 index 0000000..47552af --- /dev/null +++ b/scratchdir/heapsort.go @@ -0,0 +1,37 @@ +package main + +import ( + "container/heap" + "fmt" +) + +var sortingdata []int = []int{1, 23, 4, 234, 123, 4, 4512, 0} + +type myHeap []int + +func (h myHeap) Less(i, j int) bool { return h[i] > h[j] } +func (h myHeap) Len() int { return len(h) } +func (h myHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } + +func (h *myHeap) Pop() any { + popper := *h + popped := popper[len(*h)-1] + *h = popper[:len(*h)-1] + return popped +} + +func (h *myHeap) Push(x any) { + *h = append(*h, x.(int)) +} + +func main() { + orig := myHeap(sortingdata) + heap.Init(&orig) + + end := orig.Len() - 1 + h := orig + for h.Len() > 0 { + heap.Pop(&h) + } + fmt.Println(end, orig) +} diff --git a/scratchdir/insertionsort.go b/scratchdir/insertionsort.go new file mode 100644 index 0000000..cc7d2d6 --- /dev/null +++ b/scratchdir/insertionsort.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +var sortingdata []int = []int{1, 23, 4, 234, 123, 4, 4512, 0} + +func insert(in []int) { + for i := len(in) - 1; i > 0; i-- { + if in[i-1] >= in[i] { + in[i], in[i-1] = in[i-1], in[i] + } + } +} + +func insertionSort(in []int) []int { + for i := 0; i < len(in); i++ { + insert(in[:i+1]) + } + return in +} + +func main() { + fmt.Println(insertionSort(sortingdata)) +} diff --git a/scratchdir/radixsort.go b/scratchdir/radixsort.go new file mode 100644 index 0000000..806a5f8 --- /dev/null +++ b/scratchdir/radixsort.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "math/rand" +) + +const ( + signedMin = -(1 << 31) + signedMax = (1 << 31) - 1 +) + +func countsort(in []int32, r uint32) { + count := make([]int32, r) + + for i := range in { + count[in[i]]++ + } + + for i := 1; i < len(in)-1; i++ { + count[i] += count[i-1] + } + + var pointer int32 + for i := 0; i < len(count)-1; i++ { + for count[i] > 0 && pointer < count[i] { + in[pointer] = int32(i) + pointer++ + } + } +} + +func radix() { +} + +func input(r *rand.Rand, n int) []int32 { + out := make([]int32, n) + for i := 0; i < n; i++ { + out[i] = int32(r.Intn(signedMax)) + } + return out +} + +func main() { + in := input(rand.New(rand.NewSource(1)), 1<<8) + countsort(in, signedMax) + fmt.Println(in) +} diff --git a/scratchdir/scratch.fnl b/scratchdir/scratch.fnl new file mode 100644 index 0000000..ea6dc40 --- /dev/null +++ b/scratchdir/scratch.fnl @@ -0,0 +1,8 @@ +(vim.api.nvim_create_namespace :debug) +(vim.api.nvim_buf_clear_namespace 0 41 0 -1) + +(vim.api.nvim_buf_set_extmark 0 41 1 1 + {:id 4 + :virt_text_pos :overlay + :virt_lines [[["Hi there is this cool? Making a long string hereereeeeeeeeeeeeeeeeeeeeeeeeeee hidden messages should also be visible otherwise this is pretty useless" + :DiffText]]]}) diff --git a/scratchdir/selectionsort.go b/scratchdir/selectionsort.go new file mode 100644 index 0000000..67a33cf --- /dev/null +++ b/scratchdir/selectionsort.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +var sortingdata []int = []int{1, 23, 4, 234, 123, 4, 4512, 0} + +func selectionSort(in []int) []int { + end := len(in) + for i := 0; i < end; i++ { + min := i + for j := i; j < end; j++ { + if in[j] < in[min] { + min = j + } + } + in[i], in[min] = in[min], in[i] + } + return in +} + +func main() { + fmt.Println(selectionSort(sortingdata)) +} diff --git a/selectionsort.go b/selectionsort.go deleted file mode 100644 index 67a33cf..0000000 --- a/selectionsort.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import "fmt" - -var sortingdata []int = []int{1, 23, 4, 234, 123, 4, 4512, 0} - -func selectionSort(in []int) []int { - end := len(in) - for i := 0; i < end; i++ { - min := i - for j := i; j < end; j++ { - if in[j] < in[min] { - min = j - } - } - in[i], in[min] = in[min], in[i] - } - return in -} - -func main() { - fmt.Println(selectionSort(sortingdata)) -} -- cgit v1.2.3