clean things up quite a bit
This commit is contained in:
		
							
								
								
									
										128
									
								
								03/main.go
									
									
									
									
									
								
							
							
						
						
									
										128
									
								
								03/main.go
									
									
									
									
									
								
							@@ -8,8 +8,8 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func mustAtoi(line string) int {
 | 
			
		||||
	i, _ := strconv.Atoi(line)
 | 
			
		||||
func mustAtoi(line []rune) int {
 | 
			
		||||
	i, _ := strconv.Atoi(string(line))
 | 
			
		||||
	return i
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -36,8 +36,13 @@ func makeScanner(test bool) *bufio.Scanner {
 | 
			
		||||
type schematic map[string]entry
 | 
			
		||||
 | 
			
		||||
type entry struct {
 | 
			
		||||
	num  string
 | 
			
		||||
	symb string
 | 
			
		||||
	num  []rune
 | 
			
		||||
	symb rune
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type tmp struct {
 | 
			
		||||
	tmp []rune
 | 
			
		||||
	col int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func key(row, col int) string {
 | 
			
		||||
@@ -46,7 +51,7 @@ func key(row, col int) string {
 | 
			
		||||
 | 
			
		||||
func isSymbol(s schematic, row, col int) bool {
 | 
			
		||||
	if c, ok := s[key(row, col)]; ok {
 | 
			
		||||
		return c.symb != ""
 | 
			
		||||
		return c.symb != 0
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
@@ -59,18 +64,9 @@ func partOne() {
 | 
			
		||||
	// read the whole thing in
 | 
			
		||||
	for scanner.Scan() {
 | 
			
		||||
		line := scanner.Text()
 | 
			
		||||
		i := struct {
 | 
			
		||||
			tmp []rune
 | 
			
		||||
			col int
 | 
			
		||||
		}{}
 | 
			
		||||
		i := tmp{}
 | 
			
		||||
		for col, c := range line {
 | 
			
		||||
			switch c {
 | 
			
		||||
			case '.':
 | 
			
		||||
				if len(i.tmp) != 0 {
 | 
			
		||||
					schematic[key(row, i.col)] = entry{num: string(i.tmp)}
 | 
			
		||||
					i.tmp = []rune{}
 | 
			
		||||
				}
 | 
			
		||||
				continue
 | 
			
		||||
			case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
 | 
			
		||||
				if len(i.tmp) == 0 {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
@@ -78,20 +74,19 @@ func partOne() {
 | 
			
		||||
				} else {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
				}
 | 
			
		||||
				continue
 | 
			
		||||
			default:
 | 
			
		||||
				if len(i.tmp) != 0 {
 | 
			
		||||
					schematic[key(row, i.col)] = entry{num: string(i.tmp)}
 | 
			
		||||
					schematic[key(row, i.col)] = entry{num: i.tmp}
 | 
			
		||||
					i.tmp = []rune{}
 | 
			
		||||
				}
 | 
			
		||||
				schematic[key(row, col)] = entry{symb: string(c)}
 | 
			
		||||
				continue
 | 
			
		||||
				if c != '.' {
 | 
			
		||||
					schematic[key(row, col)] = entry{symb: c}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// check if we have a number still to save but ran out of line
 | 
			
		||||
		if len(i.tmp) != 0 {
 | 
			
		||||
			schematic[key(row, i.col)] = entry{num: string(i.tmp)}
 | 
			
		||||
			i.tmp = []rune{}
 | 
			
		||||
			schematic[key(row, i.col)] = entry{num: i.tmp}
 | 
			
		||||
		}
 | 
			
		||||
		row++
 | 
			
		||||
	}
 | 
			
		||||
@@ -99,24 +94,23 @@ func partOne() {
 | 
			
		||||
	// now, map back over the schematics and find the parts
 | 
			
		||||
	for row := 0; row <= 139; row++ {
 | 
			
		||||
		for col := 0; col <= 139; col++ {
 | 
			
		||||
			if num, ok := schematic[key(row, col)]; ok && num.num != "" {
 | 
			
		||||
				part := mustAtoi(num.num)
 | 
			
		||||
			if entry, ok := schematic[key(row, col)]; ok && entry.symb == 0 {
 | 
			
		||||
				part := mustAtoi(entry.num)
 | 
			
		||||
				if isSymbol(schematic, row, col-1) {
 | 
			
		||||
					// look behind
 | 
			
		||||
					sum += part
 | 
			
		||||
				} else if isSymbol(schematic, row, col+len(num.num)) {
 | 
			
		||||
				}
 | 
			
		||||
				if isSymbol(schematic, row, col+len(entry.num)) {
 | 
			
		||||
					// look ahead
 | 
			
		||||
					sum += part
 | 
			
		||||
				} else {
 | 
			
		||||
					for i := -1; i < len(num.num)+1; i++ {
 | 
			
		||||
				}
 | 
			
		||||
				for i := -1; i < len(entry.num)+1; i++ {
 | 
			
		||||
					// check row above and below
 | 
			
		||||
					if isSymbol(schematic, row-1, col+i) {
 | 
			
		||||
						sum += part
 | 
			
		||||
							break
 | 
			
		||||
						} else if isSymbol(schematic, row+1, col+i) {
 | 
			
		||||
							sum += part
 | 
			
		||||
							break
 | 
			
		||||
					}
 | 
			
		||||
					if isSymbol(schematic, row+1, col+i) {
 | 
			
		||||
						sum += part
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
@@ -133,10 +127,7 @@ func partTwo() {
 | 
			
		||||
	// read the whole thing in
 | 
			
		||||
	for scanner.Scan() {
 | 
			
		||||
		line := scanner.Text()
 | 
			
		||||
		i := struct {
 | 
			
		||||
			tmp []rune
 | 
			
		||||
			col int
 | 
			
		||||
		}{}
 | 
			
		||||
		i := tmp{}
 | 
			
		||||
		for col, c := range line {
 | 
			
		||||
			switch c {
 | 
			
		||||
			case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
 | 
			
		||||
@@ -146,63 +137,68 @@ func partTwo() {
 | 
			
		||||
				} else {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
				}
 | 
			
		||||
				continue
 | 
			
		||||
			default:
 | 
			
		||||
				if len(i.tmp) != 0 {
 | 
			
		||||
					schematic[key(row, i.col)] = entry{num: string(i.tmp)}
 | 
			
		||||
					schematic[key(row, i.col)] = entry{num: i.tmp}
 | 
			
		||||
					i.tmp = []rune{}
 | 
			
		||||
				}
 | 
			
		||||
				if c == '*' { // no longer care about other symbols
 | 
			
		||||
					schematic[key(row, col)] = entry{symb: string(c)}
 | 
			
		||||
					schematic[key(row, col)] = entry{symb: c}
 | 
			
		||||
				}
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// check if we have a number still to save but ran out of line
 | 
			
		||||
		if len(i.tmp) != 0 {
 | 
			
		||||
			schematic[key(row, i.col)] = entry{num: string(i.tmp)}
 | 
			
		||||
			i.tmp = []rune{}
 | 
			
		||||
			schematic[key(row, i.col)] = entry{num: i.tmp}
 | 
			
		||||
		}
 | 
			
		||||
		row++
 | 
			
		||||
	}
 | 
			
		||||
	gears := map[string][]int{}
 | 
			
		||||
	// map back over the schematics and find the gears
 | 
			
		||||
 | 
			
		||||
	gears := map[string]int{}
 | 
			
		||||
	sum := 0
 | 
			
		||||
	// map back over the schematics and find the gears.
 | 
			
		||||
	// there are only ever 1 or 2 parts attached to a gear.
 | 
			
		||||
	for row := 0; row <= 139; row++ {
 | 
			
		||||
		for col := 0; col <= 139; col++ {
 | 
			
		||||
			if num, ok := schematic[key(row, col)]; ok && num.num != "" {
 | 
			
		||||
				part := mustAtoi(num.num)
 | 
			
		||||
			if entry, ok := schematic[key(row, col)]; ok && entry.symb == 0 {
 | 
			
		||||
				offset := len(entry.num)
 | 
			
		||||
				part := mustAtoi(entry.num)
 | 
			
		||||
				if isSymbol(schematic, row, col-1) {
 | 
			
		||||
					if schematic[key(row, col-1)].symb == "*" {
 | 
			
		||||
						gears[key(row, col-1)] = append(gears[key(row, col-1)], part)
 | 
			
		||||
					}
 | 
			
		||||
				} else if isSymbol(schematic, row, col+len(num.num)) {
 | 
			
		||||
					if schematic[key(row, col+len(num.num))].symb == "*" {
 | 
			
		||||
						gears[key(row, col+len(num.num))] = append(gears[key(row, col+len(num.num))], part)
 | 
			
		||||
					}
 | 
			
		||||
					k := key(row, col-1)
 | 
			
		||||
					if gears[k] != 0 {
 | 
			
		||||
						sum += gears[k] * part
 | 
			
		||||
					} else {
 | 
			
		||||
					for i := -1; i < len(num.num)+1; i++ {
 | 
			
		||||
						// check row above and below
 | 
			
		||||
						gears[k] = part
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				if isSymbol(schematic, row, col+offset) {
 | 
			
		||||
					k := key(row, col+offset)
 | 
			
		||||
					if gears[k] != 0 {
 | 
			
		||||
						sum += gears[k] * part
 | 
			
		||||
					} else {
 | 
			
		||||
						gears[k] = part
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				for i := -1; i < offset+1; i++ {
 | 
			
		||||
					if isSymbol(schematic, row-1, col+i) {
 | 
			
		||||
							if schematic[key(row-1, col+i)].symb == "*" {
 | 
			
		||||
								gears[key(row-1, col+i)] = append(gears[key(row-1, col+i)], part)
 | 
			
		||||
						k := key(row-1, col+i)
 | 
			
		||||
						if gears[k] != 0 {
 | 
			
		||||
							sum += gears[k] * part
 | 
			
		||||
						} else {
 | 
			
		||||
							gears[k] = part
 | 
			
		||||
						}
 | 
			
		||||
							break
 | 
			
		||||
						} else if isSymbol(schematic, row+1, col+i) {
 | 
			
		||||
							if schematic[key(row+1, col+i)].symb == "*" {
 | 
			
		||||
								gears[key(row+1, col+i)] = append(gears[key(row+1, col+i)], part)
 | 
			
		||||
					}
 | 
			
		||||
							break
 | 
			
		||||
					if isSymbol(schematic, row+1, col+i) {
 | 
			
		||||
						k := key(row+1, col+i)
 | 
			
		||||
						if gears[k] != 0 {
 | 
			
		||||
							sum += gears[k] * part
 | 
			
		||||
						} else {
 | 
			
		||||
							gears[k] = part
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	sum := 0
 | 
			
		||||
	for _, g := range gears {
 | 
			
		||||
		if len(g) == 2 {
 | 
			
		||||
			sum += g[0] * g[1]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(sum)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user