Day 8 Complete

This commit is contained in:
Brian Buller 2016-12-08 09:16:22 -06:00
parent f90d286916
commit bd285a71c4
4 changed files with 492 additions and 0 deletions

BIN
day08/day08 Executable file

Binary file not shown.

193
day08/input Normal file
View File

@ -0,0 +1,193 @@
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 5
rect 4x1
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=0 by 10
rotate column x=5 by 2
rotate column x=0 by 1
rect 9x1
rotate row y=2 by 5
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 5
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate column x=40 by 1
rotate column x=27 by 1
rotate column x=22 by 1
rotate column x=17 by 1
rotate column x=12 by 1
rotate column x=7 by 1
rotate column x=2 by 1
rotate row y=2 by 5
rotate row y=1 by 3
rotate row y=0 by 5
rect 1x3
rotate row y=2 by 10
rotate row y=1 by 7
rotate row y=0 by 2
rotate column x=3 by 2
rotate column x=2 by 1
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 5
rotate row y=1 by 3
rotate row y=0 by 3
rect 1x3
rotate column x=45 by 1
rotate row y=2 by 7
rotate row y=1 by 10
rotate row y=0 by 2
rotate column x=3 by 1
rotate column x=2 by 2
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 13
rotate row y=0 by 5
rotate column x=3 by 1
rotate column x=0 by 1
rect 4x1
rotate row y=3 by 10
rotate row y=2 by 10
rotate row y=0 by 5
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 4x1
rotate row y=3 by 8
rotate row y=0 by 5
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 4x1
rotate row y=3 by 17
rotate row y=2 by 20
rotate row y=0 by 15
rotate column x=13 by 1
rotate column x=12 by 3
rotate column x=10 by 1
rotate column x=8 by 1
rotate column x=7 by 2
rotate column x=6 by 1
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 2
rotate column x=0 by 1
rect 14x1
rotate row y=1 by 47
rotate column x=9 by 1
rotate column x=4 by 1
rotate row y=3 by 3
rotate row y=2 by 10
rotate row y=1 by 8
rotate row y=0 by 5
rotate column x=2 by 2
rotate column x=0 by 2
rect 3x2
rotate row y=3 by 12
rotate row y=2 by 10
rotate row y=0 by 10
rotate column x=8 by 1
rotate column x=7 by 3
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate row y=0 by 20
rotate column x=46 by 1
rotate row y=4 by 17
rotate row y=3 by 10
rotate row y=2 by 10
rotate row y=1 by 5
rotate column x=8 by 1
rotate column x=7 by 1
rotate column x=6 by 1
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 2
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate column x=32 by 4
rotate row y=4 by 33
rotate row y=3 by 5
rotate row y=2 by 15
rotate row y=0 by 15
rotate column x=13 by 1
rotate column x=12 by 3
rotate column x=10 by 1
rotate column x=8 by 1
rotate column x=7 by 2
rotate column x=6 by 1
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 14x1
rotate column x=39 by 3
rotate column x=35 by 4
rotate column x=20 by 4
rotate column x=19 by 3
rotate column x=10 by 4
rotate column x=9 by 3
rotate column x=8 by 3
rotate column x=5 by 4
rotate column x=4 by 3
rotate row y=5 by 5
rotate row y=4 by 5
rotate row y=3 by 33
rotate row y=1 by 30
rotate column x=48 by 1
rotate column x=47 by 5
rotate column x=46 by 5
rotate column x=45 by 1
rotate column x=43 by 1
rotate column x=38 by 3
rotate column x=37 by 3
rotate column x=36 by 5
rotate column x=35 by 1
rotate column x=33 by 1
rotate column x=32 by 5
rotate column x=31 by 5
rotate column x=30 by 1
rotate column x=23 by 4
rotate column x=22 by 3
rotate column x=21 by 3
rotate column x=20 by 1
rotate column x=12 by 2
rotate column x=11 by 2
rotate column x=3 by 5
rotate column x=2 by 5
rotate column x=1 by 3
rotate column x=0 by 4

210
day08/main.go Normal file
View File

