~williewillus/advent_of_code

81b332682bdf5fa65797143ea2c0c213b23b2cee — Vincent Lee 1 year, 5 months ago 076c8db
format, use util vec2 in netbook days
M aoc_2022/day13/day13.go => aoc_2022/day13/day13.go +7 -5
@@ 8,6 8,7 @@ import (
)

type compareResucmp int

const (
	rightOrder = iota
	wrongOrder


@@ 45,7 46,8 @@ func (l lst) cmp(other datum) compareResucmp {
			}
		}
		panic("unreachable")
	case num: return l.cmp(lst { []datum{other} })
	case num:
		return l.cmp(lst{[]datum{other}})
	default:
		panic("unknown type")
	}


@@ 54,7 56,7 @@ func (l lst) cmp(other datum) compareResucmp {
func (n num) cmp(other datum) compareResucmp {
	switch other.(type) {
	case lst:
		l := lst { []datum{n} }
		l := lst{[]datum{n}}
		return l.cmp(other)
	case num:
		otherValue := other.(num)


@@ 94,7 96,7 @@ func parseList(s string) (lst, int) {
		}
	}

	return lst{resucmp}, consumed+1
	return lst{resucmp}, consumed + 1
}

func parseNum(s string) (num, int) {


@@ 152,7 154,7 @@ func Run() (string, string) {
	p1 := 0
	pairnum := 1
	allDatums := []datum{}
	

	for scanner.Scan() {
		line := scanner.Text()
		if line == "" {


@@ 174,7 176,7 @@ func Run() (string, string) {
	}

	allDatums = append(allDatums, parseFullDatum("[[2]]"), parseFullDatum("[[6]]"))
	sort.Slice(allDatums, func (i, j int) bool {
	sort.Slice(allDatums, func(i, j int) bool {
		return allDatums[i].cmp(allDatums[j]) == rightOrder
	})


M aoc_2022/day14/day14.go => aoc_2022/day14/day14.go +12 -12
@@ 7,6 7,7 @@ import (
	"os"
	"strconv"
	"strings"
	"williewillus/aoc_2022/util"
)

// cargo culted from container/heap doc


@@ 30,11 31,10 @@ func (h *IntHeap) Pop() interface{} {
	*h = old[0 : n-1]
	return x
}

// End cargo cult

type vec2 struct {
	x, y int
}
type vec2 util.Vec2

func parse(s string) (int, int) {
	pieces := strings.Split(s, ",")


@@ 66,16 66,16 @@ func Run() (string, string) {
		points := []vec2{}
		for _, piece := range strings.Split(line, " -> ") {
			x, y := parse(piece)
			points = append(points, vec2 { x, y})
			points = append(points, vec2{x, y})
		}
		

		// fill
		for i := 0; i < len(points) - 1; i++ {
		for i := 0; i < len(points)-1; i++ {
			from := points[i]
			to := points[i+1]
			startX, endX := iorder(from.x, to.x)
			startY, endY := iorder(from.y, to.y)
			
			startX, endX := iorder(from.X, to.X)
			startY, endY := iorder(from.Y, to.Y)

			for x := startX; x <= endX; x++ {
				for y := startY; y <= endY; y++ {
					h, ok := state[x]


@@ 90,9 90,9 @@ func Run() (string, string) {
	}

	/*
	for x, ys := range state {
		fmt.Printf("top at x=%d is y=%d\n", x, (*ys)[0])
	}
		for x, ys := range state {
			fmt.Printf("top at x=%d is y=%d\n", x, (*ys)[0])
		}
	*/

	return "incomplete", "incomplete"

M aoc_2022/day18/day18.go => aoc_2022/day18/day18.go +13 -13
@@ 18,14 18,14 @@ func expect(e error) {
	}
}

var faces [6]vec3 = [6]vec3 {
var faces [6]vec3 = [6]vec3{
	{-1, 0, 0},
		{1, 0, 0},
		{0, -1, 0},
		{0, 1, 0},
		{0, 0, -1},
		{0, 0, 1},
	}
	{1, 0, 0},
	{0, -1, 0},
	{0, 1, 0},
	{0, 0, -1},
	{0, 0, 1},
}

func flood(positions map[vec3]struct{}, mins, maxs vec3) map[vec3]struct{} {
	visited := make(map[vec3]struct{})


@@ 34,13 34,13 @@ func flood(positions map[vec3]struct{}, mins, maxs vec3) map[vec3]struct{} {
		cur := toVisit[len(toVisit)-1]
		toVisit = toVisit[:len(toVisit)-1]
		visited[cur] = struct{}{}
		

		for _, offset := range faces {
			neighbor := vec3{cur.x + offset.x, cur.y + offset.y, cur.z + offset.z}
			_, blocked := positions[neighbor]
			_, alreadyVisited := visited[neighbor]
			inBounds := neighbor.x >= mins.x && neighbor.x <= maxs.x && neighbor.y >= mins.y && neighbor.y <= maxs.y && neighbor.z >= mins.z && neighbor.z <= maxs.z
			

			if !blocked && !alreadyVisited && inBounds {
				toVisit = append(toVisit, neighbor)
			}


@@ 54,7 54,7 @@ func Run() (string, string) {
	positions := make(map[vec3]struct{})
	mins := vec3{math.MaxInt32, math.MaxInt32, math.MaxInt32}
	maxs := vec3{math.MinInt32, math.MinInt32, math.MinInt32}
	

	for scanner.Scan() {
		pieces := strings.Split(scanner.Text(), ",")
		x, err := strconv.Atoi(pieces[0])


@@ 82,8 82,8 @@ func Run() (string, string) {
		if z > maxs.z {
			maxs.z = z
		}
		
		v := vec3 {x, y, z}

		v := vec3{x, y, z}
		positions[v] = struct{}{}
	}



@@ 105,7 105,7 @@ func Run() (string, string) {
	maxs.x++
	maxs.y++
	maxs.z++
	

	visited := flood(positions, mins, maxs)
	p2 := 0
	for p := range visited {

M aoc_2022/day21/day21.go => aoc_2022/day21/day21.go +13 -8
@@ 24,11 24,11 @@ func Run() (string, string) {
		line := scanner.Text()
		splits := strings.Split(line, ": ")
		emitter := splits[0]
		

		immediate, err := strconv.Atoi(splits[1])
		if err == nil {
			ch := getOrMake(chans, emitter)
			ch<- int64(immediate)
			ch <- int64(immediate)
		} else {
			parts := strings.Split(splits[1], " ")
			ch1 := getOrMake(chans, parts[0])


@@ 40,13 40,18 @@ func Run() (string, string) {
				b := <-ch2
				var result int64
				switch op {
				case "+": result = a + b
				case "-": result = a - b
				case "*": result = a * b
				case "/": result = a / b
				default: panic("idk")
				case "+":
					result = a + b
				case "-":
					result = a - b
				case "*":
					result = a * b
				case "/":
					result = a / b
				default:
					panic("idk")
				}
				dest<- result
				dest <- result
			}()
		}
	}

M aoc_2022/day23/day23.go => aoc_2022/day23/day23.go +20 -21
@@ 5,20 5,19 @@ import (
	"math"
	"os"
	"strconv"
	"williewillus/aoc_2022/util"
)

type vec2 struct {
	x, y int
}
type vec2 util.Vec2

var north vec2 = vec2{0, -1}
var south vec2 = vec2{0, 1}
var west vec2 = vec2{-1, 0}
var east vec2 = vec2{1, 0}
var northeast vec2 = vec2{north.x + east.x, north.y + east.y}
var northwest vec2 = vec2{north.x + west.x, north.y + west.y}
var southeast vec2 = vec2{south.x + east.x, south.y + east.y}
var southwest vec2 = vec2{south.x + west.x, south.y + west.y}
var northeast vec2 = vec2{north.X + east.X, north.Y + east.Y}
var northwest vec2 = vec2{north.X + west.X, north.Y + west.Y}
var southeast vec2 = vec2{south.X + east.X, south.Y + east.Y}
var southwest vec2 = vec2{south.X + west.X, south.Y + west.Y}

var eightdir [8]vec2 = [8]vec2{
	north, south, west, east, northeast, northwest, southeast, southwest,


@@ 28,17 27,17 @@ func bounds(elves map[vec2]struct{}) (vec2, vec2) {
	mins := vec2{math.MaxInt32, math.MaxInt32}
	maxs := vec2{math.MinInt32, math.MinInt32}
	for elf := range elves {
		if elf.x < mins.x {
			mins.x = elf.x
		if elf.X < mins.X {
			mins.X = elf.X
		}
		if elf.y < mins.y {
			mins.y = elf.y
		if elf.Y < mins.Y {
			mins.Y = elf.Y
		}
		if elf.x > maxs.x {
			maxs.x = elf.x
		if elf.X > maxs.X {
			maxs.X = elf.X
		}
		if elf.y > maxs.y {
			maxs.y = elf.y
		if elf.Y > maxs.Y {
			maxs.Y = elf.Y
		}
	}
	return mins, maxs


@@ 46,8 45,8 @@ func bounds(elves map[vec2]struct{}) (vec2, vec2) {

func show(elves map[vec2]struct{}) {
	mins, maxs := bounds(elves)
	for y := mins.y; y <= maxs.y; y++ {
		for x := mins.x; x <= maxs.x; x++ {
	for y := mins.Y; y <= maxs.Y; y++ {
		for x := mins.X; x <= maxs.X; x++ {
			pos := vec2{x, y}
			_, ok := elves[pos]
			if ok {


@@ 61,7 60,7 @@ func show(elves map[vec2]struct{}) {
}

func add(a, b vec2) vec2 {
	return vec2{a.x + b.x, a.y + b.y}
	return vec2{a.X + b.X, a.Y + b.Y}
}

func notIn(set map[vec2]struct{}, v vec2) bool {


@@ 153,8 152,8 @@ func Run() (string, string) {
	}

	mins, maxs := bounds(elves)
	width := maxs.x - mins.x + 1
	height := maxs.y - mins.y + 1
	width := maxs.X - mins.X + 1
	height := maxs.Y - mins.Y + 1

	for {
		if !step(elves, &proposalOrder) {


@@ 162,5 161,5 @@ func Run() (string, string) {
		}
		round++
	}
	return strconv.Itoa(width*height-len(elves)), strconv.Itoa(round)
	return strconv.Itoa(width*height - len(elves)), strconv.Itoa(round)
}

M aoc_2022/main.go => aoc_2022/main.go +8 -8
@@ 5,22 5,22 @@ import (
	"os"
	"strconv"
	"williewillus/aoc_2022/day1"
	"williewillus/aoc_2022/day2"
	"williewillus/aoc_2022/day3"
	"williewillus/aoc_2022/day4"
	"williewillus/aoc_2022/day5"
	"williewillus/aoc_2022/day6"
	"williewillus/aoc_2022/day7"
	"williewillus/aoc_2022/day8"
	"williewillus/aoc_2022/day9"
	"williewillus/aoc_2022/day10"
	"williewillus/aoc_2022/day11"
	"williewillus/aoc_2022/day12"
	"williewillus/aoc_2022/day13"
	"williewillus/aoc_2022/day14"
	"williewillus/aoc_2022/day18"
	"williewillus/aoc_2022/day2"
	"williewillus/aoc_2022/day21"
	"williewillus/aoc_2022/day23"
	"williewillus/aoc_2022/day3"
	"williewillus/aoc_2022/day4"
	"williewillus/aoc_2022/day5"
	"williewillus/aoc_2022/day6"
	"williewillus/aoc_2022/day7"
	"williewillus/aoc_2022/day8"
	"williewillus/aoc_2022/day9"
)

type day func() (string, string)