Gear Ratios

Advent of Code 2023 [Day 3]

03-12-2023

You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.

It doesn’t take long to find the gondolas, but there seems to be a problem: they’re not moving.

“Aaah!”

You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. “Sorry, I wasn’t expecting anyone! The gondola lift isn’t working right now; it’ll still be a while before I can fix it.” You offer to help.

The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.

The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don’t really understand, but apparently any number adjacent to a symbol, even diagonally, is a “part number” and should be included in your sum. (Periods (.) do not count as a symbol.)

Here is an example engine schematic:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.

Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?

type PartNumber struct {
	Start int
	End   int
	Row   int
}

func (p *PartNumber) isAdjacent(row int, column int) bool {
	if p.Row < row-1 || p.Row > row+1 {
		return false
	}

	for i := p.Start; i <= p.End; i++ {
		if i >= column-1 && i <= column+1 {
			return true
		}
	}

	return false
}

type Schematic struct {
	Schema      [][]rune
	partNumbers []PartNumber
}

func (s *Schematic) IsAdjacent(pn PartNumber) bool {
	for row := pn.Row - 1; row <= pn.Row+1; row++ {
		if row < 0 || row >= len(s.Schema) {
			continue
		}

		for column := pn.Start - 1; column <= pn.End+1; column++ {
			if column < 0 || column >= len(s.Schema[row]) {
				continue
			}

			switch s.Schema[row][column] {
			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
				continue
			default:
				return true
			}
		}
	}

	return false
}

func (s *Schematic) ExtractPartNumber(pn PartNumber) uint {
	extracted := []rune{}
	for column := pn.Start; column <= pn.End; column++ {
		extracted = append(extracted, s.Schema[pn.Row][column])
	}
	number, err := strconv.Atoi(string(extracted))
	if err != nil {
		return 0
	}
	return uint(number)
}

func (s *Schematic) SumPartNumber() uint {
	var (
		sum   uint = 0
		start int  = 0
		end   int  = 0
		found bool = false
	)

	for row := 0; row < len(s.Schema); row++ {
		for column := 0; column < len(s.Schema[row]); column++ {
			if num := s.Schema[row][column] - '0'; num >= 0 && num <= 9 {
				if !found {
					start = column
					found = true
				}
				end = column
			} else if found {
				partNumber := PartNumber{
					Start: start,
					End:   end,
					Row:   row,
				}
				if s.IsAdjacent(partNumber) {
					sum += s.ExtractPartNumber(partNumber)
				}
				found = false
			}
		}

		start = 0
		end = 0
		found = false
	}

	return sum
}

var testSchema = [][]rune{
	{'4', '6', '7', '.', '.', '1', '1', '4', '.', '.'},
	{'.', '.', '.', '*', '.', '.', '.', '.', '.', '.'},
	{'.', '.', '3', '5', '.', '.', '6', '3', '3', '.'},
	{'.', '.', '.', '.', '.', '.', '#', '.', '.', '.'},
	{'6', '1', '7', '*', '.', '.', '.', '.', '.', '.'},
	{'.', '.', '.', '.', '.', '+', '.', '5', '8', '.'},
	{'.', '.', '5', '9', '2', '.', '.', '.', '.', '.'},
	{'.', '.', '.', '.', '.', '.', '7', '5', '5', '.'},
	{'.', '.', '.', '$', '.', '*', '.', '.', '.', '.'},
	{'.', '6', '6', '4', '.', '5', '9', '8', '.', '.'},
}