@ -0,0 +1,210 @@
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
// First guess: 95 (too low)
func main() {
if len(os.Args) < 3 {
fmt.Println("Expected Display Width & Height as Arguments")
}
input := stdinToStringSlice()
// Test Display:
// d := CreateDisplay(7, 3)
// Prod Display:
// d := CreateDisplay(50, 6)
width := atoi(os.Args[1])
height := atoi(os.Args[2])
d := CreateDisplay(width, height)
for idx, ins := range input {
fmt.Println(idx, ins)
d.ClearScreen()
d.ProcInstruction(ins)
d.PrintScreen()
time.Sleep(time.Millisecond * 75)
}
fmt.Println("Voltage Used: ", d.GetVoltage())
}
type Display struct {
Width int
Height int
Screen [][]bool
}
func CreateDisplay(w, h int) *Display {
d := Display{Width: w, Height: h}
for i := 0; i < h; i++ {
var row []bool
for j := 0; j < w; j++ {
row = append(row, false)
}
d.Screen = append(d.Screen, row)
}
return &d
}
func (d *Display) ClearScreen() {
fmt.Print("\033[H\033[2J")
}
func (d *Display) PrintScreen() {
fmt.Print("\u250C")
for i := 0; i < d.Width; i++ {
fmt.Print("\u2500")
}
fmt.Println("\u2510")
for y := range d.Screen {
fmt.Print("\u2502")
for x := range d.Screen[y] {
if d.Screen[y][x] {
fmt.Print("\u2588")
} else {
fmt.Print(" ")
}
}
fmt.Println("\u2502")
}
fmt.Print("\u2514")
for i := 0; i < d.Width; i++ {
fmt.Print("\u2500")
}
fmt.Println("\u2518")
}
func (d *Display) SetWidth(w int) {
d.Width = w
}
func (d *Display) SetHeight(h int) {
d.Height = h
}
func (d *Display) ProcInstruction(ins string) {
pts := strings.Fields(ins)
switch pts[0] {
case "rect":
if len(pts) < 2 {
return
}
dim := strings.Split(pts[1], "x")
bldX := atoi(dim[0])
bldY := atoi(dim[1])
d.CreateRect(bldX, bldY)
case "rotate":
if len(pts) < 5 {
return
}
args := strings.Split(pts[2], "=")
switch pts[1] {
case "row":
if args[0] != "y" {
return
}
d.RotateRow(atoi(args[1]), atoi(pts[4]))
case "column":
if args[0] != "x" {
return
}
d.RotateCol(atoi(args[1]), atoi(pts[4]))
}
}
}
func (d *Display) CreateRect(x, y int) {
if y >= d.Height && x >= d.Width {
fmt.Println(" Error creating rect", x, y)
return
}
for i := 0; i < y; i++ {
for j := 0; j < x; j++ {
d.Screen[i][j] = true
}
}
}
func (d *Display) RotateRow(y, dist int) {
orig, err := d.GetRow(y)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
for i := range orig {
d.Screen[y][(i+dist)%d.Width] = orig[i]
}
}
func (d *Display) RotateCol(x, dist int) {
orig, err := d.GetCol(x)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
for i := range orig {
d.Screen[(i+dist)%d.Height][x] = orig[i]
}
}
func (d *Display) GetRow(y int) ([]bool, error) {
var ret []bool
if d.Height <= y {
return ret, errors.New("Invalid Row Requested")
}
for i := 0; i < d.Width; i++ {
ret = append(ret, d.Screen[y][i])
}
return ret, nil
}
func (d *Display) GetCol(x int) ([]bool, error) {
var ret []bool
if d.Width <= x {
return ret, errors.New("Invalid Column Requested")
}
for i := 0; i < d.Height; i++ {
ret = append(ret, d.Screen[i][x])
}
return ret, nil
}
func (d *Display) GetVoltage() int {
var ret int
for i := range d.Screen {
for j := range d.Screen[i] {
if d.Screen[i][j] {
ret++
}
}
}
return ret
}
func stdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

89
day08/problem Normal file
View File

@ -0,0 +1,89 @@
Advent of Code
--- Day 8: Two-Factor Authentication ---
You come across a door implementing what you can only assume is an implementation of two-factor authentication
after a long game of requirements telephone.
To get past the door, you first swipe a keycard (no problem; there was one on a nearby desk). Then, it displays a
code on a little screen, and you type that code on a keypad. Then, presumably, the door unlocks.
Unfortunately, the screen has been smashed. After a few minutes, you've taken everything apart and figured out how
it works. Now you just have to work out what the screen would have displayed.
The magnetic strip on the card you swiped encodes a series of instructions for the screen; these instructions are
your puzzle input. The screen is 50 pixels wide and 6 pixels tall, all of which start off, and is capable of three
somewhat peculiar operations:
 rect AxB turns on all of the pixels in a rectangle at the top-left of the screen which is A wide and B tall.
 rotate row y=A by B shifts all of the pixels in row A (0 is the top row) right by B pixels. Pixels that would
fall off the right end appear at the left end of the row.
 rotate column x=A by B shifts all of the pixels in column A (0 is the left column) down by B pixels. Pixels
that would fall off the bottom appear at the top of the column.
For example, here is a simple sequence on a smaller screen:
 rect 3x2 creates a small rectangle in the top-left corner:
###....
###....
.......
 rotate column x=1 by 1 rotates the second column down by one pixel:
#.#....
###....
.#.....
 rotate row y=0 by 4 rotates the top row right by four pixels:
....#.#
###....
.#.....
 rotate column x=1 by 1 again rotates the second column down by one pixel, causing the bottom pixel to wrap back
to the top:
.#..#.#
#.#....
.#.....
As you can see, this display technology is extremely powerful, and will soon dominate the
tiny-code-displaying-screen market. That's what the advertisement on the back of the display tries to convince you,
anyway.
There seems to be an intermediate check of the voltage used by the display: after you swipe your card, if the
screen did work, how many pixels should be lit?
Your puzzle answer was _____.
--- Part Two ---
You notice that the screen is only capable of displaying capital letters; in the font it uses, each letter is 5
pixels wide and 6 tall.
After you swipe your card, what code is the screen trying to display?
Your puzzle answer was _____________.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2016/about
. http://adventofcode.com/2016/support
. http://adventofcode.com/2016/events
. http://adventofcode.com/2016/settings
. http://adventofcode.com/2016/auth/logout
. http://adventofcode.com/2016
. http://adventofcode.com/2016
. http://adventofcode.com/2016/leaderboard
. http://adventofcode.com/2016/stats
. http://adventofcode.com/2016/sponsors
. http://adventofcode.com/2016/sponsors
. https://en.wikipedia.org/wiki/Multi-factor_authentication
. https://en.wikipedia.org/wiki/Requirement
. https://en.wikipedia.org/wiki/Chinese_whispers
. https://www.google.com/search?q=tiny+lcd&tbm=isch
. http://adventofcode.com/2016
. http://adventofcode.com/2016/day/8/input