From a14b747f4c96120f0bb0ad63e60d1d68fd19771f Mon Sep 17 00:00:00 2001 From: Jacob Lorenzen Date: Sun, 3 Dec 2023 11:46:06 +0100 Subject: [PATCH] chore: solution for day03a --- data/day03.txt | 140 ++++++++++++++++++++++++++++++++++++++++ data/day03a.txt | 10 +++ lib/day03.rb | 92 ++++++++++++++++++++++++++ test/unit/day03_test.rb | 18 ++++++ 4 files changed, 260 insertions(+) create mode 100644 data/day03.txt create mode 100644 data/day03a.txt create mode 100644 lib/day03.rb create mode 100644 test/unit/day03_test.rb diff --git a/data/day03.txt b/data/day03.txt new file mode 100644 index 0000000..2f47e68 --- /dev/null +++ b/data/day03.txt @@ -0,0 +1,140 @@ +..............423....688..934............970................................95.728..........896...113..................153..972............. +...122..................*.....*..........................919..509*..........&...@.........../...........................+.......*........... +....+..........259....698..373.992.52.674.........................781...22........130.584.....-...%399.......777.................266........ +......148..+....*........................*.....357.123.......................812.........*756.143...........*............................... +..691*.....700..708................-...357........*........$177......%..244.............................762.453....477-.707..-168..359*..... +.......................394.443....456......750..................71.160.....*..183.........835..74.........*.............../............129.. +....578$....................%........................362..1......*.......661....*...........=.....730......744..294..........247............ +..............415...306*....../.452.................+.....*......375.............76....186*....../...............*..........@.......-..441.. +.....*152......#........75.757.......204%..................605.............................77.......%405.......957..324...........404....... +..545.............../...........................................*....618......%...599...............................&....43.............=... +..............902...145............170..............568..741.285.592..........54./.......%.......923......522+..........*..../...128....602. +.......697.16*................322...........$............*............+339..........633.56............$..............792..751............... +.......*.........................*..752..826.......*...104..861......................................144.....883...............+..778.892... +......968..690.......#.....769#.718...*.........326..........*..............493..123..........362...........*.........757....570..@....*.... +..681.................242............806................509...307..../.....*......./.350..942...*..614@...511........+.................151.. +........*....../..256.......................235.....923....*..........28..264........*......&..316..............945....522........*823...... +.....961.136.567....................944.......*..........500..-.......................504................765$..........=.......563.......... +..................837..823......480.....627....984.#.........136....969..........-289......$757............................107........593... +.74.924....../.............+110....*...............511................+....564......................389%.....989*305....@...*.........*..... +.......+......110..735..........647....658.509*378..........-999............*....#225....937.............147.........778...868.....871..611. +.........$...........*.....3............*...........................608..842..............*.......589.......*....842...................*.... +......$.963.....634.....................569....&..........177................*..........774.........*..132.153..*.............=.......13.... +...723.........=.......@..324......455#.......417....849$.*.........*372..747.87............./358...35..*.......974..453...811...990........ +.....................174...%............949...............182....684.............414...................201.......................*...../.... +........@.......................62.....*.....-.@669.....................487......*................943.........740*...10.......298....384.... +....446.711.........20.674...........343..806.....................&.574.*........965..............*......776...........*.867................ +......*............&.......705.804.............................374....+.307............&.......683...830...*....625..530....*837...272*..... +.......791....................*.......&.................108...........................533..............*...966.*.......................515.. +............412...../................764....................658.+292.635.674..337.................98..814......263.......720*............... +..............*...469..162.....23......................*64..*...............*.............+.........*.....*552...............990......942... +790.536.......365.......*..587..=..........................313.....512....983....953.......348.....617.982.................................. +........580............227...*....673.808....66......595............./........*............................$..........21..989............... +.....#.....*.781...........688...*..../........=.........865...............316.162.440.............&......482.663....*.............529...... +...357...182.@......359=..........553.......#....33....................323.........*............702...........*....200........&.....*....... +...............733....................957.22..........815.........................57..657@..................792..........%.....678.305...... +..701..246........*238....553..107......*...................301..........649.555..............70....................610.233.&............... +....#...*..&.................$.*....$.......$......438.....*.......943......*.....&675..955*....*..........183...=....%.....595............. +......719.857...640*299............202...125...802..#...786...........*.....................277.424..........*.790./...............984...... +...@......................368.....................*...............323..587.479....553.272.................843.......518.............*....... +...382..........892...46..........359..372+.611..585...............*.......*........*...+........&.............................487...35..... +........&692.......*..*..............@.......-.......*697.......656.....155....34..681............64..641......................*............ +....495..........885..964................993......685................%........*....................../..........543.........863............. +..........*408................&....244.....*..........................893....339........................109.....................777......... +...521.985..................677......*......734........18....925#................352...843*74.......&....*.............260.......*.......... +...$........445.$..................802...................#..........=........533...@.................228..612......724....*................. +.............*..504............=.......*.....177...................502.....+..*.............395......................$.....274.....396...... +......................417....274.....61.504..*........36*..................34..801..........%............224......................*......... +......789.........139......&.....*............802........674....&..332..............675............819.............814............883..+915. +...@.....*.....-..*.....970...279.117...............984@.....878..........231*168.*..................*.........793*......................... +..33..875.....483.433....................*.........................................148.675...581*.....544.........................*537...... +......................884............671..839.....31..........685..@...899....350*...............621........*509............./.913.......... +....947......=742..........*.......................*...............712....*.......722...159............378........438.....300...........381. +...*....................548..-................*..638.....................827...........*......714/...../...867....$..................%.-.... +998.....966.693..............694...........781...........764......584...............233....58......-......*............67=........161....... +...........*.....................509..-851.........&.846..#........../...199.................*.443..658..976.130..489..................594.. +243....413.........499-...384.......*............352.+.............................752....298..*...............$..$...............577...*... +......*......................*......378.335.................*.........+...813.120.*....................654..........$........325...*...984.. +...847..496....151*...........741.......*............624.116....767.988..&.....*...87............807...*.........127....997+......621....... +........*..........227....345......*........@.....................*............699....566..604...*...760....290............................. +......526..................*..500..799...761...413*......277....101........$..........*.........342..........*.......589..997......=........ +.............+....379....549............................*............*97.647.779....$.655..975......216...............*.......51..307./496.. +...........485.......&...............................55...........509............825......+.....43.....*...554......36...678+.$............. +..........................73.....419.....314../...........311...........$....946.....272............351...*........................968../... +.....786.................*........-..824.$...478......908....*.....455.254.....*.........615....760.....60...$......&.....*562.........102.. +.......*......43.....&...808........*.............267.*...985.....*..........616.........../......*...........189....977........616......... +......45.932...*.....502..........805...943.840%...*..270......764......680......................750..415*184......................*606..... +..768....*.....941............%.......8.&..................*................248&..105..745...633...........................584..........692. +.......684............115....88...396*..................494.354...88...58.....................*.........956......809...53.......615......... +................*391.............................................$....%.....&64.............488...........$.........*.....*.......*......... +......440.459...........637...*....866...412.519.183..../.......................*82..963.........*307..=......715.408..421.292....626..*.... +.........*......$..........#...769............./..*....470.939.+......*.....257.....*.........257.....109.578..*.......................290.. +......+.......-..897...............................629.....-...834.254.798..*.......411....................*....411.......309....218........ +.......960..744..............384............................................433................$301.....848.................*.......#..144.. +..746............$....../...........38$...536.....-....@.............*..................-............................320...471..........*... +....*.............720...59......................260..852...........633.........714...510......256.173.23...850........*........#.....691.... +..294..434...264......................422../639..........378.493...........388*................-....*...*..../.141..............539......... +........../.&........790..........@....*....................*..........&............$.............16....633.....=.....................458... +..678...............&.......184....186.934..652...741%.........=887...980.425../....485.732...502...........%........745..............*..... +...*..........98.......@......*.................+...........................*...989.....$........*......838.9..........-...........464...... +.975..768....*.....*..702.....979....511$....161....645..............44.....18............721.695...348.........400......./.543*............ +........*.769....320......724......................&.....330&........*...................*...........$...188.....*.....977......618...%..... +......879.....79............*....658..............................601................701..490....#......*........478.......166.........892.. +..844.............706....472....*...........113.........-765..217........267....../.*..........648....973....285............................ +.....*.482..........*...........853....408.....*................&........*.......59.265.........................*.......364....45.......@... +..164..*...........309....472............#..478...40.......356........328....706..............$...............149.526.....%.....*.....27.... +...........................*.......&..................612...*..58.64%........*...766.......953......488............=.../.......152.......... +...*73....872.............347....27...22......552@.........559.*............435.....*679.............*...13.774......177.#250.......8*...... +977......#.........=....................=.......................56...508......................139.747.....*..*................861.....666... +..............703...102....../........&....637..448......%..951........*...175.552*235...@.....*.......486..693............................. +........944..*....@...........590..42.738...*..........861.....&....919.....*............108.837................#....963.530...831..&...251. +........#.....93.103....708................54.342....+......................496.......................&.........278.....*.....*....162...... +..........860............@.......$167..............431.683......=496....858.........422.............662.154.405..............635.%.......... +.....882/.*.............................%......611..........766.......-...*.....755..@.....................*..........608.........951....... +..........513......................274...226......*........@.......&..556.151....*.............229...681..............*.....911............. +..............445....375.....774....*...........348..............643.............302...........*........*.......-...88......*........975.... +....*664.........*...&........@.........................893..........523....13...............825.........558...733.....769...991.....*...... +.328......821....935...................&.......@...457.....*.....+..*.........=..................341..................#............384...... +.........*...........164..........251..756....23...........912.532..879..................459........*...=................................... +219.....71..821..............654.....................684........................@2....................931...........910..................... +............&...425+..-.............207........*.....%...360...........190..........69.....................43..........$.................... +.....................116....................127.214.........*..181........@..%...49*....444....57.........*.....................715......... +............$....*.............-..859.................=..248......*..........82........*.......@.......246..376.........#........*.......... +...726...898...-..772...223.712....%........%464.....342.........442..447.........461..102.....................*355...537....313..596...584. +....*.........189........@...............................................*187........*...............*....807.............+.....*........... +.....181.+..........197......530.653+.383...293.........461%.22*..............394....769..104...$.818.36.$....=.......&....860...333........ +..........827..........*................-....*..................110.883..................%.....56............111......872................... +........................8.321....-............209.....578...583.....$.....948..548#..................868*678......871....................... +......658...264...........&....105......132............./...................*..................640..................*...........714......... +.........+.*.....................................................................608..............*....963*565.....472.......93...*......... +...*717....14.......................702............604.608......................*.....12....689..599.............................423........ +999.........................764......*.......$780.....*.......+.......%........406....*...............278*........600*23.....407.....964.... +...........%....308................183................../......758..&..360..........127.#696...50........................839*.....&.....$... +......277..449....*............881.......822.....905...205.........345.......572@................../733....=..61...................796...... +.......#..........659...*.....=..........................................................................665...................292.......... +......................887.711.....560..........................11...213*434...................949....761......*856...233*996......%.965..... +.....699..../.............&....%......103...518...............................673......=.........*....@....149.............................. +............894......=......292..425.....#........@259....787..995..................927..813..326..............439.....379.547..292......... +....901............678.217......*..............................*.......289.346..875.........*.........653+......*......*....=.....$......... +......#........................56..887....&540..200...........824............&.....*.414=....629..............893..34.334................731 +...............476.......338........@..#............592..573*.....759...........598........$.........692..474.......*................304.... +..........................*..263*93...541....138......*..........*.....................260.187.......%...*.........436.................*.... +.....#..895............744.....................%.473...92........23..................$..#........853.....443......................12..339... +...284...*...59*...131...............................................=....354.792...679.....649....*.......................994...%.......... +.......267............*...............176..............=.............743.........*......631......856................166.......*............. +.....................684.................@...........773....108...........617...965....*........................958............777.....767.. +..........572................#....&.............................51........@..........190..............694..882.#......511..788.............. +...96........%.....119..4.954....81..836....@.....671....161.................*.........................#..*.......730.%...*................. +....................@.................*...68..924...........*...644.......654.842..473.....449.............936.....*......305............980 +..........188....7..........672......596.........@........473......*..................*418....&.......302.........7...621.....476...938.*... +.......@...*...........#824..*...303...........................%....486..........290..................*.....-907.....*.......*......*...686. +....869....976...478*.......56..*...........*..................415......97.@605.....................129............701....626....157........ +.....................455.......306.876......829............430..................956.15@.......302..........938.............................. +..........................43.......*..............$.......-.....998...106.......=.......486*....&.......+......................982.......... +.554......................*.......50.231.........409..............+...$.....*........72.....716.........51..445.....*284........*........350 +.........425............*.94..836......&..............#516..............775.701.....*............................744..........541......-.... +....*810..............34....................&....297..........#........*....................#.............899=..........972..........27..... +960........762.....................*........394........&355.401........347.....+853.....&...967............................*86..663......... +............*..........991......686.25.286.......&...................................653...........914........$.....580...........*......... +...#.....784......-.......*............*......498......*..........*..316........................&.&..........691.2.....*........91.......... +..791..............462....193..........8.............57.685.......90..........201............371.....242............996................579.. diff --git a/data/day03a.txt b/data/day03a.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/data/day03a.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/lib/day03.rb b/lib/day03.rb new file mode 100644 index 0000000..90fcd79 --- /dev/null +++ b/lib/day03.rb @@ -0,0 +1,92 @@ +class Day03 + def load(file) + @lines = File.readlines(file) + end + + def parse(line) + map = {} + for y in 0..@lines.length-1 + for x in 0..@lines[y].length-2 + map[[x,y]] = @lines[y][x] + end + end + map + end + + def parse_number(map, from, to, y) + num = "" + for x in from..to + num += map[[x,y]] + end + num.to_i + end + + def valid_char?(candidate) + if candidate != "." and !candidate.match?(/\d/) + return true + end + false + end + + def is_valid?(map, from, to, y) + neighbors = [[1,0], [-1,0], [0,1], [0,-1], [1,1], [-1,-1], [1,-1], [-1,1]] + + # check if next to the number + if map.key?([from-1,y]) + if valid_char?(map[[from-1,y]]) + return true + end + end + + if map.key?([to+1,y]) + if valid_char?(map[[to+1,y]]) + return true + end + end + + for x in from..to + for n in neighbors + if map.key?([x+n[0],y+n[1]]) + if y+n[1] != y + if valid_char?(map[[x+n[0],y+n[1]]]) + return true + end + end + end + end + end + false + end + + def part1 + map = parse(@lines) + nums = [] + for y in 0..@lines.length-1 + from = nil + for x in 0..@lines[y].length-2 + if map[[x,y]].match?(/\d/) + # set start of number + if from.nil? + from = x + end + # check if end of line and parse number + if x == @lines[y].length-2 + if is_valid?(map, from, x - 1, y) + nums << parse_number(map, from, x, y) + from = nil + end + end + else + # parse number + if not from.nil? + if is_valid?(map, from, x - 1, y) + nums << parse_number(map, from, x, y) + end + from = nil + end + end + end + end + nums.sum + end +end diff --git a/test/unit/day03_test.rb b/test/unit/day03_test.rb new file mode 100644 index 0000000..6079f50 --- /dev/null +++ b/test/unit/day03_test.rb @@ -0,0 +1,18 @@ +require "test_helper" +require "day03" + +class Day01Test < Minitest::Test + def test_day03a + sut = Day03.new + sut.load("data/day03a.txt") + assert_equal(4361, sut.part1) + end + + def test_day03_part1 + sut = Day03.new + sut.load("data/day03.txt") + assert_equal(538046, sut.part1) + end + +end +