From 4c523d9e3494d75a05894643555f95468c91fe56 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sun, 3 Dec 2023 22:18:17 +0000 Subject: Day 03 part 1 --- day03/day03.java | 117 ++++++++++++++++++++++++++++++++++++++++++++++ day03/input | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ day03/test | 10 ++++ 3 files changed, 267 insertions(+) create mode 100644 day03/day03.java create mode 100644 day03/input create mode 100644 day03/test diff --git a/day03/day03.java b/day03/day03.java new file mode 100644 index 0000000..4d4caa9 --- /dev/null +++ b/day03/day03.java @@ -0,0 +1,117 @@ +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +class Number { + int row, colstart, colend, value; + + public Number(int row, int colstart, int colend, int value) { + this.row = row; + this.colstart = colstart; + this.colend = colend; + this.value = value; + } + + public String toString() { + return "[" + this.row + ", " + this.colstart + "-" + this.colend + "]: " + this.value; + } +} + +class Symbol { + int row, col; + String symbol; + + public Symbol(int row, int col, String symbol) { + this.row = row; + this.col = col; + this.symbol = symbol; + } + + public String toString() { + return "[" + this.row + ", " + this.col + "]: " + this.symbol; + } +} + +public class day03 { + public static void main(String args[]) throws IOException { + RandomAccessFile input = new RandomAccessFile("input", "r"); + System.out.println("Day 01 Part 1: " + part1(input)); + input.seek(0); + System.out.println("Day 01 Part 2: " + part2(input)); + input.close(); + } + + public static String part1(RandomAccessFile input) throws IOException { + String line; + int row = 0; + ArrayList numbers = new ArrayList<>(); + ArrayList symbols = new ArrayList<>(); + + while ((line = input.readLine()) != null) { + getNumbers(numbers, row, line); + getSymbols(symbols, row, line); + row += 1; + } + + int tot = 0; + for (Number num: numbers) { + boolean part = false; + for (Symbol sym: symbols) { + for (int col = num.colstart - 1; col <= num.colend + 1; col++) { + if (sym.row == num.row - 1 && sym.col == col) { + part = true; + } + if (sym.row == num.row + 1 && sym.col == col) { + part = true; + } + } + if (sym.row == num.row && sym.col == num.colstart - 1) { + part = true; + } + if (sym.row == num.row && sym.col == num.colend + 1) { + part = true; + } + } + if (part) { + tot += num.value; + } + } + + return Integer.toString(tot); + } + + private static void getNumbers(ArrayList numbers, int row, String line) { + Pattern pat = Pattern.compile("\\d+"); + Matcher mat = pat.matcher(line); + while (mat.find()) { + numbers.add( + new Number( + row, + mat.start(), + mat.end() - 1, + Integer.parseInt(mat.group()) + ) + ); + } + } + + private static void getSymbols(ArrayList symbols, int row, String line) { + Pattern pat = Pattern.compile("[^0-9.]"); + Matcher mat = pat.matcher(line); + while (mat.find()) { + symbols.add( + new Symbol( + row, + mat.start(), + mat.group() + ) + ); + } + } + + public static String part2(RandomAccessFile input) throws IOException { + return "WIP"; + } +} diff --git a/day03/input b/day03/input new file mode 100644 index 0000000..bc52a0b --- /dev/null +++ b/day03/input @@ -0,0 +1,140 @@ +.679.....662....71............................805..........862.680...................................................................687.... +............*....-..811..........846..855......*.............*..$........230.92@............................=.....................92........ +..........360..........#....664.....=.*...881...677...934.780.......426.*..........8......654.....*959.....539..........21.........*........ +.....................+.........*......379..*.........*.........=.........969........*........*.976..............872....*....../....579...... +.......566......652...809....482.394......492..303.650..../...38....%...............106...385..................#.....793..484.865........... +......*...........*................*..347.......*.........220.....349...691...392*..................18..797.......................+......... +.....11......890........870.......156............733.................../..........921.....................*......*.............203.......... +.............#...238.....*...........................188......294./58........408...............677.......778....104.903............%........ +.411+...828.......*.....706.....249..=.....638...848*...........%...............*948............*.....+......+...........917.......817...346 +................69.................*..310.*....@..........118*......428=..785...................931...217.....934....475...@.145........*... +...844.885.........#66...........587.......74...773...........194......../.........&107................................%...............375.. +......*....826.190.......498........................960............24........212............................398...........474*925........... +...........*...*......../......949.-...213./353.................................@...........853......33*164....+....998...............325... +........640..823....957........&...746..*........*777.............134.......264.......904+..=........................*......./........%..... +....43...............*....457............325..390...........618.........265....*670............=...#165........909..634.......810........... +.....................503.*.....438..904*.............+.........*..563..*............345@......597.........758.*...................*154...... +...941...................612...@........691.....&...336.......837..*....489...............................*...462.689....995...106.......... +......*383..824...................911........*..467.................44....../.........&445...$...765.647..464.............&..........226.... +......................151.....116*........249..................929*.......500..140.........18.......*.........823.....454............*...... +..................968..@..........................118*.....300.................*....762.........541......59...*....35....*..........8....... +.....304.........*.......620..@...573.878................................@...@.273.*......572...............661........527.971*.......259... +....*.............83.....%...983.*......*.32..603.....201..............492.166.....339.....*...978$..............373...........340.....*.... +.761..758...934..................550.148.................................................25......................*.....*236.............624. +........&......*...393..62*.................167/..716........$........910.767.......380............873*812.362.115..537......48............. +.....#........697..*.......159.....................*.........522..............41.....*.........+............*...........812......13......... +....529.............374......../...%.............................143...........*......109.......365..........952...........*....*........... +...........122*...............555.937......674....................../...529.774..371.................#.38.........../.......304.750....715.. +..................873*....819..........165....*..928*345........................*..........77......478..*...440...743...................*... +964...........417.....802..*......230..-....509..................573...........219...675....-.865.......13............25.62*4...980..145.... +....*556........&.............278*.................957.....85#.....*................*............=....#.....442........$.......*............ +.972.....................541...........91*614..485...............680..792...+........541.325..........486......&..653....#.....247..778..... +.......680.................*.980*710............*...494...............*......88...........................825........#.629.............$.... +.399...*......439.........89...........430*....401.....*660....693......407&...........#883....#265......*.....438.......................... +........390....*.....388.........21........861............................................................798.-....#........................ +..417&.......551.102.$..............332............/804.....463*649..188.........992.....754.203....912..........732.490.................... +...................*................*......214.134......................*805.%.....*.....*....*.....#.....*..........*....302.....934..=.... +....835.........622..............8...541...*....*..861....442....874..........458...955...985..172.....497.310........341......$....*...644. +....*......246......909....320.............893.150....*.....*....*........717................................................833.178........ +...622....*....309.+.........*.........681..........414......612..678....*...........336.................+.....715.......................... +...........407.......611.....42...........*159........................................*..97..............355...*..........*954.............. +.......967.....120..=...............................................................519....+.....382............974...................651... +739.......*.....*........155.....539................132*.....702......@....923...............905*.........................971............... +....453...149...717.400............*.........618........295.....#..644.......-...786....................*...287...........$................. +...#..................*.............810........*.908$............................*......+........168...903.....*.....&.......@.............. +.................306...483..750.108..........570...................934......523..736..#..889......*.........557.......913....107.852..241... +...............................*.......................899............*974..%........42............268...........807..............-...@..... +..958.......610............/......................894.........................242.........300...........$............941..150............... +...........#....131.907.....924...529.267............=.....-./.....692*505...*......265%............$...95.....538........#................. +....137...............$...........*...*.........219.....347..268.............753....................12.....351.................955....875... +......*....258.................854..253...15........................506....#.................559........&................&........*......... +.....376...*.....892.466..160............*.......$................*....*..593.............../............682....721...750..10......187...540 +...........866...=..................$...437....994......730....404...703................507...&...........................*....126......@... +....300...............301....@...315.....................*......................572........-...793.......309.518.437..182.310.*.....&....... +...=.....-176.129*........882............................256...............249..*.....+.....................*.....%..+.........71...76..586. +......54..........910............*......734....*..............-.......445.....@.....807.....75.907....477%.................................. +.....*........................247.855....%...647.....@.....558...407....#....................%...*................................447..129.. +......322..235.......821..............*...........767...............*35..........824.338.55....232...814................546*582.......*..... +..............*........./...........509......270.......-33...673................/.......*..............*......-.....................257..... +....793*831....805.871.....701..........503.....$............%.............710......37..../.........897...-....516.....330...371............ +....................*..324*............$............875........293.........*.........@..510..............332..............*..*....2*558..... +.........../......147........373..........%926.........@......-......426...142...............%.......184.........#.....746.................. +.......589..755.......285............221+.................$.........*.................571.....539......*.....60...644.............333....... +258....................*.....499............$.107......510.......257..........583.......@...........408.............................*....... +.....................396......*.../..../..493.........................489*380....*........908.711............772..........561......24..559.. +.............................548.769.630..........504*........893................605.........*.........*..10................*..653.....*.... +........=418..........*576................-..............815.....*.389......481.........*.......859.180....................137..+....275.... +..77...........551.901.....535............683.............../.638............./......671.908.....@......360.....611....136.................. +...&...........*.......722...........................702.............514............................520.$.........*...&.....$315...#...147.. +.......830............*.............#377........39.....%.......384..*.........748..265......271.............885..882...............45...+... +..329.-...........787.311.........................*........115...@..840.........&...*.......*......513..695..........515.................... +....*......666...*..............256*999......897.651.......*............457.........34..&...933...*......*......................156......... +..627.......*....24...........@.........................177...22........*...775........668......685...947.....110................*.......... +.........172..........295*....540.856..76......................+...337.343.=.............................................18....914..449..... +.............239..........163.........*......770....594.........../..............200.....824*513..........146............*..........-....... +..............#................841...199......=............317..........97.*..........-.............165..................................... +...570...../......................*.......122.......179#..*......138........647......376........65..............735......................... +.....@.221..50..350...@....757.....88........*582.........893.....*.....230.................403...$......................865...927....892... +..........*........#...130..*......................232.........709....&...........105......*..............926=.......966.*.......*...$...... +....200...884..............338....*155....822*609....&..84.........935..............#.....481...968.64................+..337.....152...254.. +.......*.........203............70......................%...................../................*....*........................627............ +.......491........*......321.......$...-.......282............................512.282........564.625........746*825..#..........*361........ +...................908...$........335.332.509$....*..../..........216.....-.......*...................617.............896................... +......946...299............657..................255...724..../......*.....830..122..........487.........*.....645.................56.66..... +..984..@...........@....15*.............*858...............150.....91..................498.........654..969.......+............&.....*...... +.........104.870.281.........$.......796.......................................................................853.............316..984..... +.........*.....#......=...288.............86......376.804...515...............405..265*................................%.................... +.336....152..........671............545.....&......*.....*..............549...*........37.....-....313.#......110.....578..414*............. +..............496.....................*...........536..20........471...*.......397..........437.....*..996...&....18...........200.......... +.................=..........308.....147...319.12................*.....465..............$992........71..............@........................ +............712.......*123.=..............#.....*629.......%..............................................#..........153..945...........36.. +.....+340.....-....850................922...%............812..259*577...#452...-........620.567-.784....985.934.427...@..*.........280...... +216............................394......*..589.........-.....................191....622./..........*........*.............124........*...... +......764......................*......663...............165...318.....................*....200.83...896..856................................ +.....*.......705........751..46...............................-....698....132.......-..81...........................264..................... +...538.......*......170....*..............808........166..........+......$........130........428............852.......*.................902. +..............803...........938..51....66.*...514.......*430..................................*......@..............=............/469....... +.........26@.........90...................473..../..273..............13......656.......242$..829......682...........594...968............... +...180............+.........872..131....................176...781...-.......*.....294.............990.............#..........=.@...../...... +....@..344.........184..........*.............................*.........42..153..#.................*.....577......32...........303..334..... +........&..620..................821....172....32.....470....698..........%....................397..301.......................*..........569. +.../........./.........325.@.............*...*.........*........$.....*....+......124...200......*.......827....*861........857.876......... +..808.................*.....581..............644...3..190.607.175..76.712..724.......$........16.358......*...31.......270.........*915..... +...............894...453...............845..........#.......*.....................#......................793..............*...914*.......... +..................&......................+...............99.596..597*813...958.831......261*95.....922..........&.......537.......302.414... +...931*.................................................+..........................394......................65.679..........%.........*..... +.......839....644......./262......461...424.....682........................954.............43*.............*.......778..*....464......822... +.............#.......................=..*...........706....289*.....459......*..........-.....234........11..$.......@..373................. +.............../620...536+....100*......907.....159..+.........998.*..........832.....806...........695.....516....................126...... +..................................268.............*....893.........12.86.................................................*.................. +......+....355.........998..630..............$.....815...*..............*.....168...554...........735............+......861....490.......... +.....176...*....513....%....*.................120......568....*314.404+.325..........*..662...855.....495.........467..........*............ +..........132......*......677..708.................424.....715.....................520..*..................................36.410........... +..419...........454..............#...730...401&....*.....#............696..............267.=802...................761......*................ +............394.........................*........650...637....695......*.....................................586....-...735..154.....423*344 +........458..%.....*...728..+..........488.435......................241........795.552.....9.&...............................#.............. +...........*....86.981....*..633.........../................762.700.....70*.........../....*.837......-.........20..................%....... +.519...+.652...*........614.......+....................924....*............663..591......192.....952.25.........*....600....#.*239..381..... +...*.366.......845.512......@...200...-....280............*...207............................297...*......828...249.*.....408............... +.189......5........*.....353........245.......#...153..680..............295..50...............+....296.../...........906.............+...... +......741*.........466....................710....=.........561*........*..............831............................................909.... +...........+.................421............*..........+.......661..103...............................922..752..................431......... +181......218.707....*....273....*..130.$273.980.........656..............517..712..578.......&.524.....@....*.....996..............*....#709 +.................442.432..*....64.*......................................../..*....#.......534....*........498.............727.....434...... +233*559....................869.....316../..........&.....502..+...884.731.....503................239...499.....94..........*................ +........352.......231...................340.....177..584.....922.....*....57...........................*.........*349...222...212&...433.520 +...........-.........*981..479..637.....................*969.....*.........=..$.........601...=826...312.................................... +..............205.................*../...@.....213.............41.64..........80....889*.............................@....76.=702........... +....353.......*..................114..97.867...*.....542.........................................721...395............816..............*112. +....*.......367.*............478.............42..../..........947.................................*......@......956...........+...883....... +.....870........460............*...................26...........*...........960......212...479....422...........*.......780..595..*......... +....................978*368....921..760=..................416...665..$.........$.381*.......*............933.744....=..*...........792...... +....52..........300..........................................*........293.162%........................@...*........259..283................. +......%..........%........119.......575.......#..%......./.748......................945...212&.....595.....248.286...........839..106/...... +..940........$...........%............%....880....539.659.............*.........353*.....................=.....*................*.......188. +.....*....254...............794..550...............................701.797............500#................34..475...507..........763........ +..649............58..691&.....%............................................386..............@.$44.101.....................997..*............ +.........255............................376....732....-..............869......$...........473........*376....220.........@.....721...443.... +...........@.913.....168....=909..431......=......@..976.......+.......*..........155............................620.......250......@....... +......806.....*....................*...........@................45.....475...724..*......&45.........+202..-576.....*.........*............. +...............383...........................372..................................474...................................432.471......729.... diff --git a/day03/test b/day03/test new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/day03/test @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. -- cgit v1.2.1