From 0362762d6ff1ac99b906a178f27f8806f32b457d Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 11 Dec 2019 08:09:57 -0600 Subject: [PATCH] 2019 Day 11 Complete --- 2019/day11/input | 1 + 2019/day11/main.go | 188 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 2019/day11/input create mode 100644 2019/day11/main.go diff --git a/2019/day11/input b/2019/day11/input new file mode 100644 index 0000000..50d40a3 --- /dev/null +++ b/2019/day11/input @@ -0,0 +1 @@ +3,8,1005,8,301,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,29,1,1103,7,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,54,2,103,3,10,2,1008,6,10,1006,0,38,2,1108,7,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,91,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,114,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,136,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,158,1,1009,0,10,2,1002,18,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,187,2,1108,6,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,213,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,236,1,104,10,10,1,1002,20,10,2,1008,9,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,269,1,102,15,10,1006,0,55,2,1107,15,10,101,1,9,9,1007,9,979,10,1005,10,15,99,109,623,104,0,104,1,21102,1,932700598932,1,21102,318,1,0,1105,1,422,21102,1,937150489384,1,21102,329,1,0,1105,1,422,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,46325083227,0,1,21102,376,1,0,1106,0,422,21102,3263269927,1,1,21101,387,0,0,1105,1,422,3,10,104,0,104,0,3,10,104,0,104,0,21102,988225102184,1,1,21101,410,0,0,1105,1,422,21101,868410356500,0,1,21102,1,421,0,1106,0,422,99,109,2,21202,-1,1,1,21102,1,40,2,21102,1,453,3,21102,1,443,0,1105,1,486,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,448,449,464,4,0,1001,448,1,448,108,4,448,10,1006,10,480,1102,1,0,448,109,-2,2106,0,0,0,109,4,1201,-1,0,485,1207,-3,0,10,1006,10,503,21101,0,0,-3,22101,0,-3,1,21201,-2,0,2,21102,1,1,3,21101,0,522,0,1105,1,527,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,550,2207,-4,-2,10,1006,10,550,22102,1,-4,-4,1105,1,618,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,569,1,0,1106,0,527,22101,0,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,588,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,610,21201,-1,0,1,21101,610,0,0,105,1,485,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 diff --git a/2019/day11/main.go b/2019/day11/main.go new file mode 100644 index 0000000..383bd0b --- /dev/null +++ b/2019/day11/main.go @@ -0,0 +1,188 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strings" + "sync" + "time" + + intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor" + helpers "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +// Pass the program name and the problem part number in parameters +// Default: "input", and part 2 +func main() { + progFileName := "input" + part := 2 + if len(os.Args) > 1 { + progFileName = os.Args[1] + if len(os.Args) == 3 { + part = helpers.Atoi(os.Args[2]) + } + } + dat, err := ioutil.ReadFile(progFileName) + if err != nil { + fmt.Println("Error reading program file:", err.Error()) + os.Exit(1) + } + var prog []int + stringDat := strings.TrimSpace(string(dat)) + for _, v := range strings.Split(stringDat, ",") { + prog = append(prog, helpers.Atoi(v)) + } + // Pass the program and the problem part to solve: + solve(prog, part) +} + +func solve(inp []int, pt int) { + painter := NewPainter() + if pt == 2 { + painter.Paint(1) + } + p := intcode.NewProgram(inp) + go func() { + for { + for !p.NeedsInput() { + time.Sleep(1) + } + p.Input(painter.GetPanelColor()) + for !p.NeedsOutput() { + time.Sleep(1) + } + painter.Paint(p.Output()) + for !p.NeedsOutput() { + time.Sleep(1) + } + painter.Turn(p.Output()) + switch p.State() { + case intcode.RET_ERR: + panic(p.Error()) + case intcode.RET_DONE: + break + } + } + }() + res := p.Run() + if res == intcode.RET_DONE { + fmt.Println("DONE") + } else if res == intcode.RET_ERR { + fmt.Println("ERROR") + } + painter.Print() + fmt.Println("Painted Panels:", len(painter.panels)) + fmt.Println("Paint Used:", painter.paintCount) +} + +const ( + DIR_N = iota + DIR_E + DIR_S + DIR_W +) + +type Painter struct { + dir int + loc helpers.Coordinate + panels map[string]int + paintCount int + minX, minY int + maxX, maxY int + + panelMutex *sync.Mutex +} + +func NewPainter() *Painter { + p := Painter{ + dir: DIR_N, + loc: helpers.Coordinate{X: 0, Y: 0}, + panels: make(map[string]int), + panelMutex: &sync.Mutex{}, + } + return &p +} + +func (p *Painter) GetPanelColor() int { + if v, ok := p.panels[p.loc.String()]; ok { + return v + } + return 0 +} + +func (p *Painter) Paint(color int) { + p.panelMutex.Lock() + p.panels[p.loc.String()] = color + p.paintCount++ + p.panelMutex.Unlock() +} + +func (p *Painter) Turn(dir int) { + if dir == 0 { + p.dir-- + } else { + p.dir++ + } + if p.dir == DIR_W+1 { + p.dir = DIR_N + } else if p.dir == DIR_N-1 { + p.dir = DIR_W + } + // After turning we move forward + switch p.dir { + case DIR_N: + p.loc.Y-- + case DIR_E: + p.loc.X++ + case DIR_S: + p.loc.Y++ + case DIR_W: + p.loc.X-- + } + if p.loc.X < p.minX { + p.minX = p.loc.X + } + if p.loc.X > p.maxX { + p.maxX = p.loc.X + } + if p.loc.Y < p.minY { + p.minY = p.loc.Y + } + if p.loc.Y > p.maxY { + p.maxY = p.loc.Y + } +} + +func (p *Painter) Print() { + for x := p.minX; x <= p.maxX; x++ { + for y := p.minY; y <= p.maxY; y++ { + c := helpers.Coordinate{x, y} + if c.X == p.loc.X && c.Y == p.loc.Y { + switch p.dir { + case DIR_N: + fmt.Print("^") + case DIR_E: + fmt.Print(">") + case DIR_S: + fmt.Print("v") + case DIR_W: + fmt.Print("<") + } + } else { + p.panelMutex.Lock() + if v, ok := p.panels[c.String()]; ok { + if v == 1 { + fmt.Print("#") + } else { + fmt.Print(".") + } + } else { + fmt.Print(".") + } + p.panelMutex.Unlock() + } + } + fmt.Println() + } +}