day 22 part 1 is tractable; part 2 is going to need a rethink

This commit is contained in:
David 2021-12-23 20:31:40 -07:00
parent 711badad96
commit 4a9afc8de6
1 changed files with 134 additions and 0 deletions

134
22/main.go Normal file
View File

@ -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))
}