Added day 3 solution.
This commit is contained in:
parent
f2c2c6283a
commit
151dc86534
|
@ -0,0 +1,140 @@
|
||||||
|
48.................501....33.....622..............763.........331.................161.683......................................980..........
|
||||||
|
...491.842.....948*..................338.....*......=...........-...309.......633*....*....................*990...706...452......*..+.......
|
||||||
|
...*...*....................426........*..408.........................*............659...............250.........&.......*.....767...403....
|
||||||
|
363.....961...959#.508*223......843.406..........690................%..479.....................398.../....*............458..................
|
||||||
|
......=.........................................*....@946........767.........907=.................@....158.850..+670..............10*790....
|
||||||
|
.......39.306...679.%113...............665....874....................597...................861.........................840...855............
|
||||||
|
.................*.........864.....154*....#..............1*......4..........341..................731*..........783.....#.......*204........
|
||||||
|
..919...........706.....*......891........840..%473.........379....*149........-..115..519............668..........*423.....................
|
||||||
|
...*.................398.190......$.............................#.......=.%530......$..*..............................................475...
|
||||||
|
.543...@.............................834..729.&.........146......789.189........511...17........455*308..662.............530................
|
||||||
|
.....142....437....138*755..&53.....*..........783........./.......................*......................................./....674..315....
|
||||||
|
..........-./.......................826...%.....................752........414......480.................276.952...................-...*.....
|
||||||
|
.......131.......*654..590.................819..........$..........-.347*..................294.....776...&...*........741.....830....230....
|
||||||
|
..586%.....968...........+..........319..............628......516............933..........&........+..........600.....*........*............
|
||||||
|
............*...................-..*.........................%........................726...876..........772..........99......145......785..
|
||||||
|
...........710...............352...842..941...802.....133............474.&235.................*.............*561..........#...........*.....
|
||||||
|
.......................................=...............*........=...................642.......774......*.........67..@921.374.......165.....
|
||||||
|
..250........................8..........................968...375........224...#.......*............497.833........-........................
|
||||||
|
...$.....................584..*978..107&....471...............................486....408......................................@241.-555.....
|
||||||
|
......662$........%......&.....................*..#186...550........=309............................76.......399............................
|
||||||
|
................281.........808....666.......207.........-...615.................59=....569.....197*...42...%...........572....+231...%.....
|
||||||
|
....758.$777................%.....*...............433................646.834.............@.............*.........543...................769..
|
||||||
|
....*................995...........628...............*..................*..............=..........341...583.112+...*.......74..=341.........
|
||||||
|
.....775.................................744...278.128.......................126...940.839....=.....*............385.499...*............302.
|
||||||
|
..........*177...574.............+.......+......*................561#...590.*......$.........231...586....................212...........#...
|
||||||
|
.......418....................954..433.......484...........526...........*..643......-......................&..778...............701........
|
||||||
|
.414..............493.....566.....*......=..................*....236...748..........543...593...*745.......57.........&.........*...........
|
||||||
|
....*................*.....*....46....659..%.&980..........66....................36...........87...................994.......218...443......
|
||||||
|
226.241.893%..........257..312............69........792............/...........+...................$257..................790........./...184
|
||||||
|
................................................854*.............909.139..*276.680..&.........432............580....248.................*...
|
||||||
|
....136*755...@.950......................173.............95..........+...............2.877.......*668......+..+.........918*.......62....571
|
||||||
|
............333.*....812...$.428.................*496..................903@...258&.............-........650.................281......+......
|
||||||
|
......500.......6....*...719.+......285.......955.....712*12..561.497..............667...&.843..777.................427................@....
|
||||||
|
.....&............506..................=..359................#....................*.....24.../.........544.436..........492=........272.....
|
||||||
|
.........377..........=............41-......+...........229........761...540....391..............*565.....*......449..........665.......#...
|
||||||
|
.887....*.....906..141.....357...$.................%......*.................#.............-..874......811...9.....*....$113......*.967...628
|
||||||
|
......406............../.......637.551....223....162.......251...........................702.*......%......*....&.905.........747...........
|
||||||
|
....*...........321.....801............=.....-.#...............390........845..12............399...126....283.566.....124*102......&........
|
||||||
|
..447..+....935...-..............41.68.87.92...105....178=....*..........*.......*..92..............................................21......
|
||||||
|
......505...................................*................614....948..237..512......108.58..162...454@...........395.......5.............
|
||||||
|
...........454..10.../............101........885.......................*..................*...*....................*........................
|
||||||
|
..........%....*....534......114.*................../.95......427.210..674.......371.903......81....................922.....231...346.......
|
||||||
|
...525........887...........#........162....211..166..@......*.....*.......281......*....................835.635............*.....*.....828.
|
||||||
|
.....*..............571...............+........-.............933.629./25..*...........977........*............*...........178.....749..*....
|
||||||
|
..993.........-................516/................394...56................477....545.*.......251.858......499....435.........&.........170.
|
||||||
|
.......967..73.....&.....35.........656....842.....*......*........................*...749............%..........#...........622............
|
||||||
|
......+...........366.86..*...........#.....=.....162...332.............-..........859............102..488.........................717......
|
||||||
|
347.........32........#....393.................*.................252...262....951.......823...................784...358.603.......*.....515.
|
||||||
|
......917..*.....$..........................642.394..195...*..................*....$......*.448.519................*......*....302..870.*...
|
||||||
|
...-..*...674.....780.547....287....................*....504...................803.688.831....*..*...........617......./..476........*...9..
|
||||||
|
.286..738................#.......-511..11.....581..268.....................439.............523..459...............69..373............887....
|
||||||
|
......................*...................555*.........45....638...964.......-...$..........................................88*.............
|
||||||
|
.........802...+805.77.648.....66.676........................&..../....26......708...+.-981...239%........953..................672..........
|
||||||
|
......./.....#.............11.+......*....=...........756.............*.....-......298..................=...*..751.......433.........727....
|
||||||
|
....553.......659....907...@......@..530.218.............*......259..821...716.980..........89..851...793.614.......943....*.....961*.......
|
||||||
|
........*339........$..........373...............................*...............$.....667..$......*..........149.....+..613................
|
||||||
|
..987..................*...................670...105.....347....383.605..................@....447...124..........*737...........562..15*445.
|
||||||
|
........-.....595*7..215.839.781.............*..*...........$.............810....*............*...........350....................*..........
|
||||||
|
.........929.............*......*...362@...890...975....#..........2....../...252.576.......78...........+........685.240......695..........
|
||||||
|
.................475......598.152........................841.&880..#......................&....693....=.............=.....28........&384....
|
||||||
|
............472..*.......................$..320.412*...................304...=.-.........395......=.113....601..............................
|
||||||
|
..324.738.........968...................755..*......101.......476..........90..342........................*.....638..182/...........704..139
|
||||||
|
.........*..610*......601...................356.../.......633*....971..........................806.....#...240...*...................../....
|
||||||
|
........683.....326.....*..@........749.........390.........................163@....800..483..*........792.....94...........................
|
||||||
|
...600................957..741...../....376............817..899.......194........50.....@....96.368..................@.......+600....-......
|
||||||
|
..........338%..........................*...............#....#.........*....304..+....*...........*...................236............284....
|
||||||
|
..423........................&763.............947.................371.187..*.......356.441......201.......................285...............
|
||||||
|
............75................................*....................*......696........................#...............................670....
|
||||||
|
...*..........*.........426.......635..-....449.758*.......314.......*..............................246.........*................58..*......
|
||||||
|
574.322........937.......*........#...116...........40.......*.@752..400........180....@......918.........195.49......983....559*.....82....
|
||||||
|
...........*.........527..842.............................817...................../.270...445..*............*...............................
|
||||||
|
........622.235..869*..........922*...............125*610..............*459...............&....455...........21.......964...................
|
||||||
|
...................................465..%542.........................47......272............+..........775.............*.....328.....@......
|
||||||
|
.................732*........51.&..........................853.222.........................672....148....+.........572..499.+....*....12....
|
||||||
|
....125..............581....*...909.787.......720*263......*....$..881%....623*891..515..........=..................*.........726.78........
|
||||||
|
.....%..........715......935.........*....650............874.......................*.........127....................923.....................
|
||||||
|
................$.............330*..444....$....653............%....345..915.....960.395...........823.706..734.341...............295...342.
|
||||||
|
.............................................99..-...........465...&......*.............*416......*..........@....*...@......630.....*......
|
||||||
|
.....@.........356.......................399.+......+.....................349..................542..............853.375.......=.......132...
|
||||||
|
....77...=719.@.......*...../662...........*...233...781..............................&687................%.................................
|
||||||
|
.....................791..................933.*..........931.....884*...........-...................../...112..%601..........857............
|
||||||
|
583.....*292...................................438...................424......603.416.........484...104.....................*.......706.....
|
||||||
|
.............................452.......549.............24...997....................*............*.........767...379..........192............
|
||||||
|
........413.................&.....#......*....*.......*.........523.........443.412.....546......859..138*........&..66.576.................
|
||||||
|
..................../...........348....816.106.391......................447.........../...*..895.......................*................595.
|
||||||
|
...........#......771.675..................................673............*........176...856..=...627*308..696...............658............
|
||||||
|
....489..51...174......%..676......$..........*........332.*.............373.350................-..........*.................*.....*........
|
||||||
|
181.*........*...............*822.136..888.642.202.......*..65....958........*........358........510.....*..504..747...993....339..764......
|
||||||
|
...........540........................................308...........@.895..942.506......*....791.......166......#...../..../...........668..
|
||||||
|
...................452............-....631..............................*........&.....253..../............897......*....602................
|
||||||
|
..748....249.24............21...847...&.....194...@...............@...865................................=....*.....550.....................
|
||||||
|
....*.......*.....658...................573*.......679.....760/..431.................481..........907....130.912...............240...998....
|
||||||
|
.605...............*.......25.......#..................................726..646....#..........607..*................698..........*...+......
|
||||||
|
...........301&.572........#.....101...................148........55...........*.122........./........953..........-.....602.321.929.....998
|
||||||
|
511.870...............@....................808.........*........*............257.......206............@........685..........*...............
|
||||||
|
.............903...398.....588......114.......*...990...677.....182.10$...........%.........%.................%........................2*...
|
||||||
|
................*...........*...490...........987....=......803.........98......485.493.868..945.805......-........89............=944....296
|
||||||
|
......640.......669.........630..*.......................-....&..582.......341.........*.........*.........939......*.......................
|
||||||
|
.........*34....................557.....130....265.....21........*...625..$...............*796..690................997...489....293.........
|
||||||
|
....917.......916............@.................*.................948..&...........148...67..................833........./..........-...943..
|
||||||
|
....$........*......849...4$..300..217........58..........................643.170...........................................................
|
||||||
|
.........511..853......*..............*...........355.............35-.....-....@..636.....=...595..131...496..........................=687..
|
||||||
|
............%........&..476......452...435.....%...*.....700..........391........*.....418.........*.........39*.&..........................
|
||||||
|
.16...=.............500.....................426...469.....*.............&.....#..522.............939..............395.660...................
|
||||||
|
...*.696.....*869........995.....413...................864.................991.......598......42.........................@..806..386........
|
||||||
|
337.......983.....610........104*.......100*......................-............$79....@...461*....*685...270................*...*........240
|
||||||
|
......................207...........311.....741..................337.........*.................381.......*........979.....712.374.879.......
|
||||||
|
.......@.....554.........*.....485.....................853.............*..276.230...........7...........740...755*..................-.......
|
||||||
|
....927.../.....#...#...46........*466...........=.................=.531...........593......*..%....................961.....@...............
|
||||||
|
..........65......294..........................30..339..856.....966...........904..........791..841..........438.......#...747..............
|
||||||
|
..419...................614................972.......*...............131.......=.......................716..........21......................
|
||||||
|
.....=.79..............................&....*......76.....243..............115.......757...364..870....&...890......+...840..........55.....
|
||||||
|
....................383....*48...571...124...749............*................-..&.....*......*...*........@.....................201./.......
|
||||||
|
......752.....470.......+..........*........................964.......452........776...413...9..791...257.......687.....84......*...........
|
||||||
|
.........*502.*......873..850..573..729....@.....197....................*...798.........................@......*........&.....65...200..357.
|
||||||
|
...............307.............*..........597.....*...473.....=.......332....*..311..861*399...................472..................*.../...
|
||||||
|
.820.....................65....754.............713......*.....739...........622...............768/..................................967.....
|
||||||
|
....*.......180..........*...*.........218...............260......................519...933..........271.....36....132.....147..............
|
||||||
|
.935..4......@..254...815..58.689.......*..........................159..............$...*..........*....&...$.......*......../.414*164......
|
||||||
|
.......-.388.......%....................284...........833...770...*...........705.....697.......466.239.......332....489....................
|
||||||
|
....#.....*...........@.......288............972.........-.*......723.....*....*................................*............=..........592.
|
||||||
|
...535..........*.267..193.......*..367.....*..............132............509..776.790..@....466..........715.758...*115.....608............
|
||||||
|
.............979..*..........8.353.....*...329...@544.913..........................*.....759.*......704..%............................712...
|
||||||
|
...................52..170...*......459................/...#.......161........813.510.........207......@..........581..................*....
|
||||||
|
............#...........*..978...........581.9*815..........450...............*.......................................604.......308...725...
|
||||||
|
............16.........269............../.............195........217*216......890......................534.....84......-......-.............
|
||||||
|
....339...........................459.......644..........*.........................636.....113...%154...*....................127.....#348...
|
||||||
|
349...*.............402...735......@................182.121...134........%255.276...%...&.../............460....#......79...................
|
||||||
|
....503....22.............#.....$............38.....+...........*....-11.......*......326...../....*853.........216......*.../180.....16....
|
||||||
|
..........*......92.............456..619......*..-...........=...103..........190..........353..982......25*........465.209.................
|
||||||
|
...485.350..........949...994.......*....379..3.22........972.........713.734.........262...........$.......846.......*.............333-....
|
||||||
|
....................*..../....*121.729..+............325.............*.......*..........*........374..............+..769.....811............
|
||||||
|
......-..............788...488....................................851....679......*......487.....................368.........*........*.....
|
||||||
|
....612..528...255.................364.....................130.................317..251...............................665.206..972.957.184..
|
||||||
|
..........*..../.......337..475*......&..391...347...795....*.........................*......722..666...............@.......................
|
||||||
|
........823.$.....+.../.........716......*........*....*.....247......329...........697......*.....*....%.....168....624.........592........
|
||||||
|
...98*.......916..915......245............277..&..353...719..........$.....................846.601.37...47......................*......519..
|
||||||
|
......91..............720..*........$985......976......................834...........461.........*...........................266....#...*...
|
||||||
|
68....................*....45..............&...........79*888.250*461.*.......%................574..........3*....408..380........383.192...
|
||||||
|
...........836......383...........557.....672..........................764.....944............................827...........................
|
|
@ -0,0 +1,10 @@
|
||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
|
@ -0,0 +1,85 @@
|
||||||
|
import std.stdio;
|
||||||
|
import std.file;
|
||||||
|
import std.string;
|
||||||
|
import std.algorithm;
|
||||||
|
import std.array;
|
||||||
|
import std.ascii : isDigit;
|
||||||
|
import std.conv;
|
||||||
|
|
||||||
|
struct Point {const int x, y;}
|
||||||
|
|
||||||
|
char getChar(ref char[][] array, int x, int y) {
|
||||||
|
if (y >= 0 && y < array.length && x >= 0 && x < array[y].length) return array[y][x];
|
||||||
|
return '.';
|
||||||
|
}
|
||||||
|
|
||||||
|
void part1() {
|
||||||
|
char[][] schematic = File("input.txt").byLineCopy()
|
||||||
|
.map!(line => cast(char[])line)
|
||||||
|
.array();
|
||||||
|
|
||||||
|
bool hasAdjacentSymbol(int x, int y, int length) {
|
||||||
|
for (int i = y-1; i < y+2; i++) {
|
||||||
|
for (int j = x-1; j < x+length+1; j++) {
|
||||||
|
const char c = getChar(schematic, j, i);
|
||||||
|
if (c != '.' && !isDigit(c)) return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (int y = 0; y < schematic.length; y++) {
|
||||||
|
for (int x = 0; x < schematic[y].length; x++) {
|
||||||
|
if (isDigit(schematic[y][x])) {
|
||||||
|
int length = 1;
|
||||||
|
while (x + length < schematic[y].length && isDigit(schematic[y][x+length])) length++;
|
||||||
|
if (hasAdjacentSymbol(x, y, length)) {
|
||||||
|
int n = schematic[y][x..x+length].to!int;
|
||||||
|
sum += n;
|
||||||
|
}
|
||||||
|
x += length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeln(sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
void part2() {
|
||||||
|
char[][] schematic = File("input.txt").byLineCopy()
|
||||||
|
.map!(line => cast(char[])line)
|
||||||
|
.array();
|
||||||
|
|
||||||
|
Point[] findGears(int x, int y, int length) {
|
||||||
|
Point[] gears;
|
||||||
|
for (int i = y-1; i < y+2; i++) {
|
||||||
|
for (int j = x-1; j < x+length+1; j++) {
|
||||||
|
if (getChar(schematic, j, i) == '*') gears ~= Point(j, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gears;
|
||||||
|
}
|
||||||
|
|
||||||
|
int[][Point] gearNumbers;
|
||||||
|
for (int y = 0; y < schematic.length; y++) {
|
||||||
|
for (int x = 0; x < schematic[y].length; x++) {
|
||||||
|
if (isDigit(schematic[y][x])) {
|
||||||
|
int length = 1;
|
||||||
|
while (x + length < schematic[y].length && isDigit(schematic[y][x+length])) length++;
|
||||||
|
int n = schematic[y][x..x+length].to!int;
|
||||||
|
Point[] gears = findGears(x, y, length);
|
||||||
|
foreach (Point gear; gears) {
|
||||||
|
if (gear !in gearNumbers) gearNumbers[gear] = [];
|
||||||
|
gearNumbers[gear] ~= n;
|
||||||
|
}
|
||||||
|
x += length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gearNumbers.values.map!(a => a.length == 2 ? a[0] * a[1] : 0).sum.writeln();
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
part1();
|
||||||
|
part2();
|
||||||
|
}
|
Loading…
Reference in New Issue