remove ugly tmp struct
This commit is contained in:
		
							
								
								
									
										43
									
								
								03/main.go
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								03/main.go
									
									
									
									
									
								
							@@ -40,11 +40,6 @@ type entry struct {
 | 
			
		||||
	symb rune
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type tmp struct {
 | 
			
		||||
	tmp []rune
 | 
			
		||||
	col int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type coord struct {
 | 
			
		||||
	row int
 | 
			
		||||
	col int
 | 
			
		||||
@@ -65,20 +60,15 @@ func partOne() {
 | 
			
		||||
	// read the whole thing in
 | 
			
		||||
	for scanner.Scan() {
 | 
			
		||||
		line := scanner.Text()
 | 
			
		||||
		i := tmp{}
 | 
			
		||||
		acc := []rune{}
 | 
			
		||||
		for col, c := range line {
 | 
			
		||||
			switch c {
 | 
			
		||||
			case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
 | 
			
		||||
				if len(i.tmp) == 0 {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
					i.col = col
 | 
			
		||||
				} else {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
				}
 | 
			
		||||
				acc = append(acc, c)
 | 
			
		||||
			default:
 | 
			
		||||
				if len(i.tmp) != 0 {
 | 
			
		||||
					schematic[coord{row, i.col}] = entry{num: i.tmp}
 | 
			
		||||
					i.tmp = []rune{}
 | 
			
		||||
				if len(acc) != 0 {
 | 
			
		||||
					schematic[coord{row, col - len(acc)}] = entry{num: acc}
 | 
			
		||||
					acc = []rune{}
 | 
			
		||||
				}
 | 
			
		||||
				if c != '.' {
 | 
			
		||||
					schematic[coord{row, col}] = entry{symb: c}
 | 
			
		||||
@@ -86,8 +76,8 @@ func partOne() {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// check if we have a number still to save but ran out of line
 | 
			
		||||
		if len(i.tmp) != 0 {
 | 
			
		||||
			schematic[coord{row, i.col}] = entry{num: i.tmp}
 | 
			
		||||
		if len(acc) != 0 {
 | 
			
		||||
			schematic[coord{row, len(line) - len(acc)}] = entry{num: acc}
 | 
			
		||||
		}
 | 
			
		||||
		row++
 | 
			
		||||
	}
 | 
			
		||||
@@ -128,20 +118,15 @@ func partTwo() {
 | 
			
		||||
	// read the whole thing in
 | 
			
		||||
	for scanner.Scan() {
 | 
			
		||||
		line := scanner.Text()
 | 
			
		||||
		i := tmp{}
 | 
			
		||||
		acc := []rune{}
 | 
			
		||||
		for col, c := range line {
 | 
			
		||||
			switch c {
 | 
			
		||||
			case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
 | 
			
		||||
				if len(i.tmp) == 0 {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
					i.col = col
 | 
			
		||||
				} else {
 | 
			
		||||
					i.tmp = append(i.tmp, c)
 | 
			
		||||
				}
 | 
			
		||||
				acc = append(acc, c)
 | 
			
		||||
			default:
 | 
			
		||||
				if len(i.tmp) != 0 {
 | 
			
		||||
					schematic[coord{row, i.col}] = entry{num: i.tmp}
 | 
			
		||||
					i.tmp = []rune{}
 | 
			
		||||
				if len(acc) != 0 {
 | 
			
		||||
					schematic[coord{row, col - len(acc)}] = entry{num: acc}
 | 
			
		||||
					acc = []rune{}
 | 
			
		||||
				}
 | 
			
		||||
				if c == '*' { // no longer care about other symbols
 | 
			
		||||
					schematic[coord{row, col}] = entry{symb: c}
 | 
			
		||||
@@ -149,8 +134,8 @@ func partTwo() {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		// check if we have a number still to save but ran out of line
 | 
			
		||||
		if len(i.tmp) != 0 {
 | 
			
		||||
			schematic[coord{row, i.col}] = entry{num: i.tmp}
 | 
			
		||||
		if len(acc) != 0 {
 | 
			
		||||
			schematic[coord{row, len(line) - len(acc)}] = entry{num: acc}
 | 
			
		||||
		}
 | 
			
		||||
		row++
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user