func TestSchematic_IsAdjacent(t *testing.T) {
	tests := []struct {
		name   string
		Schema [][]rune
		start  int
		end    int
		line   int
		want   bool
	}{
		{
			name:   "467",
			Schema: testSchema,
			start:  0,
			end:    2,
			line:   0,
			want:   true,
		},
		{
			name:   "114",
			Schema: testSchema,
			start:  5,
			end:    7,
			line:   0,
			want:   false,
		},
		{
			name:   "35",
			Schema: testSchema,
			start:  2,
			end:    3,
			line:   2,
			want:   true,
		},
		{
			name:   "633",
			Schema: testSchema,
			start:  6,
			end:    8,
			line:   2,
			want:   true,
		},
		{
			name:   "617",
			Schema: testSchema,
			start:  0,
			end:    2,
			line:   4,
			want:   true,
		},
		{
			name:   "58",
			Schema: testSchema,
			start:  7,
			end:    8,
			line:   5,
			want:   false,
		},
		{
			name:   "592",
			Schema: testSchema,
			start:  2,
			end:    4,
			line:   6,
			want:   true,
		},
		{
			name:   "755",
			Schema: testSchema,
			start:  6,
			end:    8,
			line:   7,
			want:   true,
		},
		{
			name:   "664",
			Schema: testSchema,
			start:  1,
			end:    3,
			line:   9,
			want:   true,
		},
		{
			name:   "598",
			Schema: testSchema,
			start:  5,
			end:    7,
			line:   9,
			want:   true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &engine.Schematic{
				Schema: tt.Schema,
			}
			if got := s.IsAdjacent(engine.PartNumber{
				Start: tt.start,
				End:   tt.end,
				Row:   tt.line,
			}); got != tt.want {
				t.Errorf("Schematic.isAdjacent() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestSchematic_ExtractPartNumber(t *testing.T) {
	tests := []struct {
		name   string
		Schema [][]rune
		start  int
		end    int
		row    int
		want   uint
	}{
		{
			name:   "467",
			Schema: testSchema,
			start:  0,
			end:    2,
			row:    0,
			want:   467,
		},
		{
			name:   "114",
			Schema: testSchema,
			start:  5,
			end:    7,
			row:    0,
			want:   114,
		},
		{
			name:   "35",
			Schema: testSchema,
			start:  2,
			end:    3,
			row:    2,
			want:   35,
		},
		{
			name:   "633",
			Schema: testSchema,
			start:  6,
			end:    8,
			row:    2,
			want:   633,
		},
		{
			name:   "617",
			Schema: testSchema,
			start:  0,
			end:    2,
			row:    4,
			want:   617,
		},
		{
			name:   "58",
			Schema: testSchema,
			start:  7,
			end:    8,
			row:    5,
			want:   58,
		},
		{
			name:   "592",
			Schema: testSchema,
			start:  2,
			end:    4,
			row:    6,
			want:   592,
		},
		{
			name:   "755",
			Schema: testSchema,
			start:  6,
			end:    8,
			row:    7,
			want:   755,
		},
		{
			name:   "664",
			Schema: testSchema,
			start:  1,
			end:    3,
			row:    9,
			want:   664,
		},
		{
			name:   "598",
			Schema: testSchema,
			start:  5,
			end:    7,
			row:    9,
			want:   598,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &engine.Schematic{
				Schema: tt.Schema,
			}
			if got := s.ExtractPartNumber(engine.PartNumber{
				Start: tt.start,
				End:   tt.end,
				Row:   tt.row,
			}); got != tt.want {
				t.Errorf("Schematic.ExtractPartNumber() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestSchematic_SumPartNumber(t *testing.T) {
	t.Run("part1-test", func(t *testing.T) {
		s := &engine.Schematic{
			Schema: testSchema,
		}
		if gotSum := s.SumPartNumber(); gotSum != 4361 {
			t.Errorf("Schematic.SumPartNumber() = %v, want 4361", gotSum)
		}
	})

}

The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.

You don’t seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled “help”, so you pick it up and the engineer answers.

Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You’re going so slowly that you haven’t even left the station. You exit the gondola.

The missing part wasn’t the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.

This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.

Consider the same engine schematic again:

467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.

What is the sum of all of the gear ratios in your engine schematic?

func (s *Schematic) ExtractGearRatio(gearRow int, gearColumn int) uint {
	gears := []PartNumber{}

	for _, pn := range s.partNumbers {
		if pn.isAdjacent(gearRow, gearColumn) {
			gears = append(gears, pn)
		}
	}

	if len(gears) == 2 {
		return s.ExtractPartNumber(gears[0]) * s.ExtractPartNumber(gears[1])
	}

	return 0
}

func (s *Schematic) SumGearRatio() uint {
	var (
		sum   uint = 0
		start int  = 0
		end   int  = 0
		found bool = false
	)

	for row := 0; row < len(s.Schema); row++ {
		for column := 0; column < len(s.Schema[row]); column++ {
			if num := s.Schema[row][column] - '0'; num >= 0 && num <= 9 {
				if !found {
					start = column
					found = true
				}
				end = column
			} else if found {
				partNumber := PartNumber{
					Start: start,
					End:   end,
					Row:   row,
				}
				s.partNumbers = append(s.partNumbers, partNumber)
				found = false
			}
		}

		start = 0
		end = 0
		found = false
	}

	for row := 0; row < len(s.Schema); row++ {
		for column := 0; column < len(s.Schema[row]); column++ {
			if s.Schema[row][column] == '*' {
				sum += s.ExtractGearRatio(row, column)
			}
		}
	}

	return sum
}

func TestSchematic_SumGearRatio(t *testing.T) {
	t.Run("part2-test", func(t *testing.T) {
		s := &engine.Schematic{
			Schema: testSchema,
		}
		if got := s.SumGearRatio(); got != 467835 {
			t.Errorf("Schematic.SumGearRatio() = %v, want 467835", got)
		}
	})
}

func main() {
	file, err := os.Open("input.txt")
	if err != nil {
		log.Println(err.Error())
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)

	var (
		engineSchematic *(engine.Schematic) = &engine.Schematic{
			Schema: [][]rune{},
		}
	)

	for scanner.Scan() {
		runes := []rune(scanner.Text())
		runes = append(runes, '.')
		engineSchematic.Schema = append(engineSchematic.Schema, runes)
	}

	if err := scanner.Err(); err != nil {
		log.Println(err.Error())
	}
	log.Printf(
		"sum: %d, %d\n",
		engineSchematic.SumPartNumber(),
		engineSchematic.SumGearRatio(),
	)
}

If you’re new to Advent of Code, it’s an annual event that takes place throughout December, featuring a series of programming puzzles that get progressively more challenging as Christmas approaches.

Click to show the input
......124..................418.......587......770...........672.................564............................438..........512......653....
665/...*......................*599.....*.983......794*..140..*[email protected]*....................445........*......*.........709.....*...
.......246.....581......701..........108....%.532........../.73..699...927............................*....579.354.464..............298..86.
........................*.....@...............%........$............+.........167..................408............................$..*......
....914......335.......513..245....106=...............974................749.....*.702.......589........803........*176..386.....631..340...
....*.......*[email protected]......*..../.362...$......................*159.381.........................
..620.....430.....612.507.........365.....................335../[email protected]............
.....................*...........*..................470.........................889...........*[email protected]$.329..334............86...
..........324...............431..58..533-../..-...../......*405.................$.............47..474..............*......*.......930.*.....
............/.....*350....................400.502...............$...........168.......855.635....................258.......794...+.....846..
........................560...72.945..............866..........783..328....*....116......*...........179..904........682$..........333......
.....674...........152....*....*....*815.........*........$609.............737../................583*........*.84..............767*.........
..../......55@....+........645.914................987..................*..........972.........#.......80..750........588................=260
.....................349&...................../.................754.407..203*720./.......207...14...............=88...+...767...............
.........*824.............890.......269....893..271*139..645....*...................233...%................428...........*.........79.......
..........................#............*.................@.../...316...844.............*[email protected]*......*974.....182...............
....*.....50.......671+.................267........634*....417............-.598.....531....891................331................358.....341
.883.561..*....428.........../[email protected]../716.......*456.....=....*........$..............................607....
...........835..*..796*............*..............321......612*.......................299..203....962..431..........277.......40......$.....
......+591.....916.....294.........446..111......................237*77.....&........................-................*...150*....*......873
....%..................................*.....................819............522.................922................738.........214.595..&...
...552.........*...............+545.....627...........601......*..801..............867.....954....*.984.....752..........*830...............
............276...939.............................979....*.........*........866*.....*.......*..963.-.................172...................
278................*[email protected]*[email protected]=.......736...456...107............796.@668..................#......
...*...431.................616..............................................79..................651.806.....%.............554.........740...
.969...-............................-..........721.......555.657....+.........*....#....704........*.................556...*....196.........
................*228..........312.201.....490...%.........*..#...815........896..417.....=....890.....274....884.683*.......327...*.........
[email protected].............*........393*....=........715..............535...................*.........................529.....741.....#...
.......386........244......196...............815...........869....+580...*...................71........654...454..346=.$.............909....
....................................................265...$.............738..401...984...........265.....*......*...............-...........
.......=.....184....148....14.........685...990..................80..=....../........$.......511....%.....424..400......184..551............
.....71......*[email protected].........&..$.....573*613.....*.677.......#.......299..&.......................933.....................
...=.......142...917...-989....76*....230...*....105.............920.....+......371.......*............660..692.........553*........%.......
341................&.....................=.29.............643.82......*...714............222....934......*[email protected]..+.....
...............874...129.......................739*971.......*.......176.............3.@...........*..219..40..........#.............168....
.....179..............*...741.......524...................................757.=633..*...136......63........*..........399...................
.......*....315....307....*.....%..*.............718......371....=....654*..........89...................194........................+.23....
.....737.........&........540.253...80...273......*.........&...773............492.................722........113....970..=195....702.......
..............869..........................*.539...434...............393*933......*........679.874..%......=....*...........................
............................$....148......43...*[email protected]........=......./..388..920......423.........-.......
..118*773.142....%565.......397....*.........352..#..........217......................*....865......257..................*........421.415...
............&.........898.............607..........897...631.................787...840.......*..............684.........34.359.........*....
........995....235.....@.........#.......*.932*.............*73...940..997..#..........&.&...942...$.727..........115.........*122....380...
................-................131...........265...827...........*....*............797.490.....845..*..........#....+823..................
............96.......................383.......................53.292....19...536......................42..668..................579......666
...-..487............680..&...*45...&........801.............*.*.................-........374.....................128..109.......%....*.....
.611.+..............*.....151................=......739....622..572................103...........683.....245..748*....*............298.67...
........*735......911...................562........@.........................458.....*...753........................275.362*................
.....683......702.....736.230.....457.........................13................*..126...........458........................890.........992.
...............*......*..........*.......499........10.........+...227.227...542..........................167....661....................*...
.............691.7....135.62..157..570..*....304......*...........*......*................$589....#946....*.........*............132....190.
........687.......*.........*............768..-...453..643.........844....706...%......................509........767......*................
...........*....485......859...........*.........................................92...268.........193.....................385.....991*722...
...-.....18..................217......853................................28..............*........@.......+......302........................
...103......60=..*352...........*916........351.....347..=..452.810....................304..........539.346.......*....................*....
......................610..........................=....990.....=...819....*496..797.................*........946..44..................261..
.......630.............*........882.........................173....*...............*[email protected].....................
.......-......-...........@..=.....*841.....812.......515......*....713....+.....566....*...................*......344..297....356.430..%...
.........*482..453.......279.554./............#..320..*.....................671......873........&...637$.....413........@.........*.....906.
....263...........................861......*....*.....908......365....123.......494.............134.................808.......*.....*.......
..........465.....520%.....................432.76..........160........*......................26.......218....14.......*......598.874.844....
...417......%.............138.....................$..............84..............272...573.....*.296.*................585...................
...*................596...&......................783.....992..........*....982.....*.........857..*...314...797..265........*....*..547.#...
....260........75....*...........389....616=.........5.....*.....695..427..*....780....-425......872..........*...*.......49....599.....19..
..............+.......389...........*33.........596.......600......*......67......................................567.......................
...802............................$.....302....*.....-93........434............$........554../339..............................277..........
.....*....................$.....822.....*.....89...........233..........602.....911.....#..........958............475......773*.........%989
..849.............228..868..217......679.......................99*...../............................*.......................................
.........253.........*......*....................643..............796......-200...355..469.........174......=........174....279.638.........
295*22..*............664...462.-238...................&765.........................*........................241..............$.......%......
.......937.....25....................422.264.................244...........628...340..................106.........................551.....82
897*.........../..60.......361.......*......#.....164.........*.......804*....%..........670*194........#.......#..........83...............
....754............*.................51..47.......*.........487...585.....202........838..................28...734...*457..............427..
............155...705......................*....912.....887..........................*....*875...........*........................&.........
..952.621......*[email protected]#...+........530.223.............456......462...257*100......763......
..........=..905........................892...............................262..85..........#..................-../.....................671..
.........994...............476.............%.665...524...53*........939*........*..........703..497.........186............=.567........*...
154...............$412......&..........-.....&.....-........41.109......282......676...........%....&[email protected]...*[email protected].
........+130..................296.....308................@.....*...902..................................77....833.....%.....932.102.48......
................407%.%685......*..........927.=222........426.450...*........12.....82...../..570................*...798..........*.........
..825.....................923.429......#..*........#630...........409..314*..........*...569.....*....273..648...961.............279........
...=..827=..........293.....#.........875.401.............457.433..........690.....600............929......*..............*245.......93..94.
.............557.......*.......................52........@..........284.......................450......................986..........*.......
........................463.....583..708...........................+........101....834.445.......*.....336...................694...333......
....556....923................./......*..433.....182....181.........................*..*......492..598.*............260.....*...............
......*.....*....430...............960....*...+..&.......*[email protected]...........*..468...420...........288.............
...140...682......*..945...150............7.654....+83.941...........*......@................#....7.780.......*......922.........334........
.............365..49.......$...590............................608.........503......./......36.....*.......74...340.....*....................
..........@..*.......870.-.....*...284*556................288..*...@.............$.526............587....*...........387....................
.......188....626.$......372.733.............................*..97.466.....776.541......................413.950..........696.162............
...../.............755...........62......99...............224...............-.........333......................*..337.....*....#......%233..
442..7.......=...................#...........875*705.548..............963...............*......180.....581...350.....*....513...............
..............377....................................*.........#......*.........291......981..*...........*..........74..............&......
......895.........644...................613.......540...........756.............%............390.263....754........#.....713-.....450.......
...85*....$.............297......%.........*..........156............974./870....................*...........12....426.................483..
.......774............#..*.....872.............361......-...196..................849...........419............*.........221....667..........
...................961....604...........644....*...............*........../927......*......503................124........-......../...238...
....370.............................531*......174.693.........349..................495.......*....925.......................................
.....+...104....582...602*604...123..................*[email protected]@....656......%...........
140........*...@...............*.........-........592..*....................806..692.....511...............755..........*......917...636....
..........905.......462.....+...655......860..150.....800..903.......8*920..........*867.@...................=..........269.................
....582..................217........842.........*...........*..................912...............551...615.......-..........343*129....+....
....*........................./.....................752..347...275...127.@........*.....804.....#....+.#.........252...845............671...
..862..........38..........293....429...@............*.......*...#.....*..245....330.......*18.....182.....$538..........&..725.............
.................=.............46*....696.............581.664.........608...........................................%.......=...............
...*724.....977.............................../................565...............#15...............................782.........359....$.....
529.....373.*............198.983.....980..559..592...100.579......*889..145..839..............790.....496....193......................545...
.........*..560..769.......*.....775...*.............*[email protected]................*...%..820.....*......*.....*.............245...160.........
[email protected].......*...725.687....761......%...............%..423.773..*.....315....765..69.................*...*..........
.851*.....619...333..............907..........*.........512....536..........98.........434....................48.613.973..941...554..751....
....................*564....122.............608........$.........-.........................48.........435.......*.......*............&......
...........165*967...........-..799.186.938......@.615...317........................630...........551*................345...................
....968............281.546.......*.....*.......225.......*...162....372.........&[email protected]*........
.......*.......%.......#.......226....................341.....*......*.............................341.........*...........989&.....321..618
....884..554.163...%...............*423.233.................653...557....$....910...................*...........109....772..................
..................854......167..560......@.....311...958..............492.....*.....*[email protected]%...........*..................
....852.815.....9.................................*....#...../..941.........960..494........56.375.....................50..+......=...106...
....*......*461.&.......739....$..........=.$562.276.......408.....=.....................=...%.................769.........893..463..*......
.133..................-........797.....313...........................................20.214.....357..776.471......*[email protected]....
.......14........899...845..........*..........+.....46........634........914.....84../...............*.....*780.......878..%36.435.........
542.....*...........$........*833...257..329-.147...........+........150..*......*.......907...........429.........................*....#...
.....517..799.44@.........230..........................477.579...........836......839.....-.....................964.704.............194..310
298.......*....................307..800......346.65.....*[email protected]........&442.............*..*......................
...........510...+.....837.237*.........../.....*.....270.....818$..+..........27....*.................163.....140....647....764.163........
................181...*..........536#....335......................................610..170...............*.-....................*.......&...
.......................832........................#........&........611.........................&55...428..472....586......111........768...
.......763.....................................461.........381......-............566..814.....*.....................*........./............8
.............+.......................318............695............................=..%....323.756............711..663............827.......
...........526....=....675...353&[email protected]#......*..................+.62...880+..................631.......$......................-.118...
..................655....*....................795..30...922*.......978...+.&.........539...........-....719.................599.............
......2...574%..................#.698...475.....*...........652./...........464.163$...*..338*966.........................../.....534..386..
......*.......................404..#............747...703........231...-...............................................................*....
.......906.................................&575.........&....457.......633...395..761...355.#780....3+......799+...............496...264....
311............967.682............%[email protected].......@....*.....*.............................487.........*..........
..........@.......*......925....376....&...419......=.............*..............20..952.111/....648.&........748................834..706...
..443....940.............*....................*[email protected]........*........106.283............
..............397.........803...84............627..........704.983..........*................522............................*....541........
.....32....$.....#...643*..............116........./905......*..../...........311......811$.*........*890..........924..670........=....882.
......*.....81.....*.....636.......317...*...................899.............*....*698............626....................-..+..@.......*....
.......877......256.714...................825.........458....................869..............................54............28.823..110.....