diff --git a/2019/day18/main.go b/2019/day18/main.go index 4538ac8..e63bf99 100644 --- a/2019/day18/main.go +++ b/2019/day18/main.go @@ -1,23 +1,127 @@ package main import ( - "errors" + "fmt" helpers "git.bullercodeworks.com/brian/adventofcode/helpers" ) -var maxX, maxY int -var vault map[helpers.Coordinate]byte -var keys map[helpers.Coordinate]byte -var doors map[helpers.Coordinate]byte - func main() { file := "input" if helpers.GetArgNumber(1) != "" { file = helpers.GetArgNumber(1) } v := NewVault(file) - v.Print() + part1(v) +} + +var keylist int +var allNodes map[string]*Node + +func part1(v *Vault) { + keylist = v.keylist + allNodes = make(map[string]*Node) + for k, v := range v.vault { + if v { + x, y, z := unc(k) + n := Node{ + X: x, + Y: y, + Z: z, + steps: helpers.MAX_INT, + } + allNodes[c([]int{x, y})] = &n + } + } + for _, v := range allNodes { + n, ok := allNodes[c([]int{v.X, v.Y + 1})] + if ok { + v.N = n + } + e, ok := allNodes[c([]int{v.X + 1, v.Y})] + if ok { + v.E = e + } + s, ok := allNodes[c([]int{v.X, v.Y - 1})] + if ok { + v.S = s + } + w, ok := allNodes[c([]int{v.X - 1, v.Y})] + if ok { + v.W = w + } + } + start := allNodes[c([]int{v.start.X, v.start.Y})] + start.steps = 0 + ProcessNode(start, 0, 0) +} + +type Node struct { + X, Y, Z int + N, E, S, W *Node + steps int + visited bool +} + +func ProcessNode(n *Node, steps, keys int) { + if n.Z&keylist == keylist { + fmt.Println("Steps to all Keys:", steps) + return + } + for _, neighbor := range []*Node{n.N, n.E, n.S, n.W} { + if neighbor == nil { + continue + } + wrk, ok := allNodes[c([]int{neighbor.X, neighbor.Y})] + if ok { + if n.steps+1 < wrk.steps { + wrk.steps = n.steps + 1 + if !wrk.visited { + ProcessNode(wrk, n.steps+1, keys) + } + } + } + } +} + +type state struct { + X, Y int + keys int + steps int +} + +func FindPath(vault map[string]bool, doors, keys map[string]int, start helpers.Coordinate, keylist, currkeys int) int { + queue := []state{state{X: start.X, Y: start.Y, keys: currkeys}} + visited := make(map[string]bool) + var st state + for { + st, queue = queue[0], queue[1:] + fmt.Println(st.keys&keylist, keylist) + if st.keys&keylist == keylist { + return st.steps + } + visited[c([]int{st.X, st.Y, st.keys})] = true + for _, v := range []*helpers.Coordinate{ + start.GetNorthCoord(), start.GetEastCoord(), + start.GetSouthCoord(), start.GetWestCoord(), + } { + wrk := helpers.NewCoordinate3d(v.X, v.Y, st.keys) + if !vault[helpers.NewCoordinate(wrk.X, wrk.Y).String()] || visited[wrk.String()] { + continue // Already been here, or can't be here + } + door, ok := doors[helpers.NewCoordinate(wrk.X, wrk.Y).String()] + if ok && wrk.Z&door != door { + fmt.Println("Hit door") + continue // Space exists, but we can't open the door + } + key, ok := keys[helpers.NewCoordinate(wrk.X, wrk.Y).String()] + if ok { + fmt.Println("Getting Key") + wrk.Z |= key // Pick up the key + } + queue = append(queue, state{X: wrk.X, Y: wrk.Y, keys: wrk.Z, steps: st.steps + 1}) + } + } } func isDoor(b byte) bool { @@ -34,3 +138,33 @@ func keyToDoor(b byte) byte { func doorToKey(b byte) byte { return (b - 'A') + 'a' } + +func keyInt(b byte) int { + return 1 << (b - 'a') +} + +func doorInt(b byte) int { + return 1 << (b - 'A') +} + +func c(vals []int) string { + if len(vals) == 2 { + return fmt.Sprintf("[%d, %d]", vals[0], vals[1]) + } else if len(vals) == 3 { + return fmt.Sprintf("[%d, %d, %d]", vals[0], vals[1], vals[2]) + } + return "" +} + +func unc(c string) (int, int, int) { + var x, y, z int + _, err := fmt.Sscanf(c, "[%d, %d, %d]", &x, &y, &z) + if err == nil { + return x, y, z + } + _, err = fmt.Sscanf(c, "[%d, %d]", &x, &y) + if err != nil { + panic(err) + } + return x, y, 0 +} diff --git a/2019/day18/vault.go b/2019/day18/vault.go index 7269093..1b44222 100644 --- a/2019/day18/vault.go +++ b/2019/day18/vault.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "fmt" helpers "git.bullercodeworks.com/brian/adventofcode/helpers" @@ -10,9 +9,11 @@ import ( type Vault struct { maxX, maxY int - vault map[helpers.Coordinate]byte - keys map[helpers.Coordinate]byte - doors map[helpers.Coordinate]byte + start helpers.Coordinate + vault map[string]bool + keys map[string]int + doors map[string]int + keylist int } func NewVault(file string) *Vault { @@ -20,55 +21,41 @@ func NewVault(file string) *Vault { wrk := bytes.Split(inp, []byte{'\n'}) v := Vault{ - vault: make(map[helpers.Coordinate]byte), - keys: make(map[helpers.Coordinate]byte), - doors: make(map[helpers.Coordinate]byte), + vault: make(map[string]bool), + keys: make(map[string]int), + doors: make(map[string]int), } v.maxY = len(wrk) v.maxX = len(wrk[0]) for y, yv := range wrk { for x, xv := range yv { - v.vault[*helpers.NewCoordinate(x, y)] = xv + if xv == '@' { + v.start = *helpers.NewCoordinate(x, y) + xv = '.' + } + v.vault[helpers.NewCoordinate(x, y).String()] = (xv != '#') if isKey(xv) { - v.keys[*helpers.NewCoordinate(x, y)] = xv + k := keyInt(xv) + v.keys[helpers.NewCoordinate(x, y).String()] = k + v.keylist |= k } else if isDoor(xv) { - v.doors[*helpers.NewCoordinate(x, y)] = xv + d := doorInt(xv) + v.doors[helpers.NewCoordinate(x, y).String()] = d + v.keylist |= d } } } return &v } -func (v *Vault) findKey(door byte) (helpers.Coordinate, error) { - var r helpers.Coordinate - if !isDoor(door) { - return r, errors.New("Invalid Door") - } - for k := range v.keys { - if v.keys[k] == doorToKey(door) { - return k, nil - } - } - return r, errors.New("No key for that door") -} - -func (v *Vault) findDoor(key byte) (helpers.Coordinate, error) { - var r helpers.Coordinate - if !isKey(key) { - return r, errors.New("Invalid Key") - } - for k := range v.doors { - if v.doors[k] == keyToDoor(key) { - return k, nil - } - } - return r, errors.New("No door for that key") -} - func (v *Vault) Print() { for y := 0; y < v.maxY; y++ { for x := 0; x < v.maxX; x++ { - fmt.Print(string(v.vault[*helpers.NewCoordinate(x, y)])) + if v.vault[helpers.NewCoordinate(x, y).String()] { + fmt.Print(".") + } else { + fmt.Print("#") + } } fmt.Println() } diff --git a/2019/day20/input b/2019/day20/input new file mode 100644 index 0000000..e178749 --- /dev/null +++ b/2019/day20/input @@ -0,0 +1,125 @@ + X H D Z O K W + N O W Z S L F + ###################################.#######.###########.###.#.#######.###.####################################### + #.....#.........................#.....#.#...#...........#.#.....#.......#...............................#.....#.# + ###.#####.#.#####.#######.###.#####.###.#.#######.#####.#.#####.#######.###.###.###.###.#.#.###.#######.###.###.# + #.#...#...#.#.#.....#...#.#.#.#.......#...#.#.#.....#.#.#.....#...#.....#.#...#...#...#.#.#.#.......#...#.....#.# + #.#.#########.#########.###.#.###.#####.#.#.#.#.###.#.#.###.#.#.#.###.###.#####.###.#####.#####.###########.###.# + #.....#.#.....#.........#...........#.#.#.....#.#.#.#...#...#...#.#.......#.#.....#.#.#.#...#...#.#.#...#...#.#.# + #.#####.#####.###.#####.###.###.#.###.#####.#####.#.###.###.#########.#.###.###.#####.#.#########.#.#.###.###.#.# + #.....#...#.#...#...#.......#.#.#.......#.#.#.#.....#.#.#.........#...#.#.#...#.....#.#...#.#...#.#.#.....#.#...# + ###.#####.#.###.#######.###.#.#######.###.#.#.###.###.###.#####.#####.#.#.#.#.#.###.#.#.###.###.#.#.###.###.#.### + #.#.....#.#...#...#.#.#.#.#.#.#.......#.......#.........#.#...#.#.....#.#...#.#.#.........#.#.#...........#...#.# + #.#.#####.###.#.###.#.###.###.###.#.#.#.###.#####.###########.#.###.###.#.###.#.###.#.#.###.#.#.###.###.#####.#.# + #.......#.#.#.#...#.#...#.........#.#.#.#.....#.......#.#.#...#...#.#...#.#.....#.#.#.#...........#...#...#...#.# + ###.#.###.#.#.#.#.#.###.#########.###.#####.#####.#####.#.###.#.#######.#.#.#.###.#####.###.#.###############.#.# + #.#.#.#.......#.#.#...#.....#...#...#.#...#...#.....#.#...........#.....#.#.#.....#.#...#.#.#.#.#.....#.#...#...# + #.#.#########.###.#.#####.#####.#.#######.#.#.###.###.#######.#.#######.#.###.#####.###.#.#####.#.#.#.#.###.#.### + #...#...#.....................#.#...#.#.#...#.#...#...#.......#.#...#...#...#.....#...............#.#...#.....#.# + #.#.#.###.#.###.###.#####.#.#.#.#.###.#.#.#######.###.#####.###.###.###.#.###.###########.#.###.#########.###.#.# + #.#.#...#.#.#.#.#.....#...#.#.........#.#.#.#.#...#.....#.....#...#...#.#...#.#.....#...#.#.#.#.....#.#...#...#.# + #.###.#######.#####.#.#.#.###.#######.#.#.#.#.###.###.#.#.#####.###.#.#.###.###.#.#####.#####.#######.#######.#.# + #.....#...#.#...#.#.#.#.#.#...#.#.....#.#.#.#...#.....#.#.#.#.....#.#...#...#.#.#.#.....#...#.........#.......#.# + #.#######.#.###.#.###.#######.#.#.#####.#.#.#.#####.#######.#.#.###.#.#.#.###.#.#####.#.#.#########.#####.###.#.# + #.......#.....#...#...#.......#.........#...#...#.#.....#.....#.#...#.#.#.....#...#...#.#.........#...#.#.#...#.# + #.#########.###.#.#######.#########.#.#####.#.#.#.#####.#.#####.#.###########.#.###.#####.#.#######.#.#.#####.#.# + #.#...#.#.#...#.#.......#...#.#.#...#...#.#...#...#.....#...#.#.#...#.....#.#...#.........#...#.#...#...#.#.#...# + #.#.###.#.#.#######.###.#####.#.#######.#.#####.#####.#.###.#.###.#####.###.#.#.#.#.###.#.#####.###.#####.#.###.# + #...#.#.#...#.#...#.#.#.#.#...#.#.#.#.......#...#.#.#.#.#...#...#.......#.....#...#...#.#.#.....#.....#.#...#...# + #.###.#.#.#.#.###.###.#.#.###.#.#.#.###.###.#.#.#.#.###.#.###.###.#####.#######.###########.#.#####.###.###.###.# + #.#.#...#.#.....#...#.#.#.......#.......#.#.#.#.#.......#...#.#.....#.....#...#.........#.#.#.#.#.#.#.#.#.......# + #.#.###.###.#######.#.#.#####.###.#####.#.###.###.#.#.#####.#.###.#.#.#.#####.#.#########.###.#.#.#.#.#.###.##### + #.#.#.#.........#.........#.#.........#...#.....#.#.#.#.........#.#.#.#.#.......#...#...#.#.....#.#.#...#.......# + #.#.#.#####.###########.#.#.#.#######.#####.#####.###########.#######.###.#########.###.#.###.###.#.#.#.###.##### + #.....#...#.#.....#.#.#.#.#.#.# S K H R I H #.#.......#.#.....#.#.#.....#.# + #####.###.#.#####.#.#.#.###.### U A W P O C #.###.#####.###.#.###.###.###.# + #.#.#...#.....#.#...#...#.#...# #.....#...#...#.#.#.#...#...#.# + #.#.#.#####.###.###.###.#.#.### ###.#####.#.###.###.###.#.###.# + #.#...#...#.#...............#.# NZ..............#.............#.# + #.#.#####.#.###.#######.###.#.# #.###.#####.###.###.#.###.###.# +KA....#.#.......#.#.......#.....# #.#.#.#.........#.#.#...#.....# + #.###.#####.#########.#######.# ###.#####.#.###.#.#.#########.# + #.......#...#.#.#.#.#.....#....DA #...#.#.#.#.#.....#.#.#...#.#..HC + ###.###.#.#.#.#.#.#.#####.###.# #.#.#.#.#########.#.#.#.###.### + #...#.....#...............#...# #.#.......#.#.#.#.#.#..........PZ + #.#####################.#.#.#.# #####.#.###.#.#.#####.#.###.#.# + #.#.......#.........#.#.#.#.#.# #.#...#...............#...#.#.# + ###.###.#.#.#######.#.######### #.###.#####.###.#.#########.### +RO....#.#.#...#.#.#.....#.#.....# #...#...#.....#.#.#...#.#.#...# + ###.#.###.###.#.###.#.#.###.#.# ###.#.###########.#.###.#.##### + #.....#.#.#.#.#.#.#.#.....#.#.# XN......#.#.#...#.......#...#...# + #######.###.#.#.#.###.###.#.#.# #####.#.#.###.#.###.###.#.#.### + #...#.......#...#...#.#.....#..WF TZ....#.#.......#.#...#...#.#.#.# + ###.###.#####.#####.########### ###.###.#.#.#####.#######.#.#.# +TZ....#...#.....................# #.......#.#.#.#.....#...#.#.#..YK + ###.#.#.#.#######.###.#####.#.# ###.#.###.###.#######.###.#.#.# + #...#.#.#.....#...#.#.#...#.#.# #...#.#...#.......#.#...#.....# + ###.#.#.###.#######.###.#####.# #########.#.#.#####.#.###.###.# + #.#...#.......#.....#.#...#....QN #...#.#.....#.............#...# + #.###############.###.#.#####.# ###.#.###.#.###.#.#.#.#.####### + #...#.....#.#.......#...#.....# #.#.....#.#.#.#.#.#.#.#.#...#.# + #.###.#.#.#.#.#####.#.#.#####.# #.###.#######.###########.#.#.# + #.#.#.#.#...#...#...#.#...#.#.# #.#.......#.#.....#.....#.#....IO + #.#.#.#####.#.###.###.###.#.### #.###.###.#.#.#########.#.###.# + #.......#.......#.#.....#.....# JZ......#.........#.#.#.....#...# + #.###.#####.#####.#####.#.##### #########.###.###.#.###.#####.# +DQ....#...#.....#.........#......JO #...#.#.#.#.................#.# + #.###.#####.################### ###.#.#.#######.#######.####### + #...#.#...#.#.....#.#...#.....# #.#...........#.#.....#...#...# + #####.###.#####.###.#.#.#.###.# #.#.#####.###.#####.#########.# +RU....#.#.....#.#...#...#.#...#.# OS....#.#...#...#.....#.#...#.#.# + #.#######.###.#.#.#.###.###.#.# #.#.#.###.#.#.###.#.#.###.#.#.# + #...............#.....#.....#..QT #.#...#...#.#...#.#...#.#.....# + #.#######.#.###.###.########### ###.#.#####.#####.###.#.#.###.# + #.#.#.#...#.#...#.#...#.#.#...# #.#.#.#...#.........#.......#..QN + ###.#.###.#.###.#.#####.#.#.#.# #.#.#.###.##################### + #.#...#...#.#...#.#.#.#.....#.# #.#.#.#.............#.#...#.#..HW + #.#.#############.#.#.#####.#.# #.#####.#.#########.#.#.#.#.#.# + #.#.#...#.#...#.#.......#.#.#..DQ #.#.#...#.#...#.........#.....# + #.#.#.###.#.###.###.#.###.#.### #.#.#####.#.###.#####.#.###.### +JZ....................#.........# QO....#.#.......#.#.#...#...#...# + ###.#########################.# ###.#.#.#.#######.###.###.#.#.# +PF..#.#.......#.....#.........#.# #.......#...#...#.....#.#.#.#.# + #.###.###.#.#.###.#.#.###.##### #############.#####.#.#.####### + #.......#.#.....#...#.#.......# #...#...#...#...#...#.#.#...#.# + #.#.#.###.#.#####.#.###.#.##### #.#.#.#.#.###.#####.###.###.#.# + #.#.#...#.#.....#.#.#.#.#.#.#.# KL..#.#.#.#.....#...#.#.#.......# + #.#.###.#####.###.###.###.#.#.# ###.#.#.#.#.#####.###.#######.# + #.#...#...#...#...#............RU #...#.#...#...#.#.#.#...#.#.#..RP + #.#.###.###.###.#.#.#.#.#.###.# #.###.#.#.###.#.#.#.#.###.#.#.# + #.#...#.#.....#.#.#.#.#.#...#.# #.....#.#.#...................# + ###.###.#.###.###.###.###.###.# D P Y O H R P #.###.#######.#.#########.#.### + #...#...#.#...#.....#.#...#.#.# W Z K A O O F #.#.......#...#.....#.....#...# + #.#.###.###.###.#.#.#####.#.#########.#####.#########.#########.#####.###.###.#####.###.###.#.#.#.#######.#.#.### + #.#.#.....#...#.#.#.#.#.........#.........#.#...#.......#.......#.#...#.....#.....#.#.....#.#.#.#...#.#...#.#...# + ###.###.###.#.#.#.###.#.###.#.###.###.###.#.#.#.###.#.#.#####.###.#.#####.#.#.#.#.#########.#####.#.#.###.###.### + #.....#...#.#.#.#...#.....#.#.#.#.#...#.#.#...#...#.#.#.....#.#.........#.#.#.#.#.....#.#...#.#...#...#...#.....# + ###.#.#.#########.#######.#####.#######.#.#####.###.#.#######.###.#######.###.#.#######.#####.#####.#####.###.#.# + #...#.#.#.#.#.#.....#.#...#...............#...#...#.#.#...#.....#.....#...#...#...#.#.........#.........#...#.#.# + #.#.#.###.#.#.#.#####.#######.#.#.#.###.###.#.#.###.#####.#####.#.###.###.#.###.###.#.###########.###.#.###.#.#.# + #.#.#...#.......#.#...#...#...#.#.#.#.#...#.#...#.......#.......#.#.#.#.......#.......#.......#...#...#...#.#.#.# + #####.#########.#.#.###.#############.###.#.#######.###.#.#######.#.#####.###.#.#####.#####.#####.#.#.#.###.###.# + #.#.......#.......#.#...#.................#.#.....#...#.#.#.#.#.....#.......#.#.....#...........#.#.#.#...#...#.# + #.#.###.#.#########.#.#############.#######.#.###.###.###.#.#.###.#.###.#######.#####.#.#.#.#.#######.###.#.###.# + #.....#.#.#.....#...#...#.#...............#.#...#...#.#...#.....#.#...#.#.#.........#.#.#.#.#...#.......#.#...#.# + #.#.#####.#####.###.#.###.###############.#.###.#.###.###.#.###.#.#####.#.###.#####.###.###.#.#.###.#.#.#######.# + #.#...#.....#.#.....#.....#.....#.#.......#.....#.#.....#.....#.#...#...#...#...#.#...#...#.#.#.#...#.#.#.......# + #.#.#########.#####.#.#########.#.#.#######.#####.#.#.#.#.#.###.#.#########.#.###.#.###.#####.###.#.#.#########.# + #.#.#...#.....#...#.....#.#.#.........#.......#...#.#.#.#.#.#.#.#...#.....#.......#.#.....#.....#.#.#...#.......# + #.#####.#####.#.###.#.###.#.###.#.#.###.#.#.###.#.#.#####.###.#.#.#####.#######.#.###########.#####.#######.#.### + #.#.......#.........#.#.#.......#.#.#.#.#.#.#...#.#.#.#.....#...#...#.....#...#.#.#...#...#...#.#.#.....#...#...# + ###.###.#.#.#.#.#.#.#.#.#.###.#.###.#.###.#####.###.#.#########.#.###.#######.#.#####.###.###.#.#.#########.###.# + #.#...#.#...#.#.#.#.#...#.#.#.#...#.....#.#.......#...#.....#...#.......#.#.#...#.#...#.....#.#...#.....#.#.#...# + #.#.#.#.#.#.#.#######.#####.###.###.#########.###.#.#####.#####.#######.#.#.#.###.#.###.#######.#.#.#.#.#.#####.# + #.#.#.#.#.#.#.#.........#.........#.......#...#...#.....#...#...#.........#...........#.....#...#.#.#.#.#...#.#.# + #.#####.###.#########.#########.###.#######.#####.#####.###.###.###.#.#.#######.#.#####.#.#####.###.#####.###.#.# + #.......#.#.#.............#.....#...#.#.#.....#.#.#.....#...#.....#.#.#.#.....#.#...#.#.#...#...#...#.#.....#...# + #.#.#####.###.###.#.#####.#########.#.#.#.#####.#.#.#.#.###.###.###.#######.#.#.#####.#.#####.#####.#.###.####### + #.#...#.....#.#.#.#.#.#.........#.......#.#.#.#...#.#.#.#.......#...#...#.#.#.....................#...#.........# + #########.#.###.#####.###.#####.###.#######.#.###.#.#######.###.#.#####.#.#.###############.#.#####.###.######### + #.........#.........#.....#.........#...#.#...#...#...#...#.#...#.#.#.....................#.#...................# + ###.#.#####.#####.###.#.#####.#####.###.#.#.###.#.#.#.###.#.#####.#.###########.#######.#####.###.###########.### + #...#.#.......#.......#.#.......#.....#.........#.#.#.#.......#.......#...............#.....#.#...........#.....# + ###################################.#######.#.#####.#####.###########.###.###.################################### + J A O Q N Q S D + O A A T Z O U A diff --git a/2019/day20/main.go b/2019/day20/main.go new file mode 100644 index 0000000..42aa1b5 --- /dev/null +++ b/2019/day20/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "time" + + helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +var maze *Maze + +func main() { + file := "input" + var manual bool + if helpers.GetArgNumber(1) != "" { + file = helpers.GetArgNumber(1) + } + if helpers.GetArgNumber(2) == "manual" { + manual = true + } + maze = NewMaze(file) + if manual { + runmanual(maze) + } + part1() + //part2(inp) +} + +func part1() { + maze.GetStart().steps = 0 + ProcessNode(maze.GetStart(), 0) + fmt.Println("Distance:", maze.GetEnd().steps) +} + +func ProcessNode(m *MazeCoord, steps int) { + fmt.Println("Processing Node", m) + if maze.IsEnd(m.X, m.Y) { + fmt.Println(" End") + return + } + for _, neighbor := range []*MazeCoord{m.N, m.E, m.S, m.W} { + if neighbor == nil { + continue + } + if m.steps+1 < neighbor.steps { + neighbor.steps = m.steps + 1 + if !neighbor.visited { + ProcessNode(neighbor, m.steps+1) + } + } + } + m.visited = true +} + +func runmanual(m *Maze) { + var err error + var inp string + m.Print() + for { + reader := bufio.NewReader(os.Stdin) + inp, err = reader.ReadString('\n') + if err != nil { + fmt.Println("Input Error:", err.Error()) + continue + } + var valid bool + switch inp[0] { + case 'N', 'n': + valid = m.MoveNorth() + case 'E', 'e': + valid = m.MoveEast() + case 'S', 's': + valid = m.MoveSouth() + case 'W', 'w': + valid = m.MoveWest() + case 'Q', 'q': + fmt.Println("Quitting") + break + } + fmt.Println(helpers.CLEAR_SCREEN) + m.Print() + if m.IsDone() { + fmt.Println("DONE!") + break + } + if !valid { + fmt.Println("Invalid Move") + } + time.Sleep(time.Second / 2) + + } +} diff --git a/2019/day20/maze.go b/2019/day20/maze.go new file mode 100644 index 0000000..5cccd72 --- /dev/null +++ b/2019/day20/maze.go @@ -0,0 +1,228 @@ +package main + +import ( + "bytes" + "fmt" + + helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +type Maze struct { + start *MazeCoord + end *MazeCoord + maze map[string]*MazeCoord + portals map[string]*Portal + + current *MazeCoord + + BotX, BotY int + TopX, TopY int +} + +func NewMaze(file string) *Maze { + inp := helpers.FileToBytes(file) + lines := bytes.Split(inp, []byte{'\n'}) + m := Maze{ + maze: make(map[string]*MazeCoord), + portals: make(map[string]*Portal), + } + for yk, yv := range lines { + for xk, xv := range yv { + if xv == '.' || xv == '#' { + m.maze[c(xk, yk)] = &MazeCoord{ + X: xk, + Y: yk, + Value: xv, + steps: helpers.MAX_INT, + } + } + } + } + // Now hook up neighbor coords + for _, v := range m.maze { + if v.X < m.BotX { + m.BotX = v.X + } + if v.X > m.TopX { + m.TopX = v.X + } + if v.Y < m.BotY { + m.BotY = v.Y + } + if v.Y > m.TopY { + m.TopY = v.Y + } + var d *MazeCoord + var ok bool + // Hook up north + if d, ok = m.maze[c(v.X, v.Y-1)]; ok { + v.N = d + } else if v.Value == '.' { + // North Portal + name := string([]byte{lines[v.Y-2][v.X], lines[v.Y-1][v.X]}) + var p *Portal + if p, ok = m.portals[name]; ok { + v.N = m.maze[c(p.X1, p.Y1)] + m.maze[c(p.X1, p.Y1)].S = v + p.X2 = v.X + p.Y2 = v.Y + } else { + m.portals[name] = &Portal{ + Name: name, + X1: v.X, + Y1: v.Y, + } + } + } + // Hook up east + if d, ok = m.maze[c(v.X+1, v.Y)]; ok { + v.E = d + } else if v.Value == '.' { + // East Portal + name := string([]byte{lines[v.Y][v.X+1], lines[v.Y][v.X+2]}) + var p *Portal + if p, ok = m.portals[name]; ok { + v.E = m.maze[c(p.X1, p.Y1)] + m.maze[c(p.X1, p.Y1)].W = v + p.X2 = v.X + p.Y2 = v.Y + } else { + m.portals[name] = &Portal{ + Name: name, + X1: v.X, + Y1: v.Y, + } + } + } + // Hook up south + if d, ok = m.maze[c(v.X, v.Y+1)]; ok { + v.S = d + } else if v.Value == '.' { + // South Portal + name := string([]byte{lines[v.Y+1][v.X], lines[v.Y+2][v.X]}) + var p *Portal + if p, ok = m.portals[name]; ok { + v.S = m.maze[c(p.X1, p.Y1)] + m.maze[c(p.X1, p.Y1)].N = v + fmt.Println(v.S, "<=>", m.maze[c(p.X1, p.Y1)].N) + p.X2 = v.X + p.Y2 = v.Y + } else { + m.portals[name] = &Portal{ + Name: name, + X1: v.X, + Y1: v.Y, + } + } + } + // Hook up west + if d, ok = m.maze[c(v.X-1, v.Y)]; ok { + v.W = d + } else if v.Value == '.' { + // West Portal + name := string([]byte{lines[v.Y][v.X-2], lines[v.Y][v.X-1]}) + var p *Portal + if p, ok = m.portals[name]; ok { + v.W = m.maze[c(p.X1, p.Y1)] + m.maze[c(p.X1, p.Y1)].E = v + p.X2 = v.X + p.Y2 = v.Y + } else { + m.portals[name] = &Portal{ + Name: name, + X1: v.X, + Y1: v.Y, + } + } + } + + } + + st := m.portals["AA"] + m.current = m.maze[c(st.X1, st.Y1)] + return &m +} + +func (m *Maze) MoveNorth() bool { + if m.current.N != nil && m.current.N.Value == '.' { + m.current = m.current.N + return true + } + return false +} + +func (m *Maze) MoveEast() bool { + if m.current.E != nil && m.current.E.Value == '.' { + m.current = m.current.E + return true + } + return false +} + +func (m *Maze) MoveSouth() bool { + if m.current.S != nil && m.current.S.Value == '.' { + m.current = m.current.S + return true + } + return false +} + +func (m *Maze) MoveWest() bool { + if m.current.W != nil && m.current.W.Value == '.' { + m.current = m.current.W + return true + } + return false +} + +func (m *Maze) Print() { + for y := m.BotY; y <= m.TopY; y++ { + for x := m.BotX; x <= m.TopX; x++ { + if m.current.X == x && m.current.Y == y { + fmt.Print("%") + } else { + if v, ok := m.maze[c(x, y)]; ok { + fmt.Print(string(v.Value)) + } else { + fmt.Print(" ") + } + } + } + fmt.Println() + } +} + +func (m *Maze) GetStart() *MazeCoord { + start := m.portals["AA"] + return m.maze[c(start.X1, start.Y1)] +} + +func (m *Maze) IsDone() bool { + return m.IsEnd(m.current.X, m.current.Y) +} + +func (m *Maze) GetEnd() *MazeCoord { + end := m.portals["ZZ"] + return m.maze[c(end.X1, end.Y1)] +} + +func (m *Maze) IsEnd(x, y int) bool { + return x == m.GetEnd().X && y == m.GetEnd().Y +} + +type Portal struct { + Name string + X1, Y1, X2, Y2 int +} + +type MazeCoord struct { + X, Y int + N, E, S, W *MazeCoord + Value byte + visited bool + steps int +} + +func c(x, y int) string { + return fmt.Sprintf("[%d, %d]", x, y) +} diff --git a/2019/day20/testinput1 b/2019/day20/testinput1 new file mode 100644 index 0000000..1e8d56f --- /dev/null +++ b/2019/day20/testinput1 @@ -0,0 +1,19 @@ + A + A + #######.######### + #######.........# + #######.#######.# + #######.#######.# + #######.#######.# + ##### B ###.# +BC...## C ###.# + ##.## ###.# + ##...DE F ###.# + ##### G ###.# + #########.#####.# +DE..#######...###.# + #.#########.###.# +FG..#########.....# + ###########.##### + Z + Z diff --git a/2019/day21/input b/2019/day21/input new file mode 100644 index 0000000..5bf9e21 --- /dev/null +++ b/2019/day21/input @@ -0,0 +1 @@ +109,2050,21102,1,966,1,21102,1,13,0,1106,0,1378,21102,20,1,0,1106,0,1337,21101,0,27,0,1106,0,1279,1208,1,65,748,1005,748,73,1208,1,79,748,1005,748,110,1208,1,78,748,1005,748,132,1208,1,87,748,1005,748,169,1208,1,82,748,1005,748,239,21102,1,1041,1,21101,73,0,0,1106,0,1421,21102,78,1,1,21102,1041,1,2,21102,88,1,0,1105,1,1301,21101,0,68,1,21102,1041,1,2,21101,103,0,0,1105,1,1301,1102,1,1,750,1106,0,298,21101,0,82,1,21102,1,1041,2,21101,0,125,0,1105,1,1301,1101,2,0,750,1106,0,298,21101,79,0,1,21102,1,1041,2,21101,0,147,0,1106,0,1301,21101,0,84,1,21101,0,1041,2,21101,162,0,0,1106,0,1301,1102,3,1,750,1106,0,298,21102,65,1,1,21102,1041,1,2,21102,184,1,0,1105,1,1301,21102,1,76,1,21101,0,1041,2,21101,0,199,0,1105,1,1301,21101,0,75,1,21102,1041,1,2,21102,1,214,0,1105,1,1301,21102,221,1,0,1106,0,1337,21101,0,10,1,21101,0,1041,2,21101,0,236,0,1105,1,1301,1106,0,553,21102,85,1,1,21101,1041,0,2,21102,254,1,0,1105,1,1301,21101,0,78,1,21102,1041,1,2,21101,269,0,0,1105,1,1301,21102,1,276,0,1105,1,1337,21101,0,10,1,21101,0,1041,2,21101,0,291,0,1106,0,1301,1101,0,1,755,1106,0,553,21102,32,1,1,21101,1041,0,2,21101,0,313,0,1106,0,1301,21102,1,320,0,1106,0,1337,21101,327,0,0,1106,0,1279,2101,0,1,749,21102,1,65,2,21102,1,73,3,21102,1,346,0,1106,0,1889,1206,1,367,1007,749,69,748,1005,748,360,1102,1,1,756,1001,749,-64,751,1106,0,406,1008,749,74,748,1006,748,381,1101,-1,0,751,1105,1,406,1008,749,84,748,1006,748,395,1101,0,-2,751,1105,1,406,21101,1100,0,1,21102,1,406,0,1106,0,1421,21102,32,1,1,21102,1,1100,2,21101,0,421,0,1105,1,1301,21101,0,428,0,1106,0,1337,21102,1,435,0,1106,0,1279,1202,1,1,749,1008,749,74,748,1006,748,453,1102,-1,1,752,1105,1,478,1008,749,84,748,1006,748,467,1101,-2,0,752,1105,1,478,21102,1,1168,1,21102,1,478,0,1106,0,1421,21102,1,485,0,1105,1,1337,21102,10,1,1,21102,1,1168,2,21101,500,0,0,1106,0,1301,1007,920,15,748,1005,748,518,21101,1209,0,1,21102,1,518,0,1105,1,1421,1002,920,3,529,1001,529,921,529,1002,750,1,0,1001,529,1,537,1001,751,0,0,1001,537,1,545,102,1,752,0,1001,920,1,920,1106,0,13,1005,755,577,1006,756,570,21102,1,1100,1,21102,570,1,0,1105,1,1421,21102,987,1,1,1106,0,581,21101,1001,0,1,21101,588,0,0,1106,0,1378,1101,758,0,594,101,0,0,753,1006,753,654,20101,0,753,1,21101,0,610,0,1106,0,667,21102,1,0,1,21101,621,0,0,1106,0,1463,1205,1,647,21102,1015,1,1,21102,635,1,0,1105,1,1378,21101,1,0,1,21101,646,0,0,1105,1,1463,99,1001,594,1,594,1106,0,592,1006,755,664,1102,0,1,755,1106,0,647,4,754,99,109,2,1101,0,726,757,21201,-1,0,1,21101,9,0,2,21101,697,0,3,21102,1,692,0,1105,1,1913,109,-2,2105,1,0,109,2,101,0,757,706,2101,0,-1,0,1001,757,1,757,109,-2,2106,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,255,63,159,127,191,95,223,0,115,126,213,177,46,60,246,166,254,100,58,54,220,114,55,155,156,153,202,57,248,170,56,253,53,241,179,252,158,236,206,199,71,51,237,218,203,239,207,140,94,111,106,242,93,183,235,99,222,119,98,190,226,103,34,109,185,61,231,187,250,85,38,229,121,188,123,59,142,198,175,238,215,233,39,169,138,230,200,162,249,182,50,204,86,171,201,78,143,167,79,181,113,172,124,139,243,118,101,168,116,217,108,141,219,196,228,137,87,110,92,76,251,227,245,120,174,178,184,234,136,186,125,68,43,70,77,197,62,47,247,173,221,35,212,214,232,189,163,107,84,69,216,154,122,102,42,244,152,117,49,205,157,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,73,110,112,117,116,32,105,110,115,116,114,117,99,116,105,111,110,115,58,10,13,10,87,97,108,107,105,110,103,46,46,46,10,10,13,10,82,117,110,110,105,110,103,46,46,46,10,10,25,10,68,105,100,110,39,116,32,109,97,107,101,32,105,116,32,97,99,114,111,115,115,58,10,10,58,73,110,118,97,108,105,100,32,111,112,101,114,97,116,105,111,110,59,32,101,120,112,101,99,116,101,100,32,115,111,109,101,116,104,105,110,103,32,108,105,107,101,32,65,78,68,44,32,79,82,44,32,111,114,32,78,79,84,67,73,110,118,97,108,105,100,32,102,105,114,115,116,32,97,114,103,117,109,101,110,116,59,32,101,120,112,101,99,116,101,100,32,115,111,109,101,116,104,105,110,103,32,108,105,107,101,32,65,44,32,66,44,32,67,44,32,68,44,32,74,44,32,111,114,32,84,40,73,110,118,97,108,105,100,32,115,101,99,111,110,100,32,97,114,103,117,109,101,110,116,59,32,101,120,112,101,99,116,101,100,32,74,32,111,114,32,84,52,79,117,116,32,111,102,32,109,101,109,111,114,121,59,32,97,116,32,109,111,115,116,32,49,53,32,105,110,115,116,114,117,99,116,105,111,110,115,32,99,97,110,32,98,101,32,115,116,111,114,101,100,0,109,1,1005,1262,1270,3,1262,20102,1,1262,0,109,-1,2106,0,0,109,1,21101,0,1288,0,1106,0,1263,21001,1262,0,0,1101,0,0,1262,109,-1,2106,0,0,109,5,21102,1,1310,0,1105,1,1279,21202,1,1,-2,22208,-2,-4,-1,1205,-1,1332,21202,-3,1,1,21102,1332,1,0,1105,1,1421,109,-5,2106,0,0,109,2,21102,1,1346,0,1106,0,1263,21208,1,32,-1,1205,-1,1363,21208,1,9,-1,1205,-1,1363,1105,1,1373,21101,1370,0,0,1106,0,1279,1105,1,1339,109,-2,2105,1,0,109,5,2101,0,-4,1385,21002,0,1,-2,22101,1,-4,-4,21101,0,0,-3,22208,-3,-2,-1,1205,-1,1416,2201,-4,-3,1408,4,0,21201,-3,1,-3,1105,1,1396,109,-5,2106,0,0,109,2,104,10,21202,-1,1,1,21101,0,1436,0,1105,1,1378,104,10,99,109,-2,2105,1,0,109,3,20002,594,753,-1,22202,-1,-2,-1,201,-1,754,754,109,-3,2105,1,0,109,10,21101,0,5,-5,21101,1,0,-4,21101,0,0,-3,1206,-9,1555,21101,3,0,-6,21101,5,0,-7,22208,-7,-5,-8,1206,-8,1507,22208,-6,-4,-8,1206,-8,1507,104,64,1106,0,1529,1205,-6,1527,1201,-7,716,1515,21002,0,-11,-8,21201,-8,46,-8,204,-8,1106,0,1529,104,46,21201,-7,1,-7,21207,-7,22,-8,1205,-8,1488,104,10,21201,-6,-1,-6,21207,-6,0,-8,1206,-8,1484,104,10,21207,-4,1,-8,1206,-8,1569,21102,1,0,-9,1105,1,1689,21208,-5,21,-8,1206,-8,1583,21102,1,1,-9,1105,1,1689,1201,-5,716,1589,20102,1,0,-2,21208,-4,1,-1,22202,-2,-1,-1,1205,-2,1613,22102,1,-5,1,21102,1,1613,0,1105,1,1444,1206,-1,1634,22102,1,-5,1,21102,1627,1,0,1106,0,1694,1206,1,1634,21101,2,0,-3,22107,1,-4,-8,22201,-1,-8,-8,1206,-8,1649,21201,-5,1,-5,1206,-3,1663,21201,-3,-1,-3,21201,-4,1,-4,1106,0,1667,21201,-4,-1,-4,21208,-4,0,-1,1201,-5,716,1676,22002,0,-1,-1,1206,-1,1686,21101,0,1,-4,1105,1,1477,109,-10,2105,1,0,109,11,21101,0,0,-6,21101,0,0,-8,21101,0,0,-7,20208,-6,920,-9,1205,-9,1880,21202,-6,3,-9,1201,-9,921,1725,20102,1,0,-5,1001,1725,1,1733,20101,0,0,-4,22102,1,-4,1,21101,0,1,2,21102,1,9,3,21102,1,1754,0,1105,1,1889,1206,1,1772,2201,-10,-4,1766,1001,1766,716,1766,21002,0,1,-3,1106,0,1790,21208,-4,-1,-9,1206,-9,1786,22101,0,-8,-3,1106,0,1790,22102,1,-7,-3,1001,1733,1,1795,21001,0,0,-2,21208,-2,-1,-9,1206,-9,1812,21201,-8,0,-1,1105,1,1816,21202,-7,1,-1,21208,-5,1,-9,1205,-9,1837,21208,-5,2,-9,1205,-9,1844,21208,-3,0,-1,1106,0,1855,22202,-3,-1,-1,1106,0,1855,22201,-3,-1,-1,22107,0,-1,-1,1105,1,1855,21208,-2,-1,-9,1206,-9,1869,21201,-1,0,-8,1106,0,1873,22101,0,-1,-7,21201,-6,1,-6,1105,1,1708,22101,0,-8,-10,109,-11,2106,0,0,109,7,22207,-6,-5,-3,22207,-4,-6,-2,22201,-3,-2,-1,21208,-1,0,-6,109,-7,2106,0,0,0,109,5,1202,-2,1,1912,21207,-4,0,-1,1206,-1,1930,21101,0,0,-4,22102,1,-4,1,22101,0,-3,2,21101,0,1,3,21101,1949,0,0,1105,1,1954,109,-5,2106,0,0,109,6,21207,-4,1,-1,1206,-1,1977,22207,-5,-3,-1,1206,-1,1977,21201,-5,0,-5,1106,0,2045,21202,-5,1,1,21201,-4,-1,2,21202,-3,2,3,21102,1996,1,0,1105,1,1954,22101,0,1,-5,21101,1,0,-2,22207,-5,-3,-1,1206,-1,2015,21101,0,0,-2,22202,-3,-2,-3,22107,0,-4,-1,1206,-1,2037,21202,-2,1,1,21102,2037,1,0,105,1,1912,21202,-3,-1,-3,22201,-5,-3,-5,109,-6,2105,1,0 diff --git a/2019/day21/main.go b/2019/day21/main.go new file mode 100644 index 0000000..97e7b27 --- /dev/null +++ b/2019/day21/main.go @@ -0,0 +1,83 @@ +package main + +import ( + "fmt" + "time" + + intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor" + helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + pt := helpers.GetArgNumber(1) + prog := intcode.ReadIntCodeFile("input") + if pt == "1" { + part1(prog) + } else { + part2(prog) + } +} + +func part1(prog []int) { + p := intcode.NewProgram(prog) + //p.EnableDebug() + var lastOut int + go func() { + for { + if !p.NeedsOutput() && !p.NeedsInput() { + time.Sleep(1) + } else if p.NeedsOutput() { + lastOut = getOutput(p) + fmt.Print(string(byte(lastOut))) + } else if p.NeedsInput() { + instructions := "NOT A J\nNOT C T\nOR T J\nAND D J\nWALK\n" + //instructions := "NOT A J\nWALK\n" + for _, v := range []byte(instructions) { + fmt.Print(string(v)) + p.Input(int(v)) + } + } + } + /* + fmt.Println(getOutput(p)) + return + */ + }() + p.Run() + fmt.Println("Damage:", lastOut) +} + +func part2(prog []int) { + p := intcode.NewProgram(prog) + //p.EnableDebug() + var lastOut int + go func() { + for { + if !p.NeedsOutput() && !p.NeedsInput() { + time.Sleep(1) + } else if p.NeedsOutput() { + lastOut = getOutput(p) + fmt.Print(string(byte(lastOut))) + } else if p.NeedsInput() { + instructions := "NOT A J\nNOT B T\nOR T J\nNOT C T\nOR T J\nAND D J\nAND E T\nOR H T\nAND T J\nRUN\n" + for _, v := range []byte(instructions) { + fmt.Print(string(v)) + p.Input(int(v)) + } + } + } + /* + fmt.Println(getOutput(p)) + return + */ + }() + p.Run() + fmt.Println("Damage:", lastOut) +} + +func getOutput(p *intcode.Program) int { + for !p.NeedsOutput() { + time.Sleep(1) + } + return p.Output() +} diff --git a/2019/day22/input b/2019/day22/input new file mode 100644 index 0000000..4fca6a0 --- /dev/null +++ b/2019/day22/input @@ -0,0 +1,100 @@ +deal into new stack +deal with increment 32 +cut 5214 +deal with increment 50 +cut -7078 +deal with increment 3 +cut 5720 +deal with increment 18 +cut -6750 +deal with increment 74 +cut -6007 +deal with increment 16 +cut -3885 +deal with increment 40 +deal into new stack +cut -2142 +deal with increment 25 +deal into new stack +cut -1348 +deal with increment 40 +cut 3943 +deal with increment 14 +cut 7093 +deal with increment 67 +cut 1217 +deal with increment 75 +cut 597 +deal with increment 60 +cut -1078 +deal with increment 68 +cut -8345 +deal with increment 25 +cut 6856 +deal into new stack +cut -4152 +deal with increment 59 +deal into new stack +cut -80 +deal with increment 3 +deal into new stack +deal with increment 44 +cut 1498 +deal with increment 18 +cut -7149 +deal with increment 58 +deal into new stack +deal with increment 71 +cut -323 +deal into new stack +deal with increment 58 +cut 1793 +deal with increment 45 +deal into new stack +cut 7187 +deal with increment 48 +cut 2664 +deal into new stack +cut 8943 +deal with increment 32 +deal into new stack +deal with increment 62 +cut -9436 +deal with increment 67 +deal into new stack +cut -1898 +deal with increment 61 +deal into new stack +deal with increment 14 +cut 1287 +deal with increment 8 +cut 560 +deal with increment 6 +cut -2110 +deal with increment 8 +cut 9501 +deal with increment 25 +cut 4791 +deal with increment 70 +deal into new stack +deal with increment 5 +cut 2320 +deal with increment 47 +cut -467 +deal into new stack +deal with increment 19 +cut -1920 +deal with increment 16 +cut -8920 +deal with increment 65 +cut -3986 +deal with increment 3 +cut -2690 +deal with increment 35 +cut -757 +deal with increment 37 +cut -1280 +deal with increment 71 +cut 3765 +deal with increment 26 +deal into new stack diff --git a/2019/day22/main.go b/2019/day22/main.go new file mode 100644 index 0000000..176c6de --- /dev/null +++ b/2019/day22/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "fmt" + "math/big" + "strconv" + + helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + pt := helpers.GetArgNumber(1) + inp := helpers.StdinToStringSlice() + if pt == "1" { + part1(inp) + } else { + part2(inp) + } +} + +func NewDeck(n int) []int { + s := make([]int, n) + for i := 0; i < n; i++ { + s[i] = i + } + return s +} + +func deal(s []int) []int { + s2 := make([]int, len(s)) + for i := range s { + s2[len(s)-1-i] = s[i] + } + return s2 +} + +func cut(s []int, n int) []int { + if n >= 0 { + return append(s[n:], s[:n]...) + } + n = -n + return append(s[len(s)-n:], s[:len(s)-n]...) +} + +func dealn(s []int, n int) []int { + s2 := make([]int, len(s)) + for i := range s { + s2[(i*n)%len(s)] = s[i] + } + return s2 +} + +func part1(inp []string) { + s := NewDeck(10007) + for _, line := range inp { + if line == "deal into new stack" { + s = deal(s) + } else if line[:3] == "cut" { + i, _ := strconv.Atoi(line[4:]) + s = cut(s, i) + } else if line[:19] == "deal with increment" { + i, _ := strconv.Atoi(line[20:]) + s = dealn(s, i) + } + } + for i, v := range s { + if v == 2019 { + fmt.Println(i) + return + } + } +} + +func part2(inp []string) { + deckSize := big.NewInt(119315717514047) + shuffles := big.NewInt(101741582076661) + off, inc := big.NewInt(0), big.NewInt(1) + for _, line := range inp { + if line == "deal into new stack" { + inc.Mul(inc, big.NewInt(-1)) + off.Add(off, inc) + } else if line[:3] == "cut" { + i, _ := strconv.Atoi(line[4:]) + off.Add(off, big.NewInt(0).Mul(big.NewInt(int64(i)), inc)) + } else if line[:19] == "deal with increment" { + i, _ := strconv.Atoi(line[20:]) + inc.Mul(inc, big.NewInt(0).Exp(big.NewInt(int64(i)), big.NewInt(0).Sub(deckSize, big.NewInt(2)), deckSize)) + } + } + lastInc := big.NewInt(0).Exp(inc, shuffles, deckSize) + lastOff := big.NewInt(0).Exp(inc, shuffles, deckSize) + lastOff.Sub(big.NewInt(1), lastOff) + mod := big.NewInt(0).Exp(big.NewInt(0).Sub(big.NewInt(1), inc), big.NewInt(0).Sub(deckSize, big.NewInt(2)), deckSize) + lastOff.Mul(lastOff, mod) + lastOff.Mul(lastOff, off) + + res := big.NewInt(0).Mul(big.NewInt(2020), lastInc) + res.Add(res, lastOff) + res.Mod(res, deckSize) + fmt.Println("Result:", res) +} diff --git a/2019/day23/input b/2019/day23/input new file mode 100644 index 0000000..c384e82 --- /dev/null +++ b/2019/day23/input @@ -0,0 +1 @@ +3,62,1001,62,11,10,109,2241,105,1,0,1526,798,2012,1254,1054,1460,953,889,1975,1732,672,1190,2078,1157,2181,604,701,1295,1225,1326,1802,1493,1833,2113,1691,2144,1425,829,1942,1359,2045,1122,767,1567,1394,922,860,1660,990,732,1629,635,1023,1598,1091,2210,1878,1765,1909,571,0,0,0,0,0,0,0,0,0,0,0,0,3,64,1008,64,-1,62,1006,62,88,1006,61,170,1105,1,73,3,65,21002,64,1,1,20102,1,66,2,21101,105,0,0,1105,1,436,1201,1,-1,64,1007,64,0,62,1005,62,73,7,64,67,62,1006,62,73,1002,64,2,133,1,133,68,133,102,1,0,62,1001,133,1,140,8,0,65,63,2,63,62,62,1005,62,73,1002,64,2,161,1,161,68,161,1102,1,1,0,1001,161,1,169,1001,65,0,0,1102,1,1,61,1102,0,1,63,7,63,67,62,1006,62,203,1002,63,2,194,1,68,194,194,1006,0,73,1001,63,1,63,1106,0,178,21101,210,0,0,106,0,69,2101,0,1,70,1102,1,0,63,7,63,71,62,1006,62,250,1002,63,2,234,1,72,234,234,4,0,101,1,234,240,4,0,4,70,1001,63,1,63,1105,1,218,1106,0,73,109,4,21101,0,0,-3,21102,1,0,-2,20207,-2,67,-1,1206,-1,293,1202,-2,2,283,101,1,283,283,1,68,283,283,22001,0,-3,-3,21201,-2,1,-2,1105,1,263,22102,1,-3,-3,109,-4,2106,0,0,109,4,21101,0,1,-3,21102,0,1,-2,20207,-2,67,-1,1206,-1,342,1202,-2,2,332,101,1,332,332,1,68,332,332,22002,0,-3,-3,21201,-2,1,-2,1106,0,312,21202,-3,1,-3,109,-4,2105,1,0,109,1,101,1,68,359,20101,0,0,1,101,3,68,366,21002,0,1,2,21102,1,376,0,1106,0,436,22101,0,1,0,109,-1,2105,1,0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,109,8,21202,-6,10,-5,22207,-7,-5,-5,1205,-5,521,21101,0,0,-4,21101,0,0,-3,21102,1,51,-2,21201,-2,-1,-2,1201,-2,385,471,20101,0,0,-1,21202,-3,2,-3,22207,-7,-1,-5,1205,-5,496,21201,-3,1,-3,22102,-1,-1,-5,22201,-7,-5,-7,22207,-3,-6,-5,1205,-5,515,22102,-1,-6,-5,22201,-3,-5,-3,22201,-1,-4,-4,1205,-2,461,1105,1,547,21101,0,-1,-4,21202,-6,-1,-6,21207,-7,0,-5,1205,-5,547,22201,-7,-6,-7,21201,-4,1,-4,1105,1,529,22102,1,-4,-7,109,-8,2106,0,0,109,1,101,1,68,563,21001,0,0,0,109,-1,2105,1,0,1102,87671,1,66,1102,1,2,67,1102,598,1,68,1101,302,0,69,1101,1,0,71,1101,0,602,72,1105,1,73,0,0,0,0,19,59159,1101,59779,0,66,1101,0,1,67,1102,1,631,68,1101,0,556,69,1102,1,1,71,1101,633,0,72,1105,1,73,1,25,30,192358,1102,41491,1,66,1101,0,4,67,1101,0,662,68,1101,253,0,69,1102,1,1,71,1101,0,670,72,1105,1,73,0,0,0,0,0,0,0,0,30,96179,1101,0,77699,66,1102,1,1,67,1101,0,699,68,1101,556,0,69,1102,1,0,71,1102,701,1,72,1105,1,73,1,1177,1101,32299,0,66,1101,0,1,67,1102,1,728,68,1102,1,556,69,1101,1,0,71,1101,730,0,72,1106,0,73,1,1151,25,264644,1102,1,77591,66,1101,1,0,67,1101,759,0,68,1101,0,556,69,1101,0,3,71,1101,0,761,72,1105,1,73,1,2,25,132322,3,115706,3,289265,1101,46451,0,66,1102,1,1,67,1101,0,794,68,1101,0,556,69,1102,1,1,71,1102,1,796,72,1105,1,73,1,7537,26,236679,1101,37951,0,66,1101,0,1,67,1101,0,825,68,1102,556,1,69,1102,1,1,71,1101,827,0,72,1106,0,73,1,997,25,66161,1102,1,17491,66,1102,1,1,67,1101,856,0,68,1102,1,556,69,1102,1,1,71,1101,0,858,72,1105,1,73,1,-67132,24,75773,1101,78989,0,66,1101,0,1,67,1101,0,887,68,1102,1,556,69,1101,0,0,71,1102,889,1,72,1105,1,73,1,1547,1101,25969,0,66,1102,1,1,67,1102,916,1,68,1102,1,556,69,1101,2,0,71,1102,918,1,72,1105,1,73,1,10,47,112276,3,231412,1101,6469,0,66,1102,1,1,67,1101,0,949,68,1102,556,1,69,1101,1,0,71,1101,951,0,72,1105,1,73,1,-19261,24,454638,1102,101873,1,66,1102,1,1,67,1102,980,1,68,1101,556,0,69,1101,4,0,71,1102,1,982,72,1105,1,73,1,1,26,157786,8,164378,25,198483,11,95071,1102,1,48751,66,1102,2,1,67,1101,0,1017,68,1101,0,302,69,1102,1,1,71,1102,1021,1,72,1105,1,73,0,0,0,0,4,268724,1102,1,60383,66,1102,1,1,67,1101,1050,0,68,1102,1,556,69,1102,1,1,71,1102,1052,1,72,1106,0,73,1,121,8,246567,1102,1,67181,66,1101,4,0,67,1102,1081,1,68,1101,253,0,69,1101,1,0,71,1101,1089,0,72,1106,0,73,0,0,0,0,0,0,0,0,5,104479,1101,0,99277,66,1102,1,1,67,1101,0,1118,68,1101,0,556,69,1101,0,1,71,1102,1,1120,72,1106,0,73,1,-75,11,285213,1102,1,76753,66,1102,3,1,67,1101,1149,0,68,1101,0,302,69,1101,1,0,71,1101,0,1155,72,1105,1,73,0,0,0,0,0,0,4,201543,1101,0,79,66,1102,1,1,67,1101,1184,0,68,1102,1,556,69,1101,0,2,71,1101,0,1186,72,1105,1,73,1,125,49,87671,47,28069,1102,95071,1,66,1102,3,1,67,1101,1217,0,68,1102,1,302,69,1102,1,1,71,1101,1223,0,72,1105,1,73,0,0,0,0,0,0,12,6982,1101,0,78889,66,1101,1,0,67,1101,0,1252,68,1101,0,556,69,1102,1,0,71,1102,1,1254,72,1105,1,73,1,1927,1101,0,57853,66,1102,1,6,67,1102,1281,1,68,1102,1,302,69,1102,1,1,71,1102,1293,1,72,1106,0,73,0,0,0,0,0,0,0,0,0,0,0,0,5,208958,1101,16363,0,66,1101,0,1,67,1102,1,1322,68,1102,1,556,69,1102,1,1,71,1101,1324,0,72,1105,1,73,1,50656,24,303092,1102,59159,1,66,1102,1,2,67,1102,1,1353,68,1102,1,302,69,1102,1,1,71,1101,1357,0,72,1105,1,73,0,0,0,0,2,81758,1102,43853,1,66,1101,3,0,67,1102,1386,1,68,1102,302,1,69,1102,1,1,71,1102,1,1392,72,1106,0,73,0,0,0,0,0,0,4,134362,1102,67061,1,66,1102,1,1,67,1102,1,1421,68,1102,1,556,69,1102,1,1,71,1102,1423,1,72,1105,1,73,1,11,8,328756,1102,1,78893,66,1101,0,3,67,1102,1,1452,68,1101,302,0,69,1102,1,1,71,1102,1,1458,72,1106,0,73,0,0,0,0,0,0,41,124473,1102,1,104479,66,1101,2,0,67,1101,0,1487,68,1101,351,0,69,1101,1,0,71,1102,1491,1,72,1105,1,73,0,0,0,0,255,99571,1101,87587,0,66,1102,1,1,67,1102,1,1520,68,1101,0,556,69,1102,1,2,71,1102,1522,1,72,1106,0,73,1,509,29,87706,11,190142,1102,1,99571,66,1102,1,1,67,1102,1,1553,68,1101,0,556,69,1102,1,6,71,1102,1555,1,72,1106,0,73,1,21486,38,48751,12,3491,12,10473,31,76753,31,153506,31,230259,1102,1,85159,66,1101,1,0,67,1102,1594,1,68,1102,556,1,69,1102,1,1,71,1102,1,1596,72,1106,0,73,1,439,24,378865,1102,9187,1,66,1102,1,1,67,1101,1625,0,68,1101,556,0,69,1101,0,1,71,1101,0,1627,72,1106,0,73,1,67,8,82189,1101,49363,0,66,1102,1,1,67,1102,1656,1,68,1102,556,1,69,1101,0,1,71,1101,0,1658,72,1105,1,73,1,14,26,78893,1101,17291,0,66,1102,1,1,67,1102,1687,1,68,1102,1,556,69,1101,1,0,71,1101,0,1689,72,1105,1,73,1,-2,48,141374,1102,75773,1,66,1102,1,6,67,1102,1718,1,68,1102,1,253,69,1102,1,1,71,1102,1,1730,72,1106,0,73,0,0,0,0,0,0,0,0,0,0,0,0,49,175342,1102,13159,1,66,1102,2,1,67,1102,1759,1,68,1101,302,0,69,1101,0,1,71,1101,0,1763,72,1106,0,73,0,0,0,0,38,97502,1102,28069,1,66,1102,4,1,67,1101,0,1792,68,1101,0,302,69,1101,0,1,71,1102,1,1800,72,1105,1,73,0,0,0,0,0,0,0,0,3,57853,1101,40853,0,66,1102,1,1,67,1102,1,1829,68,1101,0,556,69,1102,1,1,71,1101,0,1831,72,1106,0,73,1,327322,24,227319,1102,1,53089,66,1101,1,0,67,1102,1860,1,68,1101,556,0,69,1101,0,8,71,1101,1862,0,72,1106,0,73,1,5,19,118318,2,40879,29,43853,28,65963,9,13159,47,56138,47,84207,3,173559,1102,1,44777,66,1102,1,1,67,1101,0,1905,68,1101,556,0,69,1102,1,1,71,1101,0,1907,72,1105,1,73,1,-71105,24,151546,1101,0,70687,66,1102,2,1,67,1102,1,1936,68,1102,302,1,69,1102,1,1,71,1102,1,1940,72,1106,0,73,0,0,0,0,41,82982,1102,1,65963,66,1102,1,2,67,1102,1,1969,68,1101,302,0,69,1102,1,1,71,1102,1,1973,72,1105,1,73,0,0,0,0,9,26318,1101,82189,0,66,1102,1,4,67,1101,0,2002,68,1101,0,302,69,1102,1,1,71,1101,0,2010,72,1106,0,73,0,0,0,0,0,0,0,0,41,41491,1101,0,40879,66,1101,0,2,67,1101,2039,0,68,1102,1,302,69,1102,1,1,71,1101,0,2043,72,1106,0,73,0,0,0,0,29,131559,1102,1,96179,66,1102,2,1,67,1102,1,2072,68,1101,0,302,69,1102,1,1,71,1102,1,2076,72,1106,0,73,0,0,0,0,28,131926,1101,3491,0,66,1102,3,1,67,1101,0,2105,68,1102,302,1,69,1102,1,1,71,1102,1,2111,72,1106,0,73,0,0,0,0,0,0,4,67181,1101,65551,0,66,1101,0,1,67,1102,1,2140,68,1102,1,556,69,1101,0,1,71,1102,1,2142,72,1105,1,73,1,296273,48,70687,1101,66161,0,66,1102,4,1,67,1101,2171,0,68,1102,302,1,69,1101,0,1,71,1101,2179,0,72,1106,0,73,0,0,0,0,0,0,0,0,41,165964,1101,0,81181,66,1101,0,1,67,1102,1,2208,68,1102,556,1,69,1102,0,1,71,1101,2210,0,72,1106,0,73,1,1141,1101,0,74093,66,1101,0,1,67,1102,2237,1,68,1102,1,556,69,1101,1,0,71,1102,1,2239,72,1106,0,73,1,160,3,347118 diff --git a/2019/day23/main.go b/2019/day23/main.go new file mode 100644 index 0000000..ce17f70 --- /dev/null +++ b/2019/day23/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "time" + + intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor" + //helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + //pt := helpers.GetArgNumber(1) + prog := intcode.ReadIntCodeFile("input") + //if pt == "1" { + part1(prog) + //} else { + // part2(prog) + //} +} + +func part1(prog []int) { + var network []*intcode.Program + msgQueue := make([][]int, 255) + for k := 0; k < 50; k++ { + p := intcode.NewProgram(prog) + //p.EnableDebug() + network = append(network, p) + msgQueue = append(msgQueue, []int{}) + go func(addr int) { + for !network[addr].NeedsInput() { + time.Sleep(1) + } + network[addr].Input(0) + for { + if !network[addr].NeedsOutput() && !network[addr].NeedsInput() { + time.Sleep(1) + } else if network[addr].NeedsOutput() { + msgD := network[addr].Output() + msgQueue[msgD] = append(msgQueue[msgD], network[addr].Output()) + msgQueue[msgD] = append(msgQueue[msgD], network[addr].Output()) + fmt.Println(addr, "->", msgD, msgQueue[msgD]) + } else if network[addr].NeedsInput() { + if len(msgQueue[addr]) > 0 { + var v int + v, msgQueue[addr] = msgQueue[addr][0], msgQueue[addr][1:] + fmt.Println(addr, "<-", v) + network[addr].Input(v) + } else { + network[addr].Input(-1) + } + } + } + }(k) + go network[k].Run() + } + for len(msgQueue[255]) < 2 { + time.Sleep(1) + } + fmt.Println(msgQueue[255]) +} + +func part2(prog []int) { +} + +func getOutput(p *intcode.Program) int { + for !p.NeedsOutput() { + time.Sleep(1) + } + return p.Output() +} diff --git a/helpers/coordinate3d.go b/helpers/coordinate3d.go new file mode 100644 index 0000000..2987b29 --- /dev/null +++ b/helpers/coordinate3d.go @@ -0,0 +1,57 @@ +package aoc + +import "fmt" + +type Coordinate3d struct { + X, Y, Z int +} + +func NewCoordinate3d(x, y, z int) *Coordinate3d { + return &Coordinate3d{x, y, z} +} + +func (c *Coordinate3d) GetNorthCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X, + Y: c.Y - 1, + Z: c.Z, + } +} +func (c *Coordinate3d) GetEastCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X + 1, + Y: c.Y, + Z: c.Z, + } +} +func (c *Coordinate3d) GetSouthCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X, + Y: c.Y + 1, + Z: c.Z, + } +} +func (c *Coordinate3d) GetWestCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X - 1, + Y: c.Y, + Z: c.Z, + } +} +func (c *Coordinate3d) GetUpCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X, + Y: c.Y, + Z: c.Z + 1, + } +} +func (c *Coordinate3d) GetDownCoord() *Coordinate3d { + return &Coordinate3d{ + X: c.X, + Y: c.Y, + Z: c.Z - 1, + } +} +func (c Coordinate3d) String() string { + return fmt.Sprintf("[%d, %d, %d]", c.X, c.Y, c.Z) +}