solve day 3

This commit is contained in:
Gabriel Augendre 2023-12-03 16:19:22 +01:00
parent 9a293cad16
commit 7a5b5306a1
4 changed files with 310 additions and 0 deletions

135
2023/day03_engine.go Normal file
View file

@ -0,0 +1,135 @@
package _023
import (
"bufio"
"fmt"
"io"
"regexp"
"slices"
"strconv"
)
type Day03Number struct {
lineNo int
start int
end int
value int
}
type Day03Symbol struct {
row, col int
}
func Day03Part1(input io.Reader) (int, error) {
numberReg := regexp.MustCompile(`\d+`)
symbolReg := regexp.MustCompile(`[^\d.]`)
scanner := bufio.NewScanner(input)
var symbols []Day03Symbol
var numbers []Day03Number
lineNo := 0
width := 0
for scanner.Scan() {
line := scanner.Text()
width = len(line)
lineNumbers := numberReg.FindAllStringIndex(line, -1)
lineSymbols := symbolReg.FindAllStringIndex(line, -1)
for _, symbol := range lineSymbols {
symbols = append(symbols, Day03Symbol{lineNo, symbol[0]})
}
for _, number := range lineNumbers {
value, err := strconv.Atoi(line[number[0]:number[1]])
if err != nil {
return 0, fmt.Errorf("parsing number: %w", err)
}
numbers = append(numbers, Day03Number{lineNo, number[0], number[1], value})
}
lineNo++
}
sum := 0
for _, number := range numbers {
if isAdjacent(number, symbols, lineNo, width) {
sum += number.value
}
}
return sum, nil
}
func isAdjacent(number Day03Number, symbols []Day03Symbol, height, width int) bool {
minRow := max(number.lineNo-1, 0)
maxRow := min(number.lineNo+1, height)
minCol := max(number.start-1, 0)
maxCol := min(number.end, width)
for row := minRow; row <= maxRow; row++ {
for col := minCol; col <= maxCol; col++ {
if slices.Contains(symbols, Day03Symbol{row, col}) {
return true
}
}
}
return false
}
func Day03Part2(input io.Reader) (int, error) {
numberReg := regexp.MustCompile(`\d+`)
symbolReg := regexp.MustCompile(`\*`)
scanner := bufio.NewScanner(input)
var (
potentialGears []Day03Symbol
numbers []Day03Number
)
lineNo := 0
width := 0
for scanner.Scan() {
line := scanner.Text()
width = len(line)
lineNumbers := numberReg.FindAllStringIndex(line, -1)
lineSymbols := symbolReg.FindAllStringIndex(line, -1)
for _, symbol := range lineSymbols {
potentialGears = append(potentialGears, Day03Symbol{lineNo, symbol[0]})
}
for _, number := range lineNumbers {
value, err := strconv.Atoi(line[number[0]:number[1]])
if err != nil {
return 0, fmt.Errorf("parsing number: %w", err)
}
numbers = append(numbers, Day03Number{lineNo, number[0], number[1], value})
}
lineNo++
}
sum := 0
for _, potentialGear := range potentialGears {
sum += gearRatio(potentialGear, numbers, lineNo, width)
}
return sum, nil
}
func gearRatio(gear Day03Symbol, numbers []Day03Number, height int, width int) int {
minRow := max(gear.row-1, 0)
maxRow := min(gear.row+1, height)
minCol := max(gear.col-1, 0)
maxCol := min(gear.col+1, width)
var ratio []int
for row := minRow; row <= maxRow; row++ {
for col := minCol; col <= maxCol; col++ {
for _, number := range numbers {
if row == number.lineNo && col >= number.start && col < number.end {
ratio = append(ratio, number.value)
col = number.end
}
}
}
}
if len(ratio) != 2 {
return 0
}
return ratio[0] * ratio[1]
}

25
2023/day03_engine_test.go Normal file
View file

@ -0,0 +1,25 @@
package _023
import (
"testing"
)
func TestDay03Part1(t *testing.T) {
tests := []testCase{
{"inputs/day03_test1", 4361},
{"inputs/day03", 529618},
}
for _, test := range tests {
t.Run(test.filename, check(test, Day03Part1))
}
}
func TestDay03Part2(t *testing.T) {
tests := []testCase{
{"inputs/day03_test1", 467835},
{"inputs/day03", 77509019},
}
for _, test := range tests {
t.Run(test.filename, check(test, Day03Part2))
}
}

