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