diff --git a/foundry.toml b/foundry.toml index 7aa51f9..412367e 100644 --- a/foundry.toml +++ b/foundry.toml @@ -2,5 +2,6 @@ src = "src" out = "out" libs = ["lib"] +memory_limit = 300000000 fs_permissions = [{ access = "read", path = "./inputs/"}] \ No newline at end of file diff --git a/inputs/03.txt b/inputs/03.txt new file mode 100644 index 0000000..347a0de --- /dev/null +++ b/inputs/03.txt @@ -0,0 +1,140 @@ +...........................................751........501...................................890.231...............829..168......143......... +......................................*.........841....*....../................+..311.......................441..........*...........202.... +.........332...60....537..697.......901.................609....678....261.....90................870....519...........272..449.......%....... +840.........*...........*....*..968......273...440.415..................*..................&......*...*......447................883...&..... +.....34....651.786...646...804.*..........*.../.....-..........=...............94......96..760.222.....637.%...&.290...55..368.....*..565... +.518*..........+................717......80.............231.....610...810*942..........*...................640...*................499....... +...................189......................772.........*........................550..626...676-......213......432...........790.......834.. +......995..=..........&..955...........391......387...825..362........481...334..*................579.*...84%.......506./................... +........*.700..............*..........*.......+..*............*...170.%..........733........658..*....832............-..777..616.915........ +......322...........977.526............852...410.14.........28.....$.........831.....%.....@.....586.........69.....................+....... +...............................30.........................*............./.....*.......505....274...........=.#....344...52.308.............. +.........123*504.................-...-..................15.968.....427.439...456..............*..935../..441......*.....*..*.....195.794.... +..729.............186...............786..476*168.653*.............*........................148...+...962...................271......*....... +............23.......................................435.491*4..40...-...26.......11..133..................*758.-....215.................... +466...656..=............957.........463.............................372..*..............................894.....661.......903.......%283.... +..................421...*.................32..946...%....668.886..........30.682......108...........................292..................189 +....759......379.*.....380.......................-..942..=....*...891......../.......*....193.......703..946/......./...................*... +.39*.........*...847............80-..........................886.*.....+.............418.@......763............432.....................873.. +...........916.......................335..369.....................651..624.445*809.........*695...*..267.......+....666.641.888.480......... +...@38...............685..149..........&.*..........312..................................27......544....*.420.........*....*..........*750.. +................&.....*..*...............208...........*...361.....&.....569................*986.....295...*..751..499.............549...... +.....@.......626...390..761....*.............$.........319....+.289..250*................189.....947......804....*......742*................ +.....660....................342.582.......24..611..................................354.*............*.............997.......595....874...... +...................................................109.........*...108.........252.*...168.@88...973................................#...$... +214.....161..............499-.......................%........497...*....419.....*...52.........................83.........*5.............739 +.......*...............................156..........................88........798..............204...............*.....444.......974........ +.108..39..................*651.395#.....*.............963.......911...............886.................532.....417...................-....... +....*..........921*82..321...........597...@321..409.....*254....../........237...*.......................&......./...................=308.. +.954..110........................................../.................478....*.....423.........990....187.429..#...506..........614*......... +........#...211.............................575.....................*....893...13.........811*................673.....85...........465...... +..................393...........782.............-166.........=.831...401........*.348....................477...........*.819..#261.......... +.....761..........*...536.....+....&.................549..427.....*...........924.@......283.50............*........795...=................. +....*....-.51....617./......954...................................248...................*........776....407................................. +.756..986..............326........516............387..342....336............142=.823..354....233.............$...........523........336..... +..........+495...856.....*....363....*188.177.4..+..............*....=512....................*...835......458..348..........*.......=....445 +................*.........638.$..............*.......214.........190......293...........&.128......*............*..167......711.........*... +...565.......842.................#.....................@...............=..#...........195.......984.....201#...530...................640.... +...*...........................289.401.......251...626..........198*..277....$..............%.......................719..........964........ +...397..910+............................$....*.....&.....936..............893../...........249.....#334.299.../503..........*....*.......... +..............................918-...420...405..............$...226.729.........910........................*.........892....76....511....... +...674=...282*418......#................................................962..........=693.....760..........499.908/..../.......$........*926 +.......................906..../......485*..........*......9....+....564*.........132.............*..........................137............. +...........................300...........960....503.572......490............608*.......*....504.979..25......230.................925...@.... +..673............787..............................................433*164.......961.956.596.........*...610...............340.......#...765. +....*....................8*............217*805..................*..................................427..*........680*....................... +....247..............699...848....................651........137.870.673............696..................660.........432...593=.........906. +..........209.........*....................327............................%..........%................3..................................... +.....%.../.....482...708...782............*..........520..................947....390...420.....%..............456.....667*544............732 +..305.......%.....*......%....%....220...419....608..*.............282............&....#........269......117-....*834.........546#.......... +............137...145.128...........%.............*.555.............*.......................=............................................... +....988.299.....................940.......978..971........437.......79..........*.........238.497...490.912.539.........85......157-........ +..........*.660....335......../...%........................*..224........@...556.368.............*...*...*..*...551......................474 +291..217..1..................321......-...................17.....*.......810............411......573.688...273.....*....$.............../... +....*.........=.......701............781...*....................761...........315........*........................895.445................... +...............467.......*...............444................................/../..........925..530+....50.....-............................. +........+..........545....970........./........767......543..............953........389.................$.....393......#....17....83........ +.668.....577...&..@............803.....931......*............829..226*.............*.......-....114................=..9.....&............298 +..../........239.......101...&...*..................419.....@.........922..17......598....704..*..................722...........768......... +.................776..*......156.163............419...@....................*...................465..+....................206=......-.*...... +....506.891-.......=.499....................@......#..........992..664....270......*395..............519.......666..............37...566.... +....&........251.................671.=.......474......429....*..../.............776.........#.............260.&....319......410*............ +............*.....46...772.........#.441.........340=..-.....393.....452..../..............219......487..*........*.....697................. +......993.507.............*.........................................&......515.231..#988........./..$...519.=...435.....*...........753..... +..177..=.......*17.........341..62.....361...820.....647+.........................*...........939...........980.......7.145....310....*..... +....*.....113.....................*17.....*..................583....585...........160..571............................*..............75..... +..937.163....*857...........322.......233..436..973................*............................678..................304.....#140........... +.........*............@................@.......@......412..521......917......+.............................................@..............32 +.........233.........5.......649.230..................*...%..................513....304.........745...85..31....799........958.............. +.....983........@...............*.....#......%.....340..........=....745...............*565........%..*..........*.....835.........../503... +.....*.......558....71.............518......546..........148..439....*.......567......................870..846$.519.......#..../17.......... +....945..381.......$........671@.....................390...........870..........+..%.........*..198..................347-...............164. +....................................................@.....-...............318......555....234.9................@..................134....... +..............489$....=608.............................&...40.*.................................................291.274...489.690*.......... +.....=485...........................633.462....+......490......370...............-......740..........416*..............*.................... +....................................*.....*.....125./.....#656.....679.....227....455.-....*.......*.................414................552. +.................227..............733.$....787.......532..............*879...-........681...148.188.426./.......547.........=.......645..... +....+..21.....75...*.....797...........73...................491...12............27......................727...@...*........912.............. +...258...*572.....955.......*..&...585.........*.......800.*...............@...-............................357.........................+... +...........................415.389.*........973.738.%..-...759.968....477..192.......553...#.......398..170......900..../..986..834$..758... +.........10......816..676..........356..............78............*..%..............*.....372......./......*..........322................... +.....122*..........*......%............590.............870..920..911............246..452................740...............199............... +...........444.....738....505...@.498...*...=..........*...................................946...................+.*......*................. +.....675.....*................180...%.957..335.295...&.222....-..........281..625....@....=.....695...........173..652......141...-......... +.......*....860........842.........................453.......809.....167....*..*..308..........*......902.101..................+...894..130. +.....834...............*....=138.........................224.....301...$..663.9.......58.....899.........*.................................. +........................948......................908............+...............#................74........384.454......69.....*724......... +...............4............84........990.949...*...................480...961..230......18........#..367......*............................. +......496.......*289..........*11............*..........139*129....@.........*........................*...255................532.....607.443 +......*................888....................812....................876..364......965....105.....446.612...*..12*............*......*...... +..374...833.........19*.......904.........432.......153.....78/...............267..........*..............800......285......890...618....... +...*......*...479..........*.*.................648.....+.........71...........*........53..216.686............3/....@..498...............346 +.739......28.....*760....112.725..145......133.../.#71.....737....$..*.....407..228......=........%....................=.............407*... +.....619............................=.........&.............../.....932........&.............................296.........348.+166........... +.......*.............22...-917......................................................-317........................=.........#........=.....111 +.......263...............................$........829......=...418-...........$..............599..304.............@..........849$..442..&... +..792%...........*880....499...........719....415*.......853................928...47...*59...@...*....679.......744......................... +.........590..589..........*...90...................*...............#282.........*..............568..$..........................736..383.321 +.........../.........481....87.*.................831.674....430.............270..393....................%65......................*......*... +....864.........809....*........922.979......................./....461.545.....@.....738........-.....................355.....646........... +.......%......@....#....539.........$.................*122............*....992...898*.....801.497.......#...&...@.................214..862.. +.............393...............................964.953..........503@......*........................764..143.17..385...........525...*....... +.......87.49......444#....................&............................108.....801..............*.....&................866.......$.334...626 +............*120.............736.971....595........715...462...=.....-........=.................754.............@.........*629.............. +.....331............620........*...*.........252.............987.=...846..........163..941.............-219...719...231*..........=......... +....../.........468*..........734...939.........+..220..934......170........745..-.................*....................635......650........ +.550.......&..........................................%.*.....................*.......&.........261.346.237..................#.............. +...*.528..303.546..........357.....878.....571..........755...619...661.....803.358.335....67.......................496...259.........25.... +.938.+....................*...........+.......*649.............*..../.....................*..........................*..........961....*.... +...........975........-....745...........312................134...........253.98...........362.......716.............94.+...620...+.-...62.. +............*.......123..............429....*26.......590............836&.@...........$..............=..................485.........821..... +....555......924...........287........../.............*............................837..........408......................................... +........265............924*.........784......382......354.............255..280.896...............+..........537....936....524...601......... +.............495........................177.....+...........148...628*......*............691.........776=..*......*......*.......&.......... +.....617....*....................832.....#........278*651...*...............767.......16.......66..........857.956.....145..............817. +.......*...560..........906..........635..........................526*.............44*...%....@......@981......................=...624.*.... +....446............-.../................*....................%........798...............796.....................448..........839...*....941. +.................523.......%.990..828-.537......22........771....251...........938..884........782....866..................@........776..... +.808...255..............527..$..............&......709*.............*....=....*........+.......*.........*..........908..659.675............ +..........=.-694.................225........603........523.......617...201.....85..............430.......982...62..=..........*...+...118... +.......=..........672............*.................713...................................120..........&.....................237..733........ +........723....77*.......765........453....296.......*...........................392.554*.......@......770.878.569..../..............757.... +........................./............*.......@.....518.................360*.................272...602........*....456....614/.............. +..............*677............593.460.51..571.................136..758.................422........+......................................... +...........265.......974....../...........=..............142..................82......+.........$......591*...........$......&.............. +....542........44.......*297.......................=.......*.......516.......$.................113.........198.......792..244......391...... +......&...$........737*.............743......741.64.....688........*...........647.986..+588..................................=......&..129. +...........4...........14..............$.397*................896..832...810*.........*.............656...........996..........618.......*... +.......765..................887....8..................226.......%...........149......884.678..........*...358....*................915....956 +........*...................*...../....514.............*................995......429.......@../969...434...*......955....../..210*.......... +.....672....@......@.......322.........*........404#...784.............../..#884....*......................506..........984..........708.... +..........757..712.195...............188.....................895...................326...501.480..&524..........998./..........996.....*.... +..972.160...............857..293..............274....-...-...........807...%............*....................+...*...128..313.........478... +...*..*...804............*...........334..110*.......974.459...........*....801........727.595..778.........699..800.........*.............. +.934..798.%...............37.....191.............*............@.......75...........486......*.................................323........... +................................*......387....477.200......360.......................*....962...47................................179....... +.....................884........985...................................67..145*.......292................191..323...213..............*....... +.....................$....860.............700.147..................$..&.......334........282..267...........*.........-.93..867........525.. +..............492.......&...*....91.......%...*....779.......-691.93.......@.........710.*....*...=...310..........97.....*./..........*.... +..........568*.......708...216..............780......*....................312........*....438.....346....$..$327...=......5........756..855. +.......................................261......807.261.............................42...................................................... \ No newline at end of file diff --git a/inputs/03_demo.txt b/inputs/03_demo.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/inputs/03_demo.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/src/Day03_01.sol b/src/Day03_01.sol new file mode 100644 index 0000000..71750e3 --- /dev/null +++ b/src/Day03_01.sol @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.13; + +import {LibString} from "solady/src/utils/LibString.sol"; +import {LibBytesUint} from "./LibBytesUint.sol"; + +struct Point { + uint256 x; + uint256 y; +} + +struct Rectangle { + Point start; + Point end; + uint256 value; + bool valueSet; +} + +function intersects(Rectangle memory a, Rectangle memory b) pure returns (bool) { + if (a.start.x > b.end.x || b.start.x > a.end.x) { + return false; + } + + if (a.end.y < b.start.y || b.end.y < a.start.y) { + return false; + } + + return true; +} + +contract Day03_01 { + Rectangle[] numberRectangles; + Rectangle[] symbolImpactAreas; + + uint256 lineIndex; + + function parseLine(string calldata line) public virtual { + bytes memory lineBytes = bytes(line); + uint256 numberVectorLength; + uint256 totalNumber; + + for (uint256 i; i < lineBytes.length; i++) { + bool isDigit = LibBytesUint.isDigit(lineBytes[i]); + if (isDigit) { + uint256 digit = uint256(uint8(bytes1(lineBytes[i]))) - 48; + totalNumber = totalNumber * 10 + digit; + numberVectorLength++; + } + + if (totalNumber != 0 && (!isDigit || i == lineBytes.length - 1)) { + numberRectangles.push( + Rectangle({ + start: Point(i - numberVectorLength, lineIndex), + end: Point(i - 1, lineIndex), + value: totalNumber, + valueSet: true + }) + ); + delete numberVectorLength; + delete totalNumber; + } + + if (lineBytes[i] != 0x2e && !isDigit) { + symbolImpactAreas.push( + Rectangle({ + start: Point(i == 0 ? 0 : i - 1, lineIndex == 0 ? 0 : lineIndex - 1), + end: Point(i + 1, lineIndex + 1), + value: 0, + valueSet: false + }) + ); + } + } + + lineIndex++; + } + + function computeResult() public view returns (uint256) { + uint256 result; + + for (uint256 i; i < numberRectangles.length; i++) { + for (uint256 j; j < symbolImpactAreas.length; j++) { + if (intersects(numberRectangles[i], symbolImpactAreas[j])) { + result += numberRectangles[i].value; + break; + } + } + } + + return result; + } +} diff --git a/src/LibBytesUint.sol b/src/LibBytesUint.sol index 505eac8..6f06825 100644 --- a/src/LibBytesUint.sol +++ b/src/LibBytesUint.sol @@ -14,7 +14,7 @@ library LibBytesUint { } function isDigit(bytes1 singleByte) public pure returns (bool) { - for (uint256 i = 48; i < 58; i++) { + for (uint256 i = 48; i < 58; i++) { if (uint256(uint8(singleByte)) == i) { return true; } diff --git a/test/Day03.sol b/test/Day03.sol new file mode 100644 index 0000000..88095d9 --- /dev/null +++ b/test/Day03.sol @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.13; + +import {Test, console2} from "forge-std/Test.sol"; +import {Day03_01} from "../src/Day03_01.sol"; + +contract Day03Test is Test { + Day03_01 public day03_01; + + function setUp() public { + day03_01 = new Day03_01(); + } + + function test_day03_01_demo() public { + while (true) { + string memory line = vm.readLine("./inputs/03_demo.txt"); + if (bytes(line).length == 0) { + break; + } + + day03_01.parseLine(line); + } + + uint256 result = day03_01.computeResult(); + console2.log("Day 3_1 (demo): ", result); + assertEq(result, 4361); + } + + function test_day03_01() public { + while (true) { + string memory line = vm.readLine("./inputs/03.txt"); + if (bytes(line).length == 0) { + break; + } + + day03_01.parseLine(line); + } + + uint256 result = day03_01.computeResult(); + console2.log("Day 3_1: ", result); + assertEq(result, 522726); + } +}