diff --git a/2023/day-01/checksum.js b/2023/day-01/checksum.js new file mode 100644 index 0000000..4309267 --- /dev/null +++ b/2023/day-01/checksum.js @@ -0,0 +1,117 @@ +/** + * Generates a checksum for a string by concatenating + * the first and last digits found in the string + * @param {string} data of a single line + */ +const checksumLine = (data) => { + const parsed = data.replaceAll(/([^0-9])/g, '') // trim non-numeric characters + let result = '' + if (parsed.length === 1) { // some strings only have a single digit + result = `${parsed}${parsed}` + } else { + result = `${parsed[0]}${parsed[parsed.length - 1]}` + } + return parseInt(result) +} + +const lazyNums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] +const lazyReg = new RegExp(lazyNums.join('|')) +const lazyNumsReversed = lazyNums.map((num) => num.split('').reverse().join('')) +const lazyReversReg = new RegExp(lazyNumsReversed.join('|')) + +const lookupPosition = (match) => { + const idx = lazyNums.indexOf(match) + if (idx > 9) { + return idx - 9 + } + return idx +} + +const lookupPositionReversed = (match) => { + const reverseMatch = match.split('').reverse().join('') + const idx = lazyNums.indexOf(reverseMatch) + if (idx > 9) { + return idx - 9 + } + return idx +} + +const lazyChecksumLine = (data) => { + let first = '' + data.replace(lazyReg, (match) => { + first = lookupPosition(match) + return match // reinsert so we don't bork the data string + }) + // find last matching digit by reversing the string and searching backwards + let last = '' + data = data.split('').reverse().join('') + data.replace(lazyReversReg, (match) => { + last = lookupPositionReversed(match) + return match // reinsert so we don't bork the data string + }) + + return parseInt(`${first}${last}`) +} + +/** + * Generates the checksum for an entire set + * @param {array} set of lines containing data + */ +const checksumSet = (set, sanitize = false, lazy = false) => { + let filter = (data) => data + if (sanitize) { + filter = sanitizeLine + } + + let checksum = checksumLine + if (lazy) { + checksum = lazyChecksumLine + } + + return set.reduce((total, current) => { + return total + checksum( + filter(current) + ) + }, 0) +} + +const numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] +const numbersReversed = numbers.map((num) => num.split('').reverse().join('')) +const reg = new RegExp(numbers.join('|')) +const regGlobal = new RegExp(numbers.join('|'), 'g') +const regReversed = new RegExp(numbersReversed.join('|')) + +// Sanitizes using a single-pass regex replace all +// Produces 53885 which is incorrect for part 2 +const byRegex = (data) => { + return data.replaceAll(regGlobal, (matched) => numbers.indexOf(matched) + 1) +} + +// Sanitizes by replacing just the first and last text digits, in case shared letters is supposed to work +// Produces 53853 which is too low for part 2 +const byFirstLast = (data) => { + // sanitize first matching digit + data = data.replace(reg, (matched) => numbers.indexOf(matched) + 1) + // sanitize last matching digit by reversing the string and searching backwards + data = data.split('').reverse().join('') + data = data.replace(regReversed, (matched) => numbersReversed.indexOf(matched) + 1) + + // return original order + return data.split('').reverse().join('') +} + +/** + * Sanitzizes a line by replacing spelled-out numbers with data + * @param {string} data line of input to sanitize + */ +const sanitizeLine = (data, method = 'byFirstLast') => { + const methods = { + none: (data) => data, + byFirstLast, + byRegex + } + + return methods[method](data) +} + +module.exports = { checksumLine, checksumSet, sanitizeLine, lazyChecksumLine } diff --git a/2023/day-01/checksum.test.js b/2023/day-01/checksum.test.js new file mode 100644 index 0000000..72b2060 --- /dev/null +++ b/2023/day-01/checksum.test.js @@ -0,0 +1,122 @@ +/* eslint-env mocha */ +const { expect } = require('chai') +const { checksumSet, checksumLine, sanitizeLine, lazyChecksumLine } = require('./checksum') +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') +const { inputToArray } = require('../../2018/inputParser') + +describe('--- Day 1: Trebuchet?! ---', () => { + describe('Part 1', () => { + describe('checksum', () => { + it('calculates the checksum for a string by concatentating the first and last number', () => { + // provided + expect(checksumLine('1abc2')).to.equal(12) + expect(checksumLine('pqr3stu8vwx')).to.equal(38) + expect(checksumLine('a1b2c3d4e5f')).to.equal(15) + }) + it('handles the edge case of a line with only a single digit', () => { + // provided + expect(checksumLine('treb7uchet')).to.equal(77) + }) + }) + describe('checksumSet', () => { + it('calculates the checksum for a set of lines by summing the checksum of each line', () => { + // provided + const set = ['1abc2', 'pqr3stu8vwx', 'a1b2c3d4e5f', 'treb7uchet'] + expect(checksumSet(set)).to.equal(142) + }) + }) + }) + describe('Part 2', () => { + describe('sanitizeLine', () => { + const data = [ + 'two1nine', + 'eightwothree', + 'abcone2threexyz', + 'xtwone3four', + '4nineeightseven2', + 'zoneight234', + '7pqrstsixteen' + ] + const result = [29, 83, 13, 24, 42, 14, 76] + it('cleans up a string when digits are spelled out', () => { + const set = JSON.parse(JSON.stringify(data)) + for (let x = 0; x < set.length; x++) { + expect(checksumLine(sanitizeLine(set[x]))).to.equal(result[x]) + // expect(checksumLine(sanitizeLine(set[x], 'sanitizeByRegex'))).to.equal(result[x]) + // expect(checksumLine(sanitizeLine(set[x], 'sanitizeFirstLast'))).to.equal(result[x]) + } + }) + it('allows for skipping sanitation', () => { + const set = JSON.parse(JSON.stringify(data)) + for (let x = 0; x < set.length; x++) { + expect(sanitizeLine(set[x], 'none')).to.equal(data[x]) + } + }) + }) + describe('checksumSet', () => { + const data = [ + 'two1nine', + 'eightwothree', + 'abcone2threexyz', + 'xtwone3four', + '4nineeightseven2', + 'zoneight234', + '7pqrstsixteen' + ] + it('can sanitize', () => { + expect(checksumSet(data, true)).to.equal(281) + }) + }) + describe('lazyChecksumLine', () => { + const data = [ + 'two1nine', + 'eightwothree', + 'abcone2threexyz', + 'xtwone3four', + '4nineeightseven2', + 'zoneight234', + '7pqrstsixteen' + ] + const result = [29, 83, 13, 24, 42, 14, 76] + it('can match text or numeric for checksum calcs', () => { + const set = JSON.parse(JSON.stringify(data)) + for (let x = 0; x < set.length; x++) { + expect(lazyChecksumLine(set[x])).to.equal(result[x]) + } + }) + }) + + describe.skip('integeration', () => { + let initData + before((done) => { + fs.readFile(filePath, { encoding: 'utf8' }, (err, rawData) => { + if (err) throw err + initData = inputToArray(rawData.trim()) + // Deep copy to ensure we aren't mutating the original data + // data = JSON.parse(JSON.stringify(initData)) + done() + }) + }) + + it('is not done without sanitation, since that matches part 1', () => { + const data = JSON.parse(JSON.stringify(initData)) + const result = checksumSet(data, false, true) + expect(result).to.not.equal(54953) + }) + + it('is not done with a one-time regex', () => { + const data = JSON.parse(JSON.stringify(initData)) + const result = checksumSet(data, false, true) + expect(result).to.not.equal(53885) // result of one-time regex + }) + + it('is not done by sanitizing just the first and last strings', () => { + const data = JSON.parse(JSON.stringify(initData)) + const result = checksumSet(data, false, true) + expect(result).to.not.equal(53853) // result of first/last substitution onlye + }) + }) + }) +}) diff --git a/2023/day-01/index.js b/2023/day-01/index.js new file mode 100644 index 0000000..af7e035 --- /dev/null +++ b/2023/day-01/index.js @@ -0,0 +1,3 @@ +// eslint-disable-next-line no-unused-vars +const console = require('../helpers') +require('./solution') diff --git a/2023/day-01/input.txt b/2023/day-01/input.txt new file mode 100644 index 0000000..446fab4 --- /dev/null +++ b/2023/day-01/input.txt @@ -0,0 +1,1000 @@ +9cbncbxclbvkmfzdnldc +jjn1drdffhs +3six7 +38rgtnqqxtc +rxszdkkv3j8kjhbm +54nzzddht8ninelrkkseightseven6 +6fourmnvkgnthjtnjqkr +nineninezsstmkone4sjnlbldcrj4eight +3rdnfkvrx4twoqgeightqkgmn +6mlhponeglrzrvbsseven +five8spgxz8 +3three2dfourfour58 +9q +two45 +five8mpkpdfiveeightfourseven +93threefive3three92 +6jdzeightnineone1sclhzrnrjxfive +6nseven16lbztpbbzthree8five +eight6mntljvbfrmftsffchsix4 +4twosixvnhjl +8d +eightsevenvqvzlqxkbm6rqhsgqpnine7twonex +seven96 +ck8tv6 +2mcddqpnbxssmrc6 +zsfbkrjjqpbbone6six2 +fourone8hfrkxrr +7ninetphdpcx +9gcn9six4mfnjgtcdc +6onevsevenseven4three +ldssggpknine7 +twolltbfkhltwo892jngx +rsh1five3ninefourmfk +xtrh534nqxr +ggllvfjthreefcckbninekspmf6bnpkvt2 +kng16 +kqgthcleightsixeight96nine69 +nchcdqbsxmeighttwo65onevtqrznxmnl +chphzzqb6threeqhspbgkrn6 +8threevbqxl4 +99eight2hgdvcqdqxsnmkxctskvxxvqxjt +9qb95oneightsf +tfmpcthbdbnzhldnbj96 +9mzbn +twoseven9rmssixbvbjpsbjbh3two +7fcmfjjrtz5six4 +hpspsgtfxvxtmdsqcninelcjhfb2mhffpvxkdxdlvkqxnine +fivexrhxhtfivesevenone3d +jvc6twofourbgfgrthree9fourmhz +8xfvjdqlgfrlhzds4 +5271ninexfthree +fbm1jd3onenine +2jm7fchdklthreedzfg +6lgrrzfkj +eight6sixthree +eightsixzng8dxxnfbqdjkjt1two +fivesix92j +1kfrjjq +tjjthblvjfspbcshfivesix5fivesixf +nrlx5vqzcfive +doneightthtpmjlzhgpxdc18229twofive +6shtlqlbsxnine7snthree +oneonengtbjmlq4 +sixhxvzpshrhbbk3qdxrbq +23onezbvbrlnseven239 +one883mztbhfvqqq3srfptz +zxlglzxgkltc8four6kzmqf5eightstzlqcvxt +3four452jclt +sixmone3phrxxdninetwosix +3nine39 +578nine4 +442ffhxfrxb +shnnn3nqcgfbgpzzfrtchbseven5dk3 +ninenine5xrxrhcr +ninefourdlckvqzz6oneseven +fivetwoqcnqhbs9 +2sevenbgp8one +twohqbp7fnndbjpn +2nctn1pbbxmns +46fourhjppdlkr +cbfour3seven7dctlone +sevenjxbbplfour488trzv +5qqfb9twooneonethree +7zzlgxrfmlpxrbbnjt7five5mxhd4 +99vdnnzcdsqxgbonefoursevenjjjgc1 +4threeeighttkqdxnkrgblvnine5 +sevendtlm5onetwo +threeonejvxqb15nnjndprnn5cdxb9 +hkr1mtcmqqqsixxbfjnlqqlb +six2five3jkknzf5 +9xgkspdhrfivesix15zj +nsztwo4jxfzxbtrsjjnj8 +1twofive2sixqxxrjpgbdfive +41sixgmgxzdseveneightmnmxj3 +8qrvxvfourseven1 +lms268eight +7ninethreeksjz +qeightwobrfvcssthreeeight3167 +ninetwoone532fqhggszllnfcmfs6 +838lcrhzs2 +ninedjpteighthqrgvklln3cxbt +8lfxxeightone4526six +1lmvvbl +fvkxpcjm7tzzgkxfqoneeightone7nbxzskngd5 +twompjtfbt2 +twoeight4six +zmpml5six54fourfour +pndklmzmsseven9pncxgjrnine1three7pkjvv +88kggbsqnfjngqffkbp5fivetwo5 +1xrvkpqvspt +1eightxhpd5bsctj3three +twothreexjvsxklsxeight4twofour +tvqsngkbl7eightzxhxjmrhgs +5zkxqxx +7eightfour +3pjsrdp +fivermk75ninetghvdrltz +prfthbhsftpmfcn7ronesix +three9pvxmhkscqv +sixninetqhj5two +nsoneight3eight +nineqdprvhtqb4fbone98nineeight +ktfgnssfourdqktfflninephpntvmmm139 +qvxfzrdnineonenkpgxnnmqpsxrlcvjq5onehx +cq57sixeightwosvx +225vntfqdzqjgmtkdgbxrsr6three +56981stx6xdjkhnvgq +xksevendzeight2sixone +863 +onethreetwofive5sevenfdmrmczlqs +28qcdzgrbdkh +6two43four +fourseven4 +1jgone +qmpsfscxxtsix872 +6sixthreeeight +1bzxjhv2x +onecs5one7five +vr4phpdtmk3fivebtltwom +4onefourqqrpvxn1two +3sixsixjkfssevennpjfzhhsg1eightgj +seven1eightsevennspvrr9 +5fiveseven7ninepkzzj +xkjpxsqsfiveeightjgv2onelhdqjd2 +sone7rhbtvlttsix +vrc6five6 +twoseven7 +onetwo9onehndznine +5six6sixr +vfiveeight6hdxnpfktttstnvhjks46 +fiveninefour8zpptwoonefjzvmnhnvq +five53 +seventpfztfccgm8bxjsjxgmz +eighttwo4tktgpbj +6426 +jjkfournqbdqkddhlninekvd56bdshtbvn +onefive1onehkhsix +7sevenm9twofourqp +26sevenseven3twonelq +46threethree9seven +9pnzsix648kgngnpxttzlqlmnine +seven7onenine +two3mtklsvrrbthtxsrgrtwo +gkpfrq4vfddfqxzppmvthree733 +fq2qbmone +threecvone7eightsevenxnmfbtp +fivetmbnqlchhtqmbcsssvxjzvlxdvznlbfive7 +fvqzlcp5 +qhgjmzntonefnlxfqqrxx5three7twofr +rczhlkcqpfkgcjmcggztjlqsgxmdxstwo88bdxmqjvlfl +npqxcdnhltbjmpjvdvfvvthreennsnbfljq2 +four9gl +gjrf94three14 +5xrpnzlneightsevenlkkltrpsqgzbzffpjhrkc +twozmxqcsgkvjtkshttglxcx234xtzhht5 +fournine3rkxhdvjlzfour +4onesnfive1sixeightwof +threeone6sjnfive +4ninefournrphscbjjc +6xsvn6twofourseven4four +khbqhgqpph5krdsrs35kfbnqdbb6 +7kdn5 +4scmksvgl8twoltonetwofivejl +mmmkthreedmflzpmxqtccfz7 +lbkdqjfcf58hpdtgjnnvhsixfive +chfbqldb52qkpzlkx +98gbxklnbcb +ghqrqplpone2ljzseven9 +fxqfour2njrxrz79 +rcbcmlvqc57btvhphxqbxvxxkjtlcvjffgxsdtjfb +crgvfkklfivesixsevenngm52jl +8rhvh5gdvx24 +qcnlc62 +pmcdbsvzgtqgczdvzln6 +fivernine29 +skslcfqkgk47four +9twonine5sevenone +ccltdxdfksnninekpzccsvtgrtseighteight9pgvlbkvs +1nine7dqxdgknlz75 +fourninebbpthvqntf6759foursix +qsvpn4mvhxgmzsevenpnkmtzrg +82fqxx6oneg +sixhnrk34 +nine5five6dkqxmnc +qhdzhhqfbfjnfmglvxctkjm59 +1threeeightbchlm +1drjnqoneninennhqt +56six6two76bs +7onefoursixhkhpdns +4threectphcxfmmksdcsvhgfx +5jsqbninelphjdmsnjl9eightql +5vkkmjkrxpntmgfkfxmg4 +6sjxrtfnjkthree +pmeight418 +vgldhpczvgr2twofour784 +5ndkknqfjjxfiveddfiveb +five1tmv +four37fourninethree34 +rcnvxqrsevenjttxd9fiverqzblpnrcjhbc4 +tdhvhhjxbbmjnls1vn8six +two4three9 +2ckvlpznbqbqblqbr +6nineseven8zfqfptcjxtfmmkqpj +2jdxnqttrjvhmbbxqqmeight +zloneightjzsxtsxhbgtwokdfour2pqmfkkqksxlfv +8sixfive3 +qxgzh3gdjmdqlpdnfgdxvbblpnqtsevenseveneighthvmqqdmr +8jjmbqnfive3bbxdzctxxn9five +fivethree38four +pdzbkmcvhbvfivexv5five5hzvvg4 +4eightkmsrlqsfcnmzvprdf4rcxxqtvpqcfjfptmk +twortrlbqqkrnqgxhgseven5 +8sevenltzbjsfjxdkdjncm +1nineqqbjsoneeightmzvfn +threeeightsevenfour3jhkcthree +nine265sixthree +nnmbqhf6three +4one5fivefivehbfktxgrdkdrgp +twothreetwoseventwo8 +bkggrdbngtjfmhone6fmvfzpjldzb +bpbg4tjqnine56zklbtkzlrs3 +4twokmxgqbgqgsseven8oneseven +hvdv74ninej +dfknvxfmczqrgdbqsph823 +3twoeightnine +foursixeightbfhlczrpjfxfive7two +fcpkrvmtzxkrfsmqcbzeight7gfourfbkthreeoneightvm +four18vgkmj2gxmtxsbnvxthree +415threecshnzmmx +sevenseven44lkfourgmqtrs +jnzsqsgznvcnjjfbblkteight8 +497three5eight8d +88onethreexzsbgprp1 +eightszseven9htqlxb +1lqqgzdrxt4qlqklftlsqzm +rqbjqpfhzfeight6oneonelllcmbrdxqhmttptg +fpqxqfourthreefqgdsmhjfk2rmb4 +qnshr9threeonefour +xshsbkqxpltjsd2fivexvtrmnlpvtwo +446 +nine2pvn12five7 +fourfourninemvfvztpkbb9nine +3fhtlpone7965four +qkpdgtrfprttrzc69 +1mfsltcsxvlcxfzdh3 +fgjfltjcsnps8three +tbeightdsdtzmncv5pdcsk +rsjvlhxtn7six +7mdtl +four3three6sgxdtdmtnfive3 +3eightninenineeightlxtqtspmklrxbknftrbh +onenine92sixrnine6tggjndsrfd +oneqdfhrfzlteighttftvfcrmmzz43lbmlbg +35bthreernjskthzrs1two +threesixeight7rjpcxnzmzfgngjpkk6eight +six9ninerxfnldpsevenone1threenine +5sixhjzkknthreenxklxbvgxfouroneseven +1zkx93two9sixseven8 +sixtwocdnqdn2vdbnblzlvffmrninetzdtdpjjsrsix +twoeighteight437nrscj +six31vtcphkpltgbcprdhvdfivefoureightwonvh +8pz +hzrggpcmpnqzzgnxjfqrlllgnqks496five +7glkjtgbsnqhnplzcp9rllkkznffnznngthree4seven +one7fivetwo45 +4nxzxtnhb5foureighttwo +threeone56lgzgdklhtwo +rjnmxbflvn5oneightpj +fs7tpvvf +fhfkcx1hnzzjjh +6onexqrfvldeightfourkcvpngj +1threeeightxxcnjfl3zbxfgmfsx +9327six +three5p86sixcxdsjjvn9eight +four1cfrxsdgnjtwo +rxjmtgsixone87 +49sevennbgqf +sixone42 +three3jblhchr +hlroneight3hsjhkl +sgtrvtcq22 +8seven985 +qrhnjnkstwo4fourthree +8phnkkdrghbmtql +two9sixncgqpsseventhreefourthree +one3six3 +1nine4rtvxxgddzhf2 +2cfp84 +khgkvntthree2 +vsznj15ninehhlfiveone +mmrxqjbjmrtwo7 +oneone7ninekmzgq +eightone84lzktckgrbkzjzkqqxlgqn +btzdblrrpfxljsix69 +82sixseven +two5nineseveneight2eighttlmvfkf +vlbnccpl1 +four5gsbz7 +418ncqrk651 +bkbbncm9eight6eightnine +3sksvljxpkz4vcxsdnztfxeight8 +fivecfrlnh51glx2dsjqseven +hnflgpth64threefour +mjxthlr1six4tdplzrnhklnz +ddrkrnssxtlkhbrjvkxpb39 +2tfoureightgvnl +79six +sixvktfvkmv5xhfgnine +86szmzzfntxxltmkffnczrjvthreethreethree4 +threethree1sixdkmjpmxtwoonezktwo +tlp189jnmskmcnkhvmn65 +41nqvvc +4xrlpc7c82oneightk +six1threecxxtttdthreeeightjc +gbhkzvnrxfourndtfc6 +fivesevengjmnbpdvcdeight6vj6 +vfjdp8 +8dfrttfrtrtfour35 +887 +714eight1tdnt29 +xbmhsbn3 +gjktjcn3kf +1327five1eighttwobfhtqcjjms +pc615 +one1rthreefive +fcjsktwo9 +lhgqvtxcntrljlnhdllthree1 +5xxmtwo1dkjpchmzfz9mndbrcbzkh6 +6zxdxzxt62three37 +cveightwo6 +ninezdvzbkggh4one63 +three7fivesix +3vcdrd881vlmglkfone +2ninenjcvq4 +5jltkfhl5onehthree3 +8eight4rgfrhseven +6vqjgxltqd18two7onelkmc +xffld1jjlqxfz7xjvxfspzrkztsdtsone +9szzhnfpssevenoneeightfour42two +four91mfnrxkcckd6vbhtxvvmzhpqqrkzxv +eight2three6ksfive +94eightslkpbf5zc5tm +4twottthree91sevenone +kjgfgq226fiveseven2 +kjrrgfmthree36ck +sevensixfour24one8 +dqj8eightthreeqzcz8 +xtwo5pcdl3eight7 +one131xnghfczdpvsczkrxhjt3 +j4qqx +hncrcntcmmpkn6sixzvjzkgr7 +9nnzltv7mclrhhgnmq2nine2 +6sixeight9 +5jgnhjmdc6xsdsl1five +onerdtqzhpbdflxvrhnpjqdqzn39 +rcroneighttvnfcngrfblvmeight7sevenonevknxtpfour3 +mcfjn569 +44 +84six +nineeight3 +one916 +eightfourseven5four +nine1gpmqdxkzmr6six +two4fouronesevensix1nscgll1 +eightthree44vxbjmvbpfleightvgbjxcgrjonesix +6vmgscbtpsnkktmbdjpmmlv +1one1 +ttwo9sevenninetwothreephpfrtjztn +cxlzcpdd32dhvvcsgdcc49rhhrqpkxtwo +8fourkkrbjsqt +rxndgg4lhkcphmtmjvtqkc9qsevenpcmftwo +jpgrzfvdx3858fourthreesix7 +7krxc9mjhgtonetqd +eight2qhspjfm +gsxplbone2pffltsp4two +hqtwonethree939zknfxrqjpn4x +eightsevenone4sevenmhxbbqrtwopgvvgn +6three5tlffrcdszg384 +frqhjmqntneightxgtldk9qpdnncxgnmqzbzkqsxz5 +vsf42twolzmftmbbb346 +7eight47 +9four94kpmvbtblbxthreefourone +2eightctvgglxjgxvghmsbbfivedbnn +qxkvzrpkj79lkqtt +jfjgzhrkpc77nine4nine8 +prqrpms3ninefiveqnhtngpbsix +gqxbtbtvdoneznqsfpf9threelmhn +four7sixjrrpqdjtbsfivetvcngclhrprmhftwo +9sevenntmbflxvf1eight3nine9four +twoqgxklrmdzntrbtvcfsix6 +zzt8kt2 +ninethddninexszntlm9jnqnbpb4 +lcsfourxdsk9297 +f2v96nqfour +2tqsdl +7onethreegeight +47q667gtgj2sm +8four7sevenfseveneightninettlqkc +5ppkgf +61threetwohxlxzbrlfjk +6fcfbvhpzgl57 +five2tjkc5three5onepqlgv7 +five925ltvrgpzm +jjstljfs47threebcg +37seven +8three47eight2pqxcvbn9 +threeeightrdndbtfpbszscbqzlhtq84 +xvqtwoeight4twoone2seven +fourpcqvbcfvz8onefivev7xvtxdpbnone +3956fivethree +sevenlmsksix55 +eightnine17 +5hgqmlb +eight91pfnngffzl8dxvlnnninektlmqq6 +5one8two9hh7 +qfourlvclhf8four +9cfgrkdmeight +foursevennvxcmjzzpjtwoone4onezlht +njpjsceighthqqeight33two8ppjf +four5djlmjfive99eightonefour +jcf2s6gmzrnjrjkvfkgone6kbhjc +threeninedbskzbqlb8jpnine7 +onekbddzcdx5eighttwo3 +3xms +mmvvxmpthreesixone96 +3djjfsixtwo7threeeight1 +339 +3seven6 +six48nine533oneightv +dkvvxngfrvhktzx3sixvqtkpqztzs49 +1three5ldzvllvqeight +9hn8mdcpvzqlzctwo922 +164k7 +sixsix4c1tnkmzkqconejvdkcdfntpfkpsdm +31qvdnvdqthreetwofour8two +htwone6fkzkrdfcpqlnxlone +2875 +6eighthj +7lcchpx3x96fivefoursix +kone1ptnkjhks65sixrsseight +23sixninefour5 +xdnzfbpq2 +hbznxkpmktwo3six6sevenfourfive +tbbcrzdtfive42eightsixfourthree +5mjgvsrrdlnrvlr6ninevlzrksfj2five +cftlmrsptdjtsndl2eight +snmkjqprhk57ninetm383six +krxqxdgdp7fkgtmdtbx11fivenqncrnhcone +3sixmcfnf +7jkfgvjlbkfvlmf9 +sixgljmbvxoneseveneightpfvvnl8 +523eight6 +seventwoone3rnxghtb9sixbkqch +5hjzzvkbls9qrcqtrrk19hbrhszxfive +8kbhdp483four +seven7five1 +dklfjdmzc15zchbgmbqkgzgkn1sixsixeightwovz +8sixqbpgkppjktwooneninethree +seven16 +sdqvhfour2glrqkmj5fiveonegzqsdtgqskjcmgg +twofiveoneseven1rqjvrrxtwonen +rmgdlx9cskhdjlmtwobvpnjrcxbfftd5 +93mcfoursix4 +gst66jphtlbtngqdqdnonefoursevenseven +seventwotntksixjrczhp557 +svjgzfkbj3five +threeninetgqdgnmr1xxnxfxlninertwo +jjhpnine33hjnine +twojgcp9 +vgkbmkzrbbvlhv267 +fournine1eightfourcdvvdgkmkndbkrheightone +1onefourthreecqbffjdvtzbxdc +mmgvhrmftnmtdkqgf29 +7qnsfqj3one +qhr933three5 +pqblftsix3 +one7sgsqvszkd6ztwovcrmqbjthdthree +1qgqtnfive487czvxsjk +37jrggmdknnine +3sevenfour7sblhjqg9fourgchqkdg +nfxdxlssvgxxvzgksrkxqtwoffpphxdqjzh9seven +4qjhllxzdb8sgbgksxbblsmftd +twofivegddmhcplptf13five +twosix55 +3zcftpvkbmzpffpjrfouroneseven +6vpsixhprqhzthree +eight7ninemsmfbnqkzmqfd +nbrtxpkpb1733 +nine33jbrkqlnf1 +14lgl +eightjjvclfxp9 +fivetnmksk8cp6 +dpxcqhbkvlhccb1fntmcrjjgccnszct +lbjppg3hrgpzstfqgbcrzbmn66two +dxbxkzmpzzthreestcvtvhftgzctnvnshzgqtbgxlrqkthreefgxdrfmm7 +31lklpfour +nineonezcmrfppsvbg7seven316 +9cxgcgjsd8ctgbh7 +9vdnine4five +2jfkrtdxvzq +qgzgclpt8jltqzkpvddtm +614two +pjb92two5sevenfkb6three +twosix766five71 +86vbnpsixthreetwonevng +onel2one +4ninesevenpgbqgpfgkfzdsixmmfive4 +7xgcmqfqmk7twothreeninepdt5 +sdonefour77one +fourmfhmsznxffzpgdonegck6nine6 +rxjspvttx6nine1knbsl +pljd3fourone8 +8onevhpeightz94seven +jtmdhqjn5eight +onefshb1sixone2b +seventwo4gnrsrpnfppseven2 +147fourfour +9hfjjmgrzntssjpxcvbzpvmqzgsd54twonine +mprnmlhxsdtntbknine1 +98onefivethreesix +gd6ninejhsrhsevenksvkcone5 +fourd9xkcsrncpdsbqhcqg34twoneb +pbx74kfivefourvmslqvfbml +dmxmpl71 +594shxctmq6qkmnbrm +5qrhd8jmmthjkdzhrxf6two +four4six4three +hlrsmmjjvshzztxrnznmseveneightfive9sjddhlfvftbtd +fivesbcklfdrvz4pcxsvdcqpeightgj1tqkfnv8 +tthree4one9 +nine4sevensixone +eight5threethreesevendrctkthree2 +4ngzcbtwo451bpvbtqdvk +9two1tdjdlrflshfourjlkdctfp +sevenseven6132sxqfspmvxjfvh +fourfour28562 +seven6gsevenrtcsldgztqthree +sixtfmvfnkqxlp3sfsmdlfgh5nine +tjeightwornmddbpcsckjrzdtvzrxfivef4 +gcv42jfcdftseven1 +166eight73 +5tlcxlscsgmrznhlgfgkqdpksjllz +2one7twofivesevenqxrdvbczfmt +1bdqfdmtfrtx2svdltfzknnqssvxvsdkzvd3six +rzztvfourfive754fournine +71vfsixrjhdqqj +one63csbsvkqmkjt +3seven4fourthreeseven36 +bmkr6threefour +5fmr6four6mxqj +3five1one93 +5nrtfrdhfv4mkkhnine4 +mklgxhrpp3two +hpfrfpkddsdqfgbhtgfourthreetvdvjrfr5 +five6ttmzjcs +tlbzoneninesixsevenpddssz2 +prceightwo1 +vvktl7eightltmtpfcrkndtkglhndhvrbtfv6tjgrzqv2 +sjpzgmvmddttrcmnvzseventwocgzc4 +seventvxrsbbbzrkfbkbcrbdsdtv456 +ttwone12threesix +85nine +hdgxncxv83 +58sqpgnine +65mpmptlg +threefourxvmdkhlqd2three8xfphd8 +rdptwo22threergcfdntchfqbsseven +98jbmmhznxvrqkdxseven1onem9 +eighttwo1twothree3sixjbjqgzx9 +9klnfhhx +7three7five +jlvrcqflvfivenine7cnx +fourrr2ktmdqhsteightwot +bkjgtqxg9fivenrsktmkmxcfbg4 +eightjj95six +6threeonebmpqrnqgdqlkkqc +seven5bbzlsnvjchbjgxh +lbsptf1threefour9 +37threeeightcgfxdonebhkrdnfive4 +379263 +24vmbb +9v +1seven15 +slxpsix3threeeight64jlrnkmkrqr +ninedjlvkxqh24 +sneightpssteightpdfzqjcjgsmseven7one +sevenninehf6bxjtfntwo +four2cknjgdkbqdvl4 +qxrzdhsjjkmtggt42 +tgsjddrgtkthreefzlvgrsix32onetwo +3zldxonenrghfnhhmptbpgcl973 +8ckzrgrzbone4sixdspxtwo +xvxzprh1mbpspkrv +lgchfzs6sixthreeksix3khvhldq +sixonefour2mxslvpdhk7 +6sixsixbghxppnztxfive +jpdbbeight57seven +2onethree61jtnjjcq +63lfsznprjddqpfourcrkthree +ninepqmvc2xtwotcjcfcvht +1bcrpone +pntsrhj725xblt2seven +12nine1 +8twooneseven22 +hfrbxdlfzmjvdslseventhmtqzbtcmsqbeight5fiveqlglhclrh +three6htff5nlzslroneightxm +hzcdc6threeseveneightmckdvg4 +tgpjgvlq6nineseven8six2foureightwozbz +three83bbbfsg +75bpmqvdseventwo9four +2nine8vjdcgvfns +rjndbbfmppgd67eight3hztlrqv6 +onefourfour6 +two5six1 +954ninetghsfnnine12cmp +6one8nfzstqlfive +seven1sevenjrslmhrfivexsk +nineqjgmh1onekqblsvjkdn2five +eight94mzsevenvtkkv1cdqgv +xsix4sixnine +3pbxzxrsd +77four9 +8zcthreethreeninettdb +c4eightsevenxqnhvclfour +gxbkjmfhhzlzrbzcmjmmnqxlsevenninefour4jcbgttq +9zptjlhzkls1fdfzxvpssleightsdcmfour +8oneone9one +1threefive4zkslknvmr +9seven4jninesix834 +sixdpgkdnnt4kjzgrjtcqlqq +1qmcgnclrdqrc +5xhlzgzpzbgzjcsthreefive +mrgcpmnnsixeightfrtjfourjlbcdt3 +129twokqmtmdj +fourskzt382mnznt17eightwocx +9hnx914mtsnhnxtgn6 +81psmkhzk3fiveltthree +lhbtwonetwo3dvrtmpjxk +x859zhjsbzjpjrtrp6 +73mqrrpdrs +43d73rbddvrz +rnmrrcndm6 +x6rrnsmpgqmp3three1 +pbtnndfj2onetrvmvcftf4 +four6rfoursixfour5three +69nine2szkcnjbmcvjtbjbgnmssghfdlninetwo +one92phhclzfsfxqg +hzc2 +fivethree8threedttx +pjdkcdbxt5 +trgggsgx92six +9ninethreeknlvmb +eighttbbxhxcmrjqxkbjrdcjv5nineeighttffour +9cghvbkpmkt +sevenzhlcljtqcntthp7rr2 +3fxgjqpbp +7nineqxqvhgf1plh4 +2fivethreeeightsixfiveqz +twoeight8 +fvvjphpmqffqjchvtfivemseven8jmlrk6troneightvf +three46 +tktkqsqskrpxl7thfrqnpdkzcjxvfmmbfourone +9twosxgk +g3scthxjb33nppgcone +mxmr17fvcgh4nine +vz82 +2threefivenineeightqmjlcsbqrcg4 +xptfourthree7mcfeight +bdthlfbqbczmlh56 +onesixlpqxqlkxxkqldhd5twop36 +seventvptphkhhjvslhtphntwo2cfnn358 +shjlthfxpfive4rzm6eight +three565nine4 +xfjoneightsix47sevendvtxtfive6 +9717one +rkmhlxzpxgfourthree7qhh69pfds +3threepvknzvbmbrvljtcx13three2 +1zbhchxzlccgfvbdtlmfr +28hqpxjvxcmnqtxhhgninethree +eight3phxhpsxnz6nvgqznb9h9 +2klvscxmt94fourthreekgmqgjbhnzrxtwo +7eighthmbvjlfseven5 +k45ls +1qkvvbvsixsix8threeshqknine +4928plcrzs6 +gsskhbkmfzq8 +6crvcnine3six7eight1six +fourfive311 +lqdjctn82sevenjdm6twoonesix +8three8pqhmjjc +hgn6mnpmdmcpzceighteightnxkvjjfrninejmpkrfzcgv3 +ln7pqvg3sevenkqtztcpjrxeight +dqbrjj9fourseven +4sevenone16 +9cxtchrmsd3fflmkdzdgp +6349 +sixsevenlqgzcsvnd1 +kjfcbgeightthreejvqgk874 +2vkrvzpmfv4eightwob +mhjrqbvlmgsixthree74 +fivekldbzshd37 +seven11fourcgvnqr7four +fztzttvmx4vtwoone288 +5fivevlbqczq1 +twoncljq5 +jljz388kklzbronetmmf +92twonmjmkhgqfhx66 +2rh9nine14seven98 +7hgzjblbxpkltmjmlpscd4svtwo1two +xcchzd8fourhkrstwo9three +lxgxlsdkvcfxsclj4cqzjgjgvtmkjlhxfnmfc3 +86hzhn8eight6 +qhsevenvpg4hzffldbrvpxxpthreeqpvvdndv +five6eight15 +mhl8rcdkxx8 +3sevenv +one131eight86fourk +7hsnjrnlcnb8twosevennineeight1 +2khhfivejgbknv65 +8lmfd298dlnmszrrfive +one5ppczgjzzsix +nine6fchgqxsdjhqtwoqpqffhn6ncjxx +bfivehtndrmm62 +829fourbjpmbfqkqgsixtwo +xvcdsix3five +sixhqthreeninethree7 +gpsdvj3zpztgcndvcxz12 +seven3fglvdzxzcqfive7four9five +3five4gzgjbpptwo +nine44four8 +4143two +fourpcjhfjrxdhvzf2dkmszvtjx +ninecrxdkznbg8p72nine2 +sevenrbzhktn7five7sixvklcksmb +rndrrfhvl4zscdjmxcbrfvrxxjxcc1jjfsjntxzqp6eight +sbnkdhrjrthreecvxqrfxccdpmqsix5jjpkrxfqlnxbzlzskcfgr +two4xdtsdjtneight +teightwo5k4eightmtheight +eightzlc6lgtgxsvckm56 +qzrzhnchttjnjxjnine4seven611 +ninefiveninefqtd37eighthzghljhhv9 +bcbbbneight1rqjtnnzrv +ninegldjhplfthreetnqcbrllpvjtlthn9xkbqkfourthree +nineeight4 +4zvghdrbl7onemsbffzkjrb265 +lthreesix7two +vvkfivefmkdsbst66 +3rxdgrfgdtwofourseven +onegzbdndnqkkckpsh1vftnlhnqjcvrm4 +gpjld3lcvtzckpqrdghlpz +3ngvnzone3 +sixfive8eightnine +tscdrcsvztsnktrj495sg +9twojbzsqv64smmhbqc +8ninefour +ttlrjjxm41four +qtwonekvlm9tnmzlvpzzs1 +twotwo1fiveninelcl +8rfvffourtwotwosixpqmthjsrgj3 +nineeight2seven +n9 +two7fivenrgdqshs +nine5foureightwom +4tworvqpsxzhn +18ghsmdrjlq8rfpmvqtgcl3fournine +8three7qfskg +nine82 +41zb5 +5cdpnqzfbthree +247pgfzbhpbkxsk88three +xsnjsqrrpck8two7 +qmlbtk79vqnzqv +pkcmsixjhqtfzvjv23 +1nine1lnzeightlpxpssdgtsgbvqmftmnv2 +threefourhkfckgvqn721 +onethreegzninenineone31 +kttwo4gcpxpvrp +4six4632 +7vmkqlqfbbzkksix3 +jsixpckt8six142nine +two753six22f +twotwokjgftjhnhp6onefour7hqzlcbkz8 +ghmjbfv49pls68 +three1889twogbcjkkzc6seven +twothreethreevkmlsplbgninenine4six +seven7vhjqfmklxsvc +pxnxlqg91 +fzrbbvfsqrgcgsjjrd5 +skcm59mfgkjvcffourhqzsbpt12five +three9eightvfksbfcf18vlvgsixnine +oneonefour9eight +zjrxbvxtjbqtqqeight75rf +flk6gjsixtwo74five +4jncmngfmqxs1four9 +1fivefivefournine58 +274 +gjjzjfplnninetwofourlgpjfpnkhsixthree3 +4jnthree75tdxnhpm7bv +three82jgvgjthreev +sevenfour8 +threeqfrhnkfkj2jhzllpn +one859one +8xtnbtqqh41fourmgxzpdv957 +37126 +zzsixsevenfrszzvchj9hxhdcxmxqqckclmfm +sixsix79nine +s2mkfdqmcznztqtgddtwo +hpvpdczjvkf8two +6two73hdnbrkgblgfiveblgzksljjjskfpthree +7kndzrhvcnstgfxjlff9twoninervrknsffmfzmdhtth +three81kqcfplcf7 +fivegtfbnspddkeightmmv4bzksixeighteight +596ninefqrfvpfs +sfpzkhxqp7 +kqd5 +1sevenljlmfh6 +75sevenxpzv8ngffsnm8 +rxqnshcskglgkrlhzone4 +1threespckmrpdnxfoursgqsevennnrhjkcrlbnine +hffpvhxzznnkg86one61two +7bqm5sevenkdnmqpqfvvtbsct +3sevensixnineone5nine +two1five23dgmqsgzftflfmjseven +jzmltz1jtlnsbgtsix +nine1sevenjltnln8fivenine +svmzvnr8tfxqlxnc1 +sixrmcsqtphlk5 +fourjnpshskvmsqscq8threethree8six +3threetrqfhnbtsbckkvf +5fcmhxjhpr75zkcbqgltq93zcdm +eightlzjjtzk3xlbvgnsfoureight8 +hrrxxnj6nine1two +zbnj6txxhqgdtq21fvcjxvkkrsrrdmkrmvtbjhs +threex86three +onesevenone6four6twogjtqp +92onethree52 +qgzcfour9eightbsrseven5 +rnrlmtqdqlb6 +sixeightzgbf788five +sixgnlbxrfrc1jseightsixeight +nzxgkcfive44tkblnn58jbmdg +49fivefgsk +one8zqztmlmss6fiveznccsnnnzqk +6fivesevenfiveqrvlmdjvjxzvsix +sxdgthcx9 +93qlthreefivezmk +xvsrgcsqbsqhdnqhbmcgn39ninesixszgtlslqb5 +7sevenfourrpfqkkrm5 +vcmfthdpkzsjpjt5six +zqtrk35lvsg1jkdfour5 +7threegqzhcnxcmcrpgjkttbmxq5 +2four25seveneight9 +3h +xcspfcvfive37three7three +nbsnvhg197csnine8gfttndsf +xzztbq981vfbcmrv6ddrhrnprrnj +5kv672fpd +two2nine4fourl9foursix +ghngfb2four67qsfhpsb +4ninetwobcr49sixfive5 +2threethree7onefour4 +824zpsjjm +four6twosevenqbmzone4fourph +ctvmbrvksix18kz +eightfive9twokrpsltfhkjkjkdqlszzs4glxxtgktsx +2two7hxrncxqeight +6121two46 +fivefourthreefivemkmb5fourmqfhmlrxmm +fiveonefivevbtbzrone9tsix +oneonethree5sgs9 +cdvrrsm53qfvdzhvnlprvfjcx +jtflvmkdnineone62klbvbzzltscsvmbsbp7 +3mjmrxl3fourqfqhrknblcthreelfmm +seven1vjlvfmh5flmnhfzsixfbdsjdkxqhvmj +sixfourninefiveoneklpsgrtthree5zcpxs +eight73pjvcbb +gdgbgzdlgjt9hklsxglkrtwo1nchtbvltmxnzn +87sixkfmrdmdx +jrpjxhqkleightttpkqqrtzvb5threerx +xdrjhrrnmnblnbtone8one +618gnvslkkmm8 +tqrctvjjdone39 +6zs9lninekjbrm1twosix +23mxsmvfthreefourkhvxmqg +onemkhn4seven3mtphqnqb +one5foursevenfivezpgvjmdhl +2mskpg1threevdjjlzbrbsevengfgmqvd +1hrrxgxgzhj2eightfive +rlleightwo2vfq +2gss +4lndrzf7vcfffcb +four5one +three5lxddbbpccp6kkpgxm +sixcbqfivesixzfgbzszq3tqcfqk +8xpsixrcfoursixfkxh +rqrtwojhcfive89fourthree +5ninesixknjmlcjmnsmv2 +18zgjpthfsix2ncmdfkcsq3 +tsxbfgzhjr55seventhreesxnnjhninefive +three61rqljxqdbch +threelhqzdvxnsgkmthmbd7 +54two5dnhx5 +btc4ggbfmtfscgzfourtwothreeqptmrn +rvqkbtvg331nine +2fourkrgdhvkgzrhvqonefive +7twoxkcpbfxthree +bmzczfl54oneeightqslllrcjkm +sbjgbkn89 +9rpq83seven +fiveqmqklgxkqrrckkjqsxkkfjzzdml87seven +74one27 +45four3 +pbhcrscll27five5 +7bbmjqlxv1dbdsvsc +sevenrhqt9sixthreethree +4sr8five +kc1 +1eightlkjrvmpt4sixtwo +1oneddzcqhmgd6 +feight6tmqcj4four +five9mj7mgjqfhshvhxskjsix8 +six99eightg42 +vfhvmxnvhxnonekrhfzqpseven27 +866three +eight5one118 +clqlseveneight8dbrqdlcf7 +9mnlgvcfmlrsrxfp7six +8mzlgnnqddptwoone +3bqckbeighteightthreesevensltsixf +eightfive37688eighteightwof +sixfour2one4zzqtdp +mkx1twofourxseven67 +1jhcktvgninethreexdmfqdbjxltzj7rhkdbvf3 +two16 +5443 +1three52nine7threemszlkc +jsgqfbhmcmtjmxkq1brl9eight6 +fzvgmbbeight8 +861snhtzkvcpnr4 +b6kpxgcv71six1 +c5four7vcnqhd84threefsklxc +lcpdthree2three9three +kljtp912sevenctg +241zlv59sevenqdbvddmrhtfgrhxgxmb +hksp3gdmcldnvbts1 +xvcpr86btlptpnphhsix5fivenine +fivehgtmtxtwothree1seven +fpjpnrtpthreegjmfsjpcsix4twoonejqrr9 +2hsrckv4639six3 +fourtwofour3 +2kggzqfourfourseven +three526seven6gbmjbqkncg +crstsqhvt98rhzvhsshthreebj +xbhcfqvplfive9one77 +7256hlnmqlhth +5rttvcfhbjzoneqdbhvtwoneb +7mxgmsvxzzsevenclzbcq3twos +xcmpjgk9 +eight5cnntwopzjrmgbhq +rvbfnddhg25lpthcsfxfdkmseven +eight82cppmnpvkvthreesevenseven8h +fourhmpknkfdtwogfhrgthree84 +6six3599fivejhmjzdzsr +sevenbngxnjljfivegcvtmnt5k1 +twoone58nine +29jfctbqssvrtwothreeg +84kkjdjjp +threektqgtgcrccbsnsqpcfxtb3vxtdfour2hgvdg +6fivejttmkvvpntvqlfpbjbcfkcztltwosix +twoseven7qkhzqlx9four +ktnxrj2sixsevenrcnqbksgbgdfxrdqgz +236four +grbhpnjrtvrbslnfgthree47vbpncxqfourfp +5sevenkrlmnrjsix4 +5fivekgsxtbvkk +2two4 +lkrjlsz7mgv9525p1 +nineonebmfdxxfqvvkrblrd9 +5six6cvmqttbsxkzg +42seven13four4 \ No newline at end of file diff --git a/2023/day-01/solution.js b/2023/day-01/solution.js new file mode 100644 index 0000000..763869e --- /dev/null +++ b/2023/day-01/solution.js @@ -0,0 +1,34 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') +const { inputToArray } = require('../../2018/inputParser') +const { checksumSet } = require('./checksum') + +fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { + if (err) throw err + + initData = inputToArray(initData.trim()) + + const resetInput = () => { + // Deep copy to ensure we aren't mutating the original data + return JSON.parse(JSON.stringify(initData)) + } + + const part1 = () => { + const data = resetInput() + return checksumSet(data) + } + + const part2 = () => { + const data = resetInput() + return checksumSet(data, false, true) + } + const answers = [] + answers.push(part1()) + answers.push(part2()) + + answers.forEach((ans, idx) => { + console.info(`-- Part ${idx + 1} --`) + console.info(`Answer: ${ans}`) + }) +}) diff --git a/index.js b/index.js index 39627a9..a1611a7 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -require('./2022/day-02/solution') +require('./2023/day-01/solution')