From 4a9afc8de6351ce1c890b7e1f2cec9201f5ed607 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Thu, 23 Dec 2021 20:31:40 -0700 Subject: [PATCH] day 22 part 1 is tractable; part 2 is going to need a rethink --- 22/main.go | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 22/main.go diff --git a/22/main.go b/22/main.go new file mode 100644 index 0000000..76018dc --- /dev/null +++ b/22/main.go @@ -0,0 +1,134 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "time" +) + +func mustAtoi(line string) int { + i, _ := strconv.Atoi(line) + return i +} + +func main() { + start := time.Now() + partOne() + duration := time.Since(start) + partTwo() + duration2 := time.Since(start) + fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) +} + +func makeScanner(test bool) *bufio.Scanner { + var f *os.File + if test { + f, _ = os.Open("inputs/testinput") + } else { + f, _ = os.Open("inputs/input") + } + reader := bufio.NewReader(f) + return bufio.NewScanner(reader) +} + +type step struct { + on bool + x []int + y []int + z []int +} + +func ParseInput(line string) step { + s := step{} + var state byte + acc := "" + for i := 0; i < len(line); i++ { + switch line[i] { + case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-': + acc += string(line[i]) + case 'f': + s.on = false + case 'n': + s.on = true + case 'x', 'y', 'z': + state = line[i] + case '.', ',': + if line[i] == '.' { + i++ // skip second . + } + switch state { + case 'x': + s.x = append(s.x, mustAtoi(acc)) + case 'y': + s.y = append(s.y, mustAtoi(acc)) + case 'z': + s.z = append(s.z, mustAtoi(acc)) + } + acc = "" + default: // all other characters do nothing + } + } + s.z = append(s.z, mustAtoi(acc)) // catch last number + return s +} + +type coord struct { + x int + y int + z int +} + +func GenerateCoords(s step) []coord { + c := []coord{} + for x := s.x[0]; x <= s.x[1]; x++ { + for y := s.y[0]; y <= s.y[1]; y++ { + for z := s.z[0]; z <= s.z[1]; z++ { + c = append(c, coord{x, y, z}) + } + } + } + return c +} + +func partOne() { + scanner := makeScanner(true) + + grid := map[coord]struct{}{} + steps := []step{} + for scanner.Scan() { + steps = append(steps, ParseInput(scanner.Text())) + } + for _, step := range steps[0:20] { + for _, coord := range GenerateCoords(step) { + if step.on { + grid[coord] = struct{}{} + } else { + delete(grid, coord) + } + } + } + fmt.Println(len(grid)) +} + +func partTwo() { + scanner := makeScanner(true) + + grid := map[coord]struct{}{} + steps := []step{} + for scanner.Scan() { + steps = append(steps, ParseInput(scanner.Text())) + } + // this ain't gonna work + for _, step := range steps { + for _, coord := range GenerateCoords(step) { + if step.on { + grid[coord] = struct{}{} + } else { + delete(grid, coord) + } + } + } + fmt.Println(len(grid)) +}