Created
November 13, 2017 21:43
-
-
Save phedoreanu/d7c6bd3f3ab60d12109de1fb105ada9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"bufio" | |
"bytes" | |
"strings" | |
"strconv" | |
) | |
type Pixel struct { | |
X, Y int | |
Color int | |
} | |
func (p Pixel) String() string { | |
return fmt.Sprintf("%d", p.Color) | |
} | |
type Bitmap struct { | |
Pixels [][]*Pixel | |
} | |
func (b *Bitmap) Clear(c int) { | |
for x := 0; x < len(b.Pixels); x++ { | |
for y := 0; y < len(b.Pixels[x]); y++ { | |
b.Pixels[x][y].Color = c | |
} | |
} | |
} | |
func (b *Bitmap) FillPixel(x...int) { | |
b.Pixels[x[1]-1][x[0]-1].Color = x[2] | |
} | |
func (b *Bitmap) FillV(x...int) { | |
//b.Pixels[x[1]-1][x[0]-1].Color = x[2] | |
} | |
func (b *Bitmap) FillH(x...int) { | |
//for i := 0; i < | |
//b.Pixels[x[1]-1][x[0]-1].Color = x[2] | |
} | |
func (b Bitmap) String() string { | |
s := "" | |
for x := 0; x < len(b.Pixels); x++ { | |
for y := 0; y < len(b.Pixels[x]); y++ { | |
s += b.Pixels[x][y].String() | |
} | |
s += fmt.Sprintln() | |
} | |
return s | |
} | |
func main() { | |
var b *Bitmap | |
data, _ := ioutil.ReadFile("input.txt") | |
if len(data) == 0 { | |
return | |
} | |
scanner := bufio.NewScanner(bytes.NewReader(data)) | |
for scanner.Scan() { | |
switch cmd := scanner.Text()[:1]; cmd { | |
case "I": | |
args := strings.Split(scanner.Text()[2:], " ") | |
dims := make([]int, 0) | |
for _, i := range args { | |
dims = append(dims, parseInt(i)) | |
} | |
pixels := make([][]*Pixel, dims[0]) | |
for x := 0; x < len(pixels); x++ { | |
pixels[x] = make([]*Pixel, dims[1]) | |
for y := 0; y < len(pixels[x]); y++ { | |
pixels[x][y] = &Pixel{x, y, 0} | |
} | |
} | |
b = &Bitmap{Pixels: pixels} | |
case "C": | |
color := 0 | |
if scanner.Text()[1:] != "" { | |
color = parseInt(scanner.Text()[1:]) | |
} | |
b.Clear(color) | |
case "P": | |
args := strings.Split(scanner.Text()[2:], " ") | |
intArgs := make([]int, 0) | |
for _, i := range args { | |
intArgs = append(intArgs, parseInt(i)) | |
} | |
b.FillPixel(intArgs...) | |
case "V": | |
args := strings.Split(scanner.Text()[2:], " ") | |
intArgs := make([]int, 0) | |
for _, i := range args { | |
intArgs = append(intArgs, parseInt(i)) | |
} | |
b.FillV(intArgs...) | |
case "H": | |
args := strings.Split(scanner.Text()[2:], " ") | |
intArgs := make([]int, 0) | |
for _, i := range args { | |
intArgs = append(intArgs, parseInt(i)) | |
} | |
b.FillH(intArgs...) | |
case "S": | |
fmt.Println(b) | |
/*default: | |
fmt.Println("11111\n11421\n11411\n11411\n33331")*/ | |
} | |
} | |
} | |
func parseInt(s string) (i int) { | |
i, err := strconv.Atoi(strings.Trim(s, " ")) | |
if err != nil { | |
panic(err) | |
} | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func Example() { | |
main() | |
//Output: | |
//11111 | |
//11421 | |
//11411 | |
//11411 | |
//33331 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment