Merge branch 'day03' into day04
This commit is contained in:
commit
5076013fc4
142
day03/engine-schema.go
Normal file
142
day03/engine-schema.go
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
// "strings"
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintAndWait(x ...any) {
|
||||||
|
fmt.Print(x)
|
||||||
|
fmt.Scanln()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("./inputs/day03_input")
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// This regex find the numbers inside strings
|
||||||
|
renum := regexp.MustCompile("[0-9]+")
|
||||||
|
resym := regexp.MustCompile("[^0-9.]+")
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
var lines []string
|
||||||
|
var totalSum int = 0
|
||||||
|
|
||||||
|
// Read the whole file into an array of strings
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
numLines := len(lines)
|
||||||
|
numbers := make([][]int, numLines)
|
||||||
|
|
||||||
|
// The 2D array of numbers will hold all the numbers to easily
|
||||||
|
// match them with corresponding strings in the file using the index
|
||||||
|
// For every line in the file, cerate an array of numbers
|
||||||
|
for i := 0; i < numLines; i++ {
|
||||||
|
tempNums := renum.FindAllString(lines[i], -1)
|
||||||
|
for j := 0; j < len(tempNums); j++ {
|
||||||
|
num, _ := strconv.Atoi(tempNums[j])
|
||||||
|
numbers[i] = append(numbers[i], num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Line [0] needs to check only line 1 for symbols.
|
||||||
|
Similarly, the last line needs to check only
|
||||||
|
(last line index - 1) for symbols.
|
||||||
|
So we first check the line [0], then start a loop
|
||||||
|
from 1 to (last line index - 1)
|
||||||
|
*/
|
||||||
|
firstLineNums := renum.FindAllStringIndex(lines[0], -1)
|
||||||
|
firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1)
|
||||||
|
secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1)
|
||||||
|
// For every *number index range*, check if in the same line there is a
|
||||||
|
// symbol on (first - 1) or (last + 1), check in other lines if there is
|
||||||
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
|
// can break as you just need one symbol
|
||||||
|
for i := range firstLineNums {
|
||||||
|
for j := range firstLineSymbolsIndex {
|
||||||
|
if firstLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1 &&
|
||||||
|
(firstLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
|
totalSum += numbers[0][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range secondLineSymbolsIndex {
|
||||||
|
if (secondLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1) &&
|
||||||
|
(secondLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
|
totalSum += numbers[0][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now we loop from 1 to (last index - i)
|
||||||
|
for i := 1; i < len(lines) - 1; i++ {
|
||||||
|
// We need to check the current line against an interval of three lines
|
||||||
|
// breaking the loop for a single number as soon as we find a match
|
||||||
|
// (we don't want duplicate matches)
|
||||||
|
currentLineNums := renum.FindAllStringIndex(lines[i], -1)
|
||||||
|
previousLineIndex := resym.FindAllStringIndex(lines[i - 1], -1)
|
||||||
|
currentLineIndex := resym.FindAllStringIndex(lines[i], -1)
|
||||||
|
nextLineIndex := resym.FindAllStringIndex(lines[i + 1], -1)
|
||||||
|
OuterLoop:
|
||||||
|
for k := range currentLineNums {
|
||||||
|
for j := range previousLineIndex {
|
||||||
|
if previousLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
previousLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range currentLineIndex {
|
||||||
|
if currentLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
currentLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range nextLineIndex {
|
||||||
|
if nextLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
nextLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Now we need to loop the last line and confront it with previous
|
||||||
|
// and itself
|
||||||
|
lastLineNums := renum.FindAllStringIndex(lines[len(lines) - 1], -1)
|
||||||
|
lastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 1], -1)
|
||||||
|
notLastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 2], -1)
|
||||||
|
// For every *number index range*, check if in the same line there is a
|
||||||
|
// symbol on (last - 1) or (last + 1), check in other lines if there is
|
||||||
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
|
// can break as you just need one symbol
|
||||||
|
for i := range lastLineNums {
|
||||||
|
for j := range lastLineSymbolsIndex {
|
||||||
|
if lastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1 &&
|
||||||
|
(lastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range notLastLineSymbolsIndex {
|
||||||
|
if (notLastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1) &&
|
||||||
|
(notLastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("The total sum is: %d\n", totalSum)
|
||||||
|
}
|
||||||
1
day03/inputs
Symbolic link
1
day03/inputs
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../inputs
|
||||||
140
inputs/day03_input
Normal file
140
inputs/day03_input
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
...............307............130..................969...601...186.........................................312....628..........878..........
|
||||||
|
......479#../..*..............................#.....*......*............../309.....484........................*......-..........+.....89....
|
||||||
|
...........726..352...461..69..............435.....390...625....................................459.........152...-....580............*.....
|
||||||
|
.......................*.......454*674.448......65................257....104*762....&..............*269.........558.&.....*907.........652..
|
||||||
|
.....................164.....-..............532*.....................................484......108........955.........252..........321.......
|
||||||
|
.......270....*..............470.......................632....................*...................$............352...................*......
|
||||||
|
820........782.347...................*838..*371..475.......607...557.......201.29..............814....=.....15*....893..............143.....
|
||||||
|
...*..796...........449..138......991.....2........*....28....*.....*588.............................353..................*.................
|
||||||
|
582...../..333.............@..224............%....4.....*.....399........883..#859.........*341...........583*40.......366.951......170*814.
|
||||||
|
...............................*....*.....252......................551.....*.......91...374.....665.804*..........938...........694.........
|
||||||
|
...549.769..............527..466.169.712.......898.......408......*............-...*............*.......912.......*.......771%.../..........
|
||||||
|
.....*...=.875*342......$........................@........-.....846.........610..829.934*508...79............&691..48...................483.
|
||||||
|
....402.............................423-..............................465.........................342.....................@.................
|
||||||
|
..............837..................................264..................................*823.180....@.........%.........538............634..
|
||||||
|
..........807*....990+.................453............*277........657..379...........200.......*.............296..............767..885......
|
||||||
|
744...611................@........+.............500........866.....*......*..717............656..80...254................&775...%..*........
|
||||||
|
.......%.......*.2....878..793.97..143.413.&856......329*...*...732......878./.........191...............*..........+..............584......
|
||||||
|
.........535.506..............*.........-.................368.......................#.+........*.........429.......363......................
|
||||||
|
..564....*................966........................189......=............/24....939......26...547.......................193*241...........
|
||||||
|
.....*......................-..........................*...204........156......7............*................/.717*..................296....
|
||||||
|
..826.....924.......................956..............920..................259.-......$......286........6..955.........................=..457
|
||||||
|
......124*......................48.....=...435..............12.....886....*........411..........*170..%............90..-295.624.............
|
||||||
|
...........................290....*375.............747.....................175..........................653........*...........*............
|
||||||
|
..........642*157...........*.............................218.945..949&...................864+....720..*...........883.786-.....434..-..&...
|
||||||
|
...118.................@.....309...586............698.152....*..............250......*954........*......932...414...................239.39..
|
||||||
|
......@..999..574.......302..................53..=......*......933.69..............21.........538.............@................143..........
|
||||||
|
........*........+...........267..587*........*...........25......*..........241......289.........436.........................*.............
|
||||||
|
.......448.............437....*.......334...605......../..=...........689...%.........*...25.........*....97........171.780....813..836.....
|
||||||
|
......................*........169...............427...94.........389..$........360............/....692.....*......*......*.........%.......
|
||||||
|
......641...-..679.541..56.606...............490*.............871..........757.................612........64.....256...*...281..............
|
||||||
|
...........419...*......*..*..........................................*705...-.......................*.........@.......121..................
|
||||||
|
.......*.......786....738...28.........83.550........980...........915...............475..@.........524...=..145.349.................331....
|
||||||
|
....215.922.......................766.%....*.............993.................-.968..*.....301............205............................*...
|
||||||
|
............775..............273..*.........168.....504...$.....179.....+..466...*....352........850................771-.........-.......729
|
||||||
|
..699@........................*..174...............#........573*.......191.......567..*....925*....$......825..................543....&.....
|
||||||
|
........#....................939.......137...#..........474......../.................975........$.....219*............935............94.....
|
||||||
|
.....508...547....+.455..347.....593....&...56...140.......*246.198.....@....41...........46.476...........539...........+.15...............
|
||||||
|
...............210..........-.......*...............*773.............336....*..............+..........847.....*.............*.....957.......
|
||||||
|
...435...........................736...987.....573........32*............135.....60*405.........802*6...$......252....@...208.129*..........
|
||||||
|
.............187....353...............*..........*..504......276.........................#.........................838.............604......
|
||||||
|
..202...79....$........*..82-.............../.580......*..............181.......288......925.939....292......158..................*.........
|
||||||
|
.....................746.................381.......211..272...........*.....890....*215.........=...........*............953....22..........
|
||||||
|
.........................957......#..................*......../.......240....*............18.................294...766...............*......
|
||||||
|
.......483..434$........*.........184......#.....&..561.......354.........445....*626.......*174......................*54....603..432.457...
|
||||||
|
......#..........916.231..*178..........967...902........895..................135.............................207...............*...........
|
||||||
|
..941.....600...*...................31...............*.../............138*559.......19.803..........396.........&..377.....261...904........
|
||||||
|
...........*...146....#........626....*............809......199................@........*..............+.....#.......*.275*............%....
|
||||||
|
...........181.......459.486.....*.....912....787..........*..................847.....160.433.728.........901......404................899...
|
||||||
|
..........................*.....97...........*..............252..............................*.......+.........451........434..*............
|
||||||
|
...621...294..10.........913..........5..318.349.......749.......457......594........................326.........*..........%.884........494
|
||||||
|
.....*...*...-.........................$...........157.@........*..........*....&..951....524..869#..............405.................416....
|
||||||
|
....864.631....=......958..........................*....../.....953.......690..144..$.......*..........................204.&....%.......+...
|
||||||
|
750.............868....*.....430............882...98.......655..........................98.961...674........*.........*....955..530.........
|
||||||
|
...*..................459.......*929..........$...................281....-...............*......*.....38...688........878...................
|
||||||
|
.779....494.................+........-725............978..............134......2.......465....685.929*............759............351..291...
|
||||||
|
.....51*.......244....173..222..233........153*......*....123..............*.....324......................#.456.#...%...............-..@....
|
||||||
|
.........&....*.......$.............................542...*......77.......288...*....*51.......624......68....*.888....507..................
|
||||||
|
...213.367.953................................-667.......225.......*285.......506.177....*32.....*..........43........../...................
|
||||||
|
...#.................753.%........506.................................................131.....793...-.....-....................917$.147.....
|
||||||
|
.........275.............372.....$..........$........679.407..674..205#....406............647.....297..642.........621...............*......
|
||||||
|
.........*...................................173........*.....................*721........*.......................*....#207..........533....
|
||||||
|
.........68..............&.........274............322*...................707............280...............188..272..+...........223.........
|
||||||
|
.....553..................289.534.....%...830.........927..................*.176............*.............*..........217.........*..........
|
||||||
|
...................$..-.......-..........*................796*..........606...%...756...-.994....252.694..351.793$........930..230.....611..
|
||||||
|
.841%..659.237..356...116.............683...860...............494................$....127..............*....................*.........*.....
|
||||||
|
......#....................558..#891........*........418............&.............................22..323.....540...92....792.........408...
|
||||||
|
.................231*780....*.........*452.82........*........940..709....................662....*..............*...@...........432.........
|
||||||
|
....456.91...............972.....=.523.............949.837#........................823....*....627..............880....511.............203..
|
||||||
|
...$......@.........190.......162........508..........................158..861..........533.............749............................=....
|
||||||
|
......................*.............117..@....608....967.10............../........................&........$....................800.........
|
||||||
|
..752/...988.....82..281....894........*......-..................533.............692...............524.............=....623......@....939...
|
||||||
|
.........*...217*..............*.......51.........400...............&........200..@......435..484&........2*648.95..416.....................
|
||||||
|
.........319..................788....................*..279.......*.....................*...........321.........*..........464...286*....311
|
||||||
|
................733...457............*.............65...*.........620...205....457.782..513..342..........561.670.160..899.*.........607....
|
||||||
|
...............*.....+............397.....613/..........938.171...............*....................869......$.........*.....589.............
|
||||||
|
.............85..............=324.................189.........#.......808..578..-.........170*........*..&..........461.....................
|
||||||
|
......................992.............555........=...............375.....=.....599............336...835.282.....714.........................
|
||||||
|
...@......622.........%...............-....%....................*.....................705.287................../....955.426..140............
|
||||||
|
..202.....*...365.........$8....636*....148....................720............648......+...*................%........*...................220
|
||||||
|
.........223...+....................554........629...........*......785..............&.....460....*...#..848...24.948....961............*...
|
||||||
|
....................274..........................*...........995...........92.823.....958........185.983...............=..*....566....33....
|
||||||
|
.............146#....+....865.307................797...411................./..............577.......................245....935..............
|
||||||
|
....150...................*......*..560.................+...+298...............*....213.......984.......103.811.......................&.....
|
||||||
|
.......*..297..-905.822.643.....452....*333............................527..911.......*.........=.........*....*...............834.76.410...
|
||||||
|
..........+........../.......*...............774.....................................228..764-...........806..621.............*.....=.......
|
||||||
|
.......83.......753.......664.36..371..132.........341.......347...267........@813.............341*322..............237%.....134......$.....
|
||||||
|
....66*...630......*.238.............$.$......609.....*673.........*...920-...............%................937*873................844.807...
|
||||||
|
.........*......410.....#.........90..........%....-............789.........316.........755.932..$....................*921....363..=........
|
||||||
|
.........524........-.........52..........422...349......................15....*............*....550.........384..9.22........*.............
|
||||||
|
..............752....809..........$........*............#......397.......*..147....666.....83................*..............747......@474...
|
||||||
|
.......995......*............898.753.....305...........306......*......344............#....................985./................206=........
|
||||||
|
......*......172...............*...........................-..603..........*771...870...260....609..............588.....+503................
|
||||||
|
.....475.....................412.634*480..155....$824...845........409..367..........@..........*..498...............................893....
|
||||||
|
317......737.654.............................*97............781...*..............#......184..252..*....@.564*347......312*..............*...
|
||||||
|
............*.............658..........91.............402..*.....929.....346.....912......=......825.412.........908..........&..........500
|
||||||
|
.889..%.............471*...*...........*.........685-...+..208.........................*..................*291..*.............926.826.......
|
||||||
|
.......647...@..........84.929........518.375.........+.............%...............139................467......813.690...........*.........
|
||||||
|
...........276...................165.........%......778............412....594.619...............768........401..........195*861..70...$.....
|
||||||
|
......................1.....584.........+.......954...........299@..............*..............*......./../............................759..
|
||||||
|
........886..356.&784.......*........414.............................702.......128...+315.....302.....283....+.......309*231.......316......
|
||||||
|
..........$.............811....................*.............237....*.....................426.....713.......638.442............957*.........
|
||||||
|
....572...........491&..*......46............41.362.............*..361.700...883..478.......%.......*............./.....245.................
|
||||||
|
655*.......720...................*............................211.........*....*....*.........502.862.........*............+.......137......
|
||||||
|
.....302....*...............762..668.....446.............................552.....488.......................415.622.113.437........%.........
|
||||||
|
.190*.......362...581......#...............*...803.14.............&61.................$......972...479......................................
|
||||||
|
......760............=................478..627....*.......34*..........................51......=....%.....828............................531
|
||||||
|
........*.....764#...................*..............529............................356...........*.........-...................996*271..*...
|
||||||
|
220...58............721...508.872.634...........300*.........507.....290...........*..........356.780...36.....553.......................875
|
||||||
|
...............292.@......*............833.71...................*305.......382.....273................../.............998.........679.......
|
||||||
|
.........616..*......../..524.885.....-......*..527*538...794......../.....=....$........=....................812........*..........*.+297..
|
||||||
|
....579....*...629....666.................982...................467...468........292.....67........398...........*..283..225......550.......
|
||||||
|
241*.....415.....................832............................&...........&.................481..*.....444*826.....*......................
|
||||||
|
.....*..........................*.......$...920......600-..323#............930....591....357...*...653..............182.............816..575
|
||||||
|
..178.157...............186=..568.../...134....#....................292............*.....+....438..........+824...........859.814..#........
|
||||||
|
..........*209...462................606......%......405............*........*..334.623................*...................+...@.............
|
||||||
|
.......784......*............................632...@.......*....597........320.........219............210...........805.....................
|
||||||
|
595............664............&..%847.................$.749.434.....*894..........757...*..................$813......*.......&.%....278.191.
|
||||||
|
............................311.....................156.........244...............*....163....$...................706.....893..115...*......
|
||||||
|
.......451-...215...474.............610.687......................*..............535........424..........727*411.....................314.....
|
||||||
|
..............*................82.....*./....-.....482*307.....502.......595...................490..............590....*406.............309.
|
||||||
|
.............97.............................860...................................................*.......395...#...656.....................
|
||||||
|
......732........516......486......662*............*739...........=....579.............754......821..718...&..............609...753..103.62.
|
||||||
|
.908...*...........*.........*...........*824...197.....$......227...........956............331.......*......390.................*....*.....
|
||||||
|
....*...210......284.........821......148................644....................*....533.......*.....280....*.......-..........904.....748..
|
||||||
|
....368.......$.........542....................................*.411...........982...*....958.671........975.........610....................
|
||||||
|
...........820...358$....&..298........$....62...@....983..+..49.....254............784...*......................947.....................776
|
||||||
|
.....766.......................*......68.....#..477...$...428....209*.......*...265.......191......*........268...=....81...618#........=...
|
||||||
|
........*847.....661.474......624.........................................223...#....*...........74...................*.....................
|
||||||
|
.................*......%.574.....................976.475*618.@.....=..27.........975.280.....*......=...............13.....757-.......112..
|
||||||
|
.....721.........748.........*......392&.........../..........636.911......................359.......857.656....72-.....................*...
|
||||||
|
........*...................405..40...........87+..............................%89..............237.........#...................83....632...
|
||||||
|
........108...............................................172.54*.................................&...................%........*............
|
||||||
|
............736........3....727.....=.40...................*.........877.........=191.......................+...139.903........151.....%....
|
||||||
|
...725...56..%..410...$..@.....*...2...*....969.............207...../.......831........977.........158....946...*...................967.....
|
||||||
|
.....-....*......+......346..163......8........*..........@..................*.....509*.....458...*..............834...636@.....304......357
|
||||||
|
...-.......790......308........................453......396...........871..712..*.......625...%.426......777.423............................
|
||||||
|
..810.................%....355.421.....36.........................151...........935.774*........................#.......619.390.............
|
||||||
|
.............360*201..........*.......%...794...-..70......./24..*..........................765......*303.167.........*..&....-.........*...
|
||||||
|
.........*.......................273......%...198..*..............967................724=...*.....776........@.120.696......=.........37.301
|
||||||
|
......333.588.........205..........................572......................................673.........876..................893............
|
||||||
10
inputs/day03_test_input
Normal file
10
inputs/day03_test_input
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
||||||
Loading…
x
Reference in New Issue
Block a user