From 06a83cd63b948f36b537e3ee369d8c2571f3ff8e Mon Sep 17 00:00:00 2001 From: Alexander Avery Date: Sun, 10 Dec 2023 14:16:54 -0500 Subject: [PATCH] begin day 10 --- day10.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + main_test.go | 13 ++++++-- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 day10.go diff --git a/day10.go b/day10.go new file mode 100644 index 0000000..cf4a005 --- /dev/null +++ b/day10.go @@ -0,0 +1,92 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "io" + "log" + "unicode" +) + +type groundpipe struct { + value byte // value of the pipe + + // byte slices should be sorted to allow for easier comparison + north []byte // valid byte set for a pipe to the north + south []byte // valid byte set for a pipe to the south + east []byte // valid byte set for a pipe to the east + west []byte // valid byte set for a pipe to the west +} + +type day10 struct{} + +func scanmaze(r io.Reader) ([][]groundpipe, int, int) { + layers := make([][]groundpipe, 0) + row, col := 0, 0 + buf := bufio.NewReader(r) + + for next := true; next; { + bts, err := buf.ReadBytes('\n') + next = !reachedeof(err) + + layers = append(layers, make([]groundpipe, 0)) + end := len(layers) - 1 + + for i, b := range bytes.TrimFunc(bts, unicode.IsSpace) { + pipe := groundpipe{value: b} + switch b { + case '|': + pipe.north = []byte("7F|") + pipe.south = []byte("JL|") + // pipe.east = []byte("") + // pipe.west = []byte("") + case '-': + // pipe.north = []byte("") + // pipe.south = []byte("") + pipe.east = []byte("-7J") + pipe.west = []byte("-FL") + case 'L': + pipe.north = []byte("7F|") + // pipe.south = []byte("") + pipe.east = []byte("-7J") + // pipe.west = []byte("") + case 'J': + pipe.north = []byte("7F|") + // pipe.south = []byte("") + // pipe.east = []byte("") + pipe.west = []byte("-FL") + case '7': + // pipe.north = []byte("") + pipe.south = []byte("JL|") + // pipe.east = []byte("") + pipe.west = []byte("-FL") + case 'F': + // pipe.north = []byte("") + pipe.south = []byte("JL|") + pipe.east = []byte("-7J") + // pipe.west = []byte("") + case 'S': + row, col = end, i + default: + // ignoring '.' and '\n' + continue + } + layers[end] = append(layers[end], pipe) + } + } + + return layers, row, col +} + +func (d day10) solve1(r io.Reader) string { + _, startx, starty := scanmaze(r) + log.Printf("x: %d; y: %d", startx, starty) + farthest := 0 + + return fmt.Sprintf("%d", farthest) +} + +func (d day10) solve2(r io.Reader) string { + return "incomplete" +} diff --git a/main.go b/main.go index 4178bb2..fae2481 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ var ( &day07{}, &day08{}, &day09{}, + &day10{}, } ) diff --git a/main_test.go b/main_test.go index e950c5d..509a5a2 100644 --- a/main_test.go +++ b/main_test.go @@ -163,10 +163,19 @@ ZZZ = (ZZZ, ZZZ)`, { // day 9 inp1: `0 3 6 9 12 15 -1 3 6 10 15 21 -10 13 16 21 30 45`, + 1 3 6 10 15 21 + 10 13 16 21 30 45`, ans1: "114", ans2: "2", }, + { + // day 10 + inp1: `..F7. + .FJ|. + SJ.L7 + |F--J + LJ...`, + ans1: "8", + }, } )