140
2023/inputs/day03 Normal file
View file

@ -0,0 +1,140 @@
........................617.........123...........341.........................293..................38..19.753..................533..........
565.......................-..............951.....+..........354.....697.58....*.....941............*.....*.........+....529....&.....36.....
....1.....225...73...................472.......................-....*......920..999.......646..771.433......407..405.....*.......426*.......
.....*....*........./227..-113........@...825/.....348...881......603...........%....793...=............235*..............472.........82.941
..360..432..997....................................*.....=............62...702......*..............................273..................*...
...........&.......833.489.......@.........176...895............503.......$.......493...............929...............*.302....492.526......
....................*.....+....85.......................601............................................*386......*...96...........*....*613.
.....650.360+...#..589..............................221*..............927...........941..404..+669..............823.................360.....
.527...........919.................799.....................&............*...............-..............485............-..754................
....&....*187........./149............*.....................653........84......120...............-.....=....581...574.76....*287..968.......
.......26..................497........254........#..682..........$.236..........................111............*...=.............*..........
....79.......394.......112.-...762.............847....*........450.*...$...495&........$.49.............#.....213...................171.....
.....$.......*..........$....*....*136....594......134....&............991..........292.................374.......123......676.........$....
...........957...&..740......631..........*.............403......186.........................................260...*..308...................
................582..*...............463.......524....-.....883...*..463..........389....71......+594..........*...45..+....................
.815....520..........747......@......*...588.....-....14...*.....632....*.946........../...............$982...927.....................822...
...........*..............271.889.631......................889........951.........363.774...............................%975...546....*.....
........709.......561.....-.................+885...................................................121..............794.......&....450......
............469.............478.44...799..........273..218.....361..........763.88............977.....*.969..............528................
...30.=........%......746.....*.*.........................*401.*...............*.........27......*.......*.................@.....#....572...
...*..749..............+...573...286..539........110...........101................241...........132.217..736...................714..........
.........................................*976...*....897.136.......@............./..........338........*......149.....894..874..............
...267*537.................850..864.262..........178..*..+......721.......314..............$........834.........%..........@................
...............966.479.............*.....448.........98....25#......................524.................728.871....../.............258......
....109...........*.....570....775......*...............82.............933.107*......*...415..109............*....795...*953....#...........
......%..................=.....*....408.277.677...143...*................*.....792..501..*....*........354.629........81.......899..695.....
................*166............98.%.........-....*...677.......+.........................955..26.....#....................103......+...140.
.............286..................................638........158...........4.&....................57..............-...........*......../....
...968...............112.@139.....102..............................860.546...922.847.....888*390..........88.....796.........507............
...*...........550..*................*493.................54*299......*................................$.................348........%.......
672...........+....142......................................................548..343....................843........&.....*.......773.....3..
.........791.........................................................&..#........*.........383..716.........599...415.155...................
.565......%..............49........................712$............159...854......662...-..*....=.....*.......*............173..973.........
......./.................*....624......905....590......................................271..445....958.42....531......293..*............866.
....645..877.............304...$...761*......*.................718*495...............................................*.....587.....815+.....
.........*......958*319......................40............678......................701*422....37......357............170...................
..........493...................438.224...............&.....*.............814..................*...832*........%863................448......
......-.............727.....412.*....*...........@...514..563...............*.....853*194.986.475.....................................@.....
......274.....810....*..874.../.688.40...271..646...........................66..............*........351..........*122.97&.435..............
.236...............755....*./...............%..............112...................492.......798........*...946..717........./...........453..
...*...........509.....766..169....&.........................*......................................719......=.........998.......*....*.....
309....896......*................773...........+..155.797..#.........164......296........................................-.......312.452....
..........*..216.............670..............90..*.........705.......=.......*...893.214.....655.......439*158..345.$.......51.............
....#....746..................@.....874..*.........46............456...........26.*...*..........*...............#....491...*...421*795.....
....596...............578&..&.........@..190.256................*......614........769.47........7.......$..+405.............536.............
.............868.973........613.+.......................516........431*...........................613..759......+.....546.......441...&.....
.....134*82.*.....*..483=.......940.987....................*...................13....-..............*..........184.............*....789.....
...............697........347-.................23.#........27.568/..............*....312.&373....889.................%........682.......673.
.........930..........141..............997.....%..528.=829..........254.................................55.......690.765....#...........$...
...655....*.............*....636..........*....................36..$..................503.....*417.....*.....942*...........67..92$.........
.....*....397...999......441...*....568...686............................................=.376............-.............683.................
713...79..........@..........423....$............935...................899...303$....=..........579.......720.508.......%....=..............
............416.......22.....................$..*............624.........*...........922.......*................&...........63......977.....
...........+..........*..........*27.......943...35...........-..........83..210...........497.854...28.......&.....*344....................
662..892..............146............................886.........503........$..............*...............253...678..........441...........
......*...814.......*.......%....../203...35......68*....#.......*.....579...............35.....650...../......+......716............901....
506..152.*........777......551..............*898........225....728........*224...................*.....425......917..............500...*....
...*......984...........................861.........&22............30...+........353........77.565............#............405...@....873...
...99...........109....490&..445...322../............................-..135.......*.........+........859...454................*....&........
.................*............*.....@.........927.@738......925..797..............503..618.............=.....................562.643........
......546.358.281....247/...831..........977....*.........../....*............553.....*.....994.343......................880...........-949.
..721...@..........................305........590....410..........94.296......*......19....*..............202......88+......-...............
....*........590..................%....542..........+..................*......582...........229..................&......-........273..585...
....623.......@.............771........=.........+.........737&.......683...............*.......*.......613...769........849......*..@......
........722.....571..725.....*.................758..........................209...584...162..826.246......*..................212.487...774..
....352.........*...../......269.669.....................527..537...........*.......+....................649......368....251*...............
165....+.708.675..79.....408...........612.....&.........+......%..244@....399........912...364-..............17.....................547....
...*.....*........=.....*.................*...551......+.................................*.........726.257...*..............623..839.*......
.478.....768..........935.......194..813..890..........290..911...261..........698..450...340.........*.....361..280...............*........
..................820.......548*...../............................*...941$.......#....*..........................*....406...........621.....
....@.672.............177...............868..716..965.............246..............444...%164.....................44.@......931.............
..540...#.74.............*.....875...90*......*.....*..962................................................107@..............................
...........%.791.......95.........*..........166.$.....#......615*470.......333/....73........5..449...........@.........................896
....227......*...582%......17#....................703.....@...........................*.733...*...../........216.495........383*289.........
...#.........255.................%.......&...434.......270.....712..................628..+...893....................-.280...................
...................171..750.......312....733......%............./......*887...@.............................................................
327..+295..700@....+......=.....................158.................357.......930....................645..........399....149...-......716...
.....................966.......378........................96............576.+................58.....+.............*.........*.114.....-.....
....46$....................776*.....522......................$.........*....70.....534.......*..990........892...556.....131................
.............98....-.587........697*..../729..@...352....688..987....647.......974.........706....*..........%..............................
...47...498.....121.../.......................218..*........*................-...%..647*.......808................&...../...647.............
..../.......................880...996.............351.$...276.......937*636.4...........75...................502..43.156....................
.......141..140......-..976*..........................853.....=402.........................487........648+../..............*124.............
..........+...*...868.........*....158.....559.......................128.....476........@.-...................@..986....729.................
..............663..............114.*...363*........909*961...........*........*.......897....$.............678..$...............725.........
..886.329..................324.....722........287..................$.81....166.............555.929..................414.............292/....
.....*.....$....947..767...-....................*.......527..111.293.............726...........*...553....../665.............950............
171........937......*........751.................993...../....*...........915......=......651.519.....*..........426...21/..*........907....
...*560...............................217...................347...429.......*.........50...........293.......160*.............*698..*.......
..........49...................55.....*....130......305@..........=....459..141.380*...................920........501......432......301.....
211*......*...................*......29.......*...........586.............*.........866...............*..........*..........................
....766.102.901.....*...........696.....@.....179..969......+.............513............840#......#...639.....102..........................
..............@..366.710.$545....+...654...........*............133.............209...............675................41........122..........
.............................................430*...75..........*........889..............761.702..........*379...............*.............
...958...905..........*718....*932......585............650...689.....177.....840.........*.......*......869.............107...170...526.912.
...........$.......268.....812.........../...941..........*......893*........*.........971..614..452.........207.......*...............*....
.293.............................286.........*.........432............*83..499..682..........@................=.....257..........520.....977
.....767...711..$683......*......*..........419.625#...........788.549............+.712........411..946.........@...........................
.......*....=...........58.991..412.42.222......................*.......79..978.....*............%.*........*58..389......616.........686...
....131............67.......................995............926.561......*....*....406.273...........490..611...................634$.........
..........908.320........................................................725.533.......*...624.....................198*246.209.........#....
............*...@....594..298....743...601......123......@......@606...$..............439.$.....#....../175...386.............*......490....
...16....371........*.....-........*...........=..........202..........373.....6.749............28................*....675.....529..........
...#.............662...............422.............&.......................462....*.........139...........175@.....376.+....................
.......................+..@325...........18.....543....................../....%.699.....611*..........240...................................
...............297...754....................................15......790..785........474.................+.........-.......719.....962.......
198*...........@..........252...................413...*800......@................./.*...............951...@899...748....#.....486*..........
....295....334....900.......*..................*....37.........306..............263.737.......722......-................651.............679.
...........*.....=.......150.......535.531....609..................287.....@.................*....519..............92............*8....*....
904......139.........995.......@..*.......*.*.....933...229........../...45.......361.206.....37.*..........730......&...226..585......553..
........................*...276.......$.435..582..@.....*........185...............#....*.559.....471..........*...........*......974.......
......285..........493.61...........81..................844.......*.......424........330..*.....................342..994.222.......*........
..492*.....456*326..*....................728....705.............973.........*............313.554*......596............*.........288..958....
.....................247..........*508.....+.....*.....#...................789.....................861*................936...........#......
.589....368......956.......#...469......................757..361....144*..................................=......*..........................
........-.......$....80.822........560...........172....................212........=552..*.....#....%..858...462.739.....343........22......
..........=........................*.......170..*..............................413.......368.264.622...........-............*........*......
...601..505.234.&371...........123.............739.812......429.................%...788....................................277..967...170...
....*.......*........547...........%.....849........*......*.......532...309@.........*..................536....581...352.........-.........
.....840.....636.996*.......399.....455..............35.723..934..*............842...875....90...........*..............*...992......@......
.........817...........#462....*............$788................*..615.469........*...........*..261.....774.........830......*...624.......
.........*..................607...............................117.................532...............$....................950..676...........
..749*12..558..62...+199........492......870.....719........................548.........353......................486.....-........698*61....
..................................-..514*....25@...............681..*975..........*437....*....99..............@.*.........+................
....363...296.....350.............................457.....421-....*.......*234.980.........339.#.............660.443...$.645.132.....930....
........$.....*....*........617.........712.......*..............122...237............491........../452..............445.......#.342...@....
........903.422....854.643...*......942...*.......334................................*.......855*..........507....................@.........
.326...................*....426.838.*.....948...................868...635....635.....401.........810.........#........887...40.......743....
............879......889..#.........252.............496....*...........#..........55........370..................39..$.....*....331..*......
..506......&...............487..............794....*.....55.................262.....*..465..*.............309......=.......460...*..431.....
.....#............................*......41.........911........./.495.....................*.241..........*....509.....314.......329.........
...............627....15.706...277.276........................68.............*.........................491.......*........#898......%956....
...........830....*12..*....*.................219.112......................952.....637*........-..539..........999.316.2..........%.........
....../.......*.......132..577.595......426..*......*.3....#380.......681+...............460..829.*..................=.....223.....615......
......726...811...........................+..91..980..*........................$..........*.......639..................193.%............403.
...................358*............633................526....266.........666...534.....662........................+.....$.........758...*...
.......=.......137.....313.........=.............998......&....*..........*.....................559..313..825=.....353....405.........296...
....447...........#...........342....%.....%........*..938......238.....327..............*152......@...*...................%..472.153.......
.............152#............*......792...334......741........................570*....335..............137..........338..........*......+...
952.........................................................793......583..........623............11........730............50.116.........446

10
2023/inputs/day03_test1 Normal file
View file

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..