package day06 import ( "aoc2015/aoclib" "strconv" "strings" ) func Part1(puzzle aoclib.Puzzle) interface{} { grid := make(map[int]map[int]bool) for y := 0; y < 1000; y++ { grid[y] = make(map[int]bool) for x := 0; x < 1000; x++ { grid[y][x] = false } } var values []string var command, start, end string var a_start, a_end []string var start_x, start_y, end_x, end_y int for _, instruction := range puzzle.GetInputArray() { values = strings.Split(instruction, " ") if len(values) == 4 { command = values[0] start = values[1] end = values[3] } else { command = values[0] + " " + values[1] start = values[2] end = values[4] } a_start = strings.Split(start, ",") a_end = strings.Split(end, ",") start_x, _ = strconv.Atoi(a_start[0]) start_y, _ = strconv.Atoi(a_start[1]) end_x, _ = strconv.Atoi(a_end[0]) end_y, _ = strconv.Atoi(a_end[1]) for x := start_x; x <= end_x; x++ { for y := start_y; y <= end_y; y++ { switch command { case "toggle": grid[x][y] = !grid[x][y] case "turn on": grid[x][y] = true case "turn off": grid[x][y] = false } } } } onCount := 0 for y := range grid { for x := range grid[y] { if grid[x][y] { onCount++ } } } return onCount } func Part2(puzzle aoclib.Puzzle) interface{} { grid := make(map[int]map[int]int) for y := 0; y < 1000; y++ { grid[y] = make(map[int]int) for x := 0; x < 1000; x++ { grid[y][x] = 0 } } var values []string var command, start, end string var a_start, a_end []string var start_x, start_y, end_x, end_y int for _, instruction := range puzzle.GetInputArray() { values = strings.Split(instruction, " ") if len(values) == 4 { command = values[0] start = values[1] end = values[3] } else { command = values[0] + " " + values[1] start = values[2] end = values[4] } a_start = strings.Split(start, ",") a_end = strings.Split(end, ",") start_x, _ = strconv.Atoi(a_start[0]) start_y, _ = strconv.Atoi(a_start[1]) end_x, _ = strconv.Atoi(a_end[0]) end_y, _ = strconv.Atoi(a_end[1]) for x := start_x; x <= end_x; x++ { for y := start_y; y <= end_y; y++ { switch command { case "toggle": grid[x][y] += 2 case "turn on": grid[x][y]++ case "turn off": if grid[x][y] > 0 { grid[x][y]-- } } } } } onCount := 0 for y := range grid { for x := range grid[y] { onCount += grid[x][y] } } return onCount }