diff --git a/days/11.go b/days/11.go new file mode 100644 index 0000000..62393a7 --- /dev/null +++ b/days/11.go @@ -0,0 +1,118 @@ +package days + +import ( + "fmt" + "strings" + + u "parnic.com/aoc2019/utilities" +) + +type Day11 struct { + program u.IntcodeProgram + painted map[u.Pair[int, int]]int +} + +func (d *Day11) Parse() { + d.program = u.LoadIntcodeProgram("11p") +} + +func (d Day11) Num() int { + return 11 +} + +func (d *Day11) paintHull() (int, u.Pair[int, int], u.Pair[int, int]) { + pos := u.Pair[int, int]{First: 0, Second: 0} + facing := 0 + + min := pos + max := pos + + outputState := 0 + numPainted := 1 + d.program.RunIn(func(inputStep int) int64 { + return int64(d.painted[pos]) + }, func(val int64, state u.IntcodeProgramState) { + if outputState == 0 { + outputState++ + color := int(val) + if _, exists := d.painted[pos]; !exists { + numPainted++ + } + d.painted[pos] = color + } else { + outputState = 0 + direction := val + + if direction == 0 { + facing-- + if facing == -1 { + facing = 3 + } + } else { + facing++ + if facing == 4 { + facing = 0 + } + } + + switch facing { + case 0: + pos.First-- + if pos.First < min.First { + min.First = pos.First + } + case 1: + pos.Second++ + if pos.Second > max.Second { + max.Second = pos.Second + } + case 2: + pos.First++ + if pos.First > max.First { + max.First = pos.First + } + case 3: + pos.Second-- + if pos.Second < min.Second { + min.Second = pos.Second + } + } + } + }) + + return numPainted, min, max +} + +func (d *Day11) Part1() string { + d.painted = map[u.Pair[int, int]]int{ + {First: 0, Second: 0}: 0, + } + numPainted, _, _ := d.paintHull() + + return fmt.Sprintf("Unique panels painted: %s%d%s", u.TextBold, numPainted, u.TextReset) +} + +func (d *Day11) Part2() string { + d.painted = map[u.Pair[int, int]]int{ + {First: 0, Second: 0}: 1, + } + _, min, max := d.paintHull() + + outStr := strings.Builder{} + outStr.WriteString("Registration identifier:\n") + outStr.WriteString(u.TextBold) + for x := min.First; x <= max.First; x++ { + for y := min.Second; y <= max.Second; y++ { + val, exists := d.painted[u.Pair[int, int]{First: x, Second: y}] + if exists && val == 1 { + outStr.WriteRune('█') + } else { + outStr.WriteRune(' ') + } + } + outStr.WriteRune('\n') + } + outStr.WriteString(u.TextReset) + + return outStr.String() +} diff --git a/inputs/11p.txt b/inputs/11p.txt new file mode 100644 index 0000000..31d2af3 --- /dev/null +++ b/inputs/11p.txt @@ -0,0 +1 @@ +3,8,1005,8,330,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,29,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,51,1,1103,2,10,1006,0,94,1006,0,11,1,1106,13,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,87,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,109,2,1105,5,10,2,103,16,10,1,1103,12,10,2,105,2,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,146,1006,0,49,2,1,12,10,2,1006,6,10,1,1101,4,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,183,1,6,9,10,1006,0,32,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,213,2,1101,9,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,239,1006,0,47,1006,0,4,2,6,0,10,1006,0,58,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,274,2,1005,14,10,1006,0,17,1,104,20,10,1006,0,28,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,309,101,1,9,9,1007,9,928,10,1005,10,15,99,109,652,104,0,104,1,21101,0,937263411860,1,21102,347,1,0,1105,1,451,21101,932440724376,0,1,21102,1,358,0,1105,1,451,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,0,29015167015,1,21101,0,405,0,1106,0,451,21102,1,3422723163,1,21101,0,416,0,1106,0,451,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,868389376360,1,21101,0,439,0,1105,1,451,21102,825544712960,1,1,21102,1,450,0,1106,0,451,99,109,2,21201,-1,0,1,21101,0,40,2,21102,482,1,3,21102,1,472,0,1106,0,515,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,477,478,493,4,0,1001,477,1,477,108,4,477,10,1006,10,509,1101,0,0,477,109,-2,2106,0,0,0,109,4,2101,0,-1,514,1207,-3,0,10,1006,10,532,21102,1,0,-3,22101,0,-3,1,22102,1,-2,2,21102,1,1,3,21101,551,0,0,1106,0,556,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,579,2207,-4,-2,10,1006,10,579,22102,1,-4,-4,1106,0,647,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,1,598,0,1106,0,556,22101,0,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,617,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,639,21201,-1,0,1,21102,639,1,0,105,1,514,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 \ No newline at end of file diff --git a/main.go b/main.go index 201e8f1..ee0ff88 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ var dayMap = []day{ &days.Day08{}, &days.Day09{}, &days.Day10{}, + &days.Day11{}, } func main() {