Compare commits
5 Commits
b7ce003ea0
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
bf600501fa | ||
|
d5574c1167 | ||
|
b69c454c4c | ||
|
7268243458 | ||
|
e95bee2b13 |
60
07/code.js
Normal file
60
07/code.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const fs = require('fs');
|
||||
|
||||
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
const tree = { };
|
||||
let path = [];
|
||||
|
||||
for (i in inputArray) {
|
||||
const command =inputArray[i].split(' ');
|
||||
if (command[0] === '$') {
|
||||
if (command[1] === 'cd') {
|
||||
if (command[2] === '..') {
|
||||
path.pop();
|
||||
} else {
|
||||
path.push(command[2]);
|
||||
tree[path.join('.')] = 0;
|
||||
}
|
||||
}
|
||||
} else if (command[0] !== 'dir') {
|
||||
const size = parseInt(command[0]);
|
||||
const dirs = [];
|
||||
for (const dir of path) {
|
||||
dirs.push(dir);
|
||||
tree[dirs.join('.')] += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let summedSizes = 0;
|
||||
for (const size of Object.values(tree)) {
|
||||
if (size <= 100000) {
|
||||
summedSizes += size;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(summedSizes);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
const freeSpace = 70000000 - tree['/'];
|
||||
let candidates = [];
|
||||
|
||||
for (const size of Object.values(tree)) {
|
||||
if (size >= (30000000 - freeSpace)) {
|
||||
candidates.push(size);
|
||||
}
|
||||
}
|
||||
|
||||
candidates.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
console.log(candidates[0]);
|
||||
|
||||
// functions
|
||||
|
1082
07/input.txt
Normal file
1082
07/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
23
07/sample.txt
Normal file
23
07/sample.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
155
08/code.js
Normal file
155
08/code.js
Normal file
@@ -0,0 +1,155 @@
|
||||
const fs = require('fs');
|
||||
|
||||
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
const treeMap = [];
|
||||
|
||||
for (i in inputArray) {
|
||||
const trees = inputArray[i].split("");
|
||||
treeMap.push(trees);
|
||||
}
|
||||
|
||||
const width = treeMap[0].length;
|
||||
const height = treeMap.length;
|
||||
|
||||
// Part One
|
||||
|
||||
|
||||
let talls = width * height;
|
||||
|
||||
for (h = 1; h < height - 1; h++) {
|
||||
for (w = 1; w < width - 1; w++) {
|
||||
let short = true;
|
||||
const myHeight = treeMap[h][w];
|
||||
|
||||
// look up
|
||||
let heights = [];
|
||||
for (y = h - 1; y >= 0; y--) {
|
||||
heights.push(treeMap[y][w]);
|
||||
}
|
||||
let shorties = heights.filter((t) => t < myHeight);
|
||||
if (heights.length === shorties.length) {
|
||||
short = false;
|
||||
}
|
||||
// look down
|
||||
heights = [];
|
||||
for (y = h + 1; y < height; y++) {
|
||||
heights.push(treeMap[y][w]);
|
||||
}
|
||||
shorties = heights.filter((t) => t < myHeight);
|
||||
if (heights.length === shorties.length) {
|
||||
short = false;
|
||||
}
|
||||
// look left
|
||||
heights = [];
|
||||
for (x = w - 1; x >= 0; x--) {
|
||||
heights.push(treeMap[h][x]);
|
||||
}
|
||||
shorties = heights.filter((t) => t < myHeight);
|
||||
if (heights.length === shorties.length) {
|
||||
short = false;
|
||||
}
|
||||
// look right
|
||||
heights = [];
|
||||
for (x = w + 1; x < width; x++) {
|
||||
heights.push(treeMap[h][x]);
|
||||
}
|
||||
shorties = heights.filter((t) => t < myHeight);
|
||||
if (heights.length === shorties.length) {
|
||||
short = false;
|
||||
}
|
||||
|
||||
if (short) {
|
||||
talls--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(talls);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
const scores = [];
|
||||
|
||||
for (h = 1; h < height - 1; h++) {
|
||||
for (w = 1; w < width - 1; w++) {
|
||||
const myHeight = treeMap[h][w];
|
||||
let myScore = 1;
|
||||
|
||||
// look up
|
||||
let heights = [];
|
||||
for (y = h - 1; y >= 0; y--) {
|
||||
heights.push(treeMap[y][w]);
|
||||
}
|
||||
let visible = 0;
|
||||
for (i in heights) {
|
||||
if (heights[i] < myHeight) {
|
||||
visible++
|
||||
} else {
|
||||
visible++
|
||||
break;
|
||||
}
|
||||
}
|
||||
myScore = myScore * visible;
|
||||
|
||||
// look down
|
||||
heights = [];
|
||||
for (y = h + 1; y < height; y++) {
|
||||
heights.push(treeMap[y][w]);
|
||||
}
|
||||
visible = 0;
|
||||
for (i in heights) {
|
||||
if (heights[i] < myHeight) {
|
||||
visible++
|
||||
} else {
|
||||
visible++
|
||||
break;
|
||||
}
|
||||
}
|
||||
myScore = myScore * visible;
|
||||
|
||||
// look left
|
||||
heights = [];
|
||||
for (x = w - 1; x >= 0; x--) {
|
||||
heights.push(treeMap[h][x]);
|
||||
}
|
||||
visible = 0;
|
||||
for (i in heights) {
|
||||
if (heights[i] < myHeight) {
|
||||
visible++
|
||||
} else {
|
||||
visible++
|
||||
break;
|
||||
}
|
||||
}
|
||||
myScore = myScore * visible;
|
||||
|
||||
// look right
|
||||
heights = [];
|
||||
for (x = w + 1; x < width; x++) {
|
||||
heights.push(treeMap[h][x]);
|
||||
}
|
||||
visible = 0;
|
||||
for (i in heights) {
|
||||
if (heights[i] < myHeight) {
|
||||
visible++
|
||||
} else {
|
||||
visible++
|
||||
break;
|
||||
}
|
||||
}
|
||||
myScore = myScore * visible;
|
||||
|
||||
scores.push(myScore);
|
||||
}
|
||||
}
|
||||
|
||||
scores.sort(function(a, b) {
|
||||
return b - a;
|
||||
});
|
||||
|
||||
console.log(scores[0]);
|
||||
|
||||
// functions
|
99
08/input.txt
Normal file
99
08/input.txt
Normal file
@@ -0,0 +1,99 @@
|
||||
122202212311200210333041243104410211022242435153422552354101423030331120103122123201123013212020121
|
||||
212122122000221311323440442444413314215241554351515445144551151103011402331412314230102131310221002
|
||||
200010233110132032201044033410403402323542453441213225433145441453242244421030303220011011132300002
|
||||
120101110203333110240120011412225434434121313542334144355225111454523132420142243111113223031311111
|
||||
120110223201233140434331404320315125532452213233242311511524412523343520430421042000030001030200222
|
||||
000102322010022341104032321113312242521134214543411111515333425154553221421211441143024103333323012
|
||||
012132022020312444102430014331221411222235334515444152433144133435532224431441304412411212000133031
|
||||
120233223310314441323131225313444245412451315432455236445534542223421552522450312102202033233322003
|
||||
110303300214411413041135513123311333342254632434525246464244653512154122151153240314331404133102032
|
||||
320333021041401210342112413252521354154654526224624264656643323243514213144525443140321100432322102
|
||||
213333013400314344445512541224344542425335542246245535342346525455632342143432241440034221113121103
|
||||
103101323212441144514541213255356562253454324423562254643435566355652443131311521310340011410023010
|
||||
232231310023333125425145335413643646335436344645256622626244424566265461143132555442033102212102133
|
||||
230012330403333244113122423525434543453346345442546444536356456656653336522243141515320031310242132
|
||||
001331331400242344452535542244353454256436245444356344633623323223624634633531345443212332124100001
|
||||
320213230430015352541121166364225342324465546743674376745764665342525653462642431254322120131320301
|
||||
030030441321345531435352552565634242236456747547675667333646754662532244322223425143335202013410331
|
||||
130323430403445155252353542665556632743644346337573354575553473564426335656353332452444512100342303
|
||||
000124441244513213434222225365536245467565344634335675756354647753423526454264443544455252333100012
|
||||
303404133431225152343544434244326457656763736537576455577647443546747235534536353521453342020431303
|
||||
311104212215444221552335243356444654364337535373347655455775663756457363653523325323222244130341434
|
||||
033113243413224543336252525236557657445633537454545454747556455455777455542442433541422521522303132
|
||||
032301305335212345536223463445733334466466456857685556646643443634434366755622423221321152153211112
|
||||
432413234435244244452452522766657337677487457667574884664485837634653735355652265344225135521142200
|
||||
214204451352332225552233236447335656736454774865747468575666547736456546737626423225514542353132003
|
||||
033032334245244345353263544373576764747865456884456784664774575446775657546624634322432214245131331
|
||||
121012311135135242665226463546373455674886866675578444744865445486444436446765632254623534551121111
|
||||
341323122332344433445227455474347888748648456446654846846776658764776443766657642346253535143552144
|
||||
324244533333334656636247465677737577755486464656557545574668654666685345546335423322234653144345021
|
||||
043204235524544223242334547537787887788644766889958956876454754674478577343347433636323431422535420
|
||||
132053544243564633627466765775657547755548985595865776876887476476586773674345367554624423333443214
|
||||
434145415151462624423574737777874646757869897568985857768555766747845787346556464366425553353151300
|
||||
033552122413443664556665676655648855845975596556995967956566699866477545537554553645645553335523524
|
||||
413544333326325544236357664865456474858595889756579986899779997968486748464773366656464426231155442
|
||||
004332522265552444763374547484644488886789598575569799777767888975656856667647455453254435233141511
|
||||
035354414545334263444765576467446658985699666669667988675585776867776657644544534734656653433441431
|
||||
442122443263522535743777354555686688857759689956668767555979587695855866658755676536345652311311234
|
||||
231314151362463367455573668565545776596986867687779777769576568988867748688753444556446645553534525
|
||||
045415334236364244375435664665477567587759896768777886876768796985898556758446336336326243331233454
|
||||
342353524362463357737675678645755585686559698888978699669989566777695575467773733463432243352222445
|
||||
244133134532623377737577688575658856867977866698779767897688987856558847648874553573743553546523534
|
||||
021553556544442474674534678684779989779667798977898967696766875855597978664786474743343545232141545
|
||||
045425316553544766676736774475658985557769887677898986998789979958865786565747357474546425542554235
|
||||
123511255462335663763334745464955879987988867676898776768888899576578976678476464757366443234425311
|
||||
325533243442663433734555785478977676679868889789787989899689977975777578786878557777434464652443351
|
||||
341155355546553356347757544788568965668697968997877777976676899896768799766576567645554342336334233
|
||||
421521345226644475636665485646689879769887867898789798988788768788759989487445577633675662663234325
|
||||
225431534432355354466644477659579957678999798898887898798887799686577775655556855457566334643614134
|
||||
254522334642647433556585856548559868968877679888988778897876767896886779646466464564775265336232441
|
||||
312113532345553345433444768455667856989979687989987899997988898888658869478687547476363665422513141
|
||||
152555423534245354346564447446976597699676889998999887989796789797775977768544533373735436335515423
|
||||
341213542653444767556388768885878975698787998799977787779899987685979877545764473357337423424514315
|
||||
322113552445453574657574755545799689867898669877988877997779898765669858668546477767666242656344212
|
||||
523331354363325454365675645869975977878797967999887779897978887888798999548865636336353426535355452
|
||||
523523423432344663765375787446557887679766968878979887789796777987687596555667445376536663356525141
|
||||
551114235346366563637355556866885895767877798798797989787886976886787698548687646557754363564525313
|
||||
251223245223334447766584755447956898986768669897979798997768689676988975874646433446733346424411335
|
||||
214241355255455766365674476646975968859866869967978898867967666685895878644875637565576656255154544
|
||||
455352455253232666575668454867955886956998987678966896777867699977589755646464755747533332342424541
|
||||
444515424223524645435648587576677675775989869686879869776776888598575688877564767577434234326235545
|
||||
431313414332525265734668687845775796567769886777989778777997885578687756877567364366345652533512134
|
||||
333555123263656354334546784878748996895956799868879687768998999597665865666673657464554445565315153
|
||||
234251415355332356354555748857549756797677788669666779776896855789587868886666535745644366551433223
|
||||
425342555454555443357573485485875677656895597978676766678885768675668576658854377547455252453332433
|
||||
323212322142542547356336654885588587756876988878668876996855885757567666665664655447625566342455323
|
||||
145453124156454236753637566754554586888986779659899875656867888757855674866346635564564335311444142
|
||||
241532355322446443663343474577685666578989895777658988767886579994465878877675775646446644411214331
|
||||
144121414316432226357635465574878648885659856586566979685759558986575567664637365426532646212254243
|
||||
110513511415656625443556556674684868678895878785759566769757697468556564753375577435453525411543113
|
||||
010054252444265234547356444555575676545757579699968766586568964546866767534346564224265623245525511
|
||||
120212515152224263635534376453556487856658876995968869757756754456584754356376564445254454534225340
|
||||
334403322452135643232646334553464468686466757956767796875866688444464456337547673543644223334124002
|
||||
424345231535522355445635437664744565688755754586858454858554458475568756654735523654265235423342312
|
||||
031443144424522463542544764444765767644457666757567678447484574545767543635774453342345411351542313
|
||||
321403155315156656344554665775567644878587754546578686767855867554637477555573424362546151514131330
|
||||
100300432342253325546433443636475644557666885556847555458755555684535636335545343542245513545400031
|
||||
241103243345314352532434567355636677768477667655874858784887748363644434657254223342542521512523103
|
||||
013432002255524532425326442376655436757756476784578755546658775744775577372453335234234123311334110
|
||||
224312132351342113543625426364775474475555435685668887764654565767573537642634534534253412535033121
|
||||
342013333254343554666262433657555734443433335545744436575736345777633344224223656361344225330312223
|
||||
234442324252344452264263235643446676735646567555767567465473767566476572456525232422431125340421040
|
||||
030302023035452131512323435555347556466757645454545634364636667436454662255465636151444354004144240
|
||||
002004133214113555342264554265222355765774744775436734535556664773753352634233435412243552441100341
|
||||
011422341232151553353542265363535632735746674664776744464473335435625662443566132542533542101243120
|
||||
203021201322125111143244446544365445365774633366633765656576533325536225455355341443312221224423103
|
||||
311021234200144512534253453355352253535542456754747757743743435344545565465655544322254011444341222
|
||||
101203222013410332153151244543442433553342325366345652255264423465522455665453535113251410414031300
|
||||
103101032340424335231434112162665465333242654445464443552552544226533654451145222233401204012240023
|
||||
300000234304043032354531115322565425623354453256432525255636642566343622125151231523130220222300331
|
||||
320320110121204431154322441252432253656645325532363344542646546553223512543244125154443233034012021
|
||||
223211323021301334023554115315242234323456322565626332455546252636654333515412114103233412041213321
|
||||
020331330004113222113544411442545513454442262444225546542423526322541134153211543210202143401031310
|
||||
033131211214044030334411533413134451124346654653645545223652313534244341353254324212241333001202010
|
||||
011223212020313313300223232314353315524525531133464633645121312251455142242212211423301012011010012
|
||||
202131322330230012310003211315154334411525215354321214224515115324133412515133121340021221223011311
|
||||
102233123000103132110110422414142252322414331525452331522255224253512324323332322332002033332200121
|
||||
010112331222023221443321410332323442443211235352422543351522342441341212433312114143012011012301012
|
||||
102001321100332034112010340431405521533133132514423245132455311232555303030103043321011110131121202
|
||||
102100020233112322130014030443133042115515544424443514351353232214340221142434234013000032002122000
|
5
08/sample.txt
Normal file
5
08/sample.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390
|
132
09/code.js
Normal file
132
09/code.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const fs = require('fs');
|
||||
|
||||
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
const head = [[0, 0]];
|
||||
const tail = [[0, 0]];
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
const dir = contents[0];
|
||||
const dist = contents[1];
|
||||
|
||||
for (m = 1; m <= dist; m++) {
|
||||
let hx = head[0][0];
|
||||
let hy = head[0][1];
|
||||
let tx = tail[0][0];
|
||||
let ty = tail[0][1];
|
||||
|
||||
if (dir === 'U') {
|
||||
hy++;
|
||||
} else if (dir === "D") {
|
||||
hy--;
|
||||
} else if (dir === "L") {
|
||||
hx--;
|
||||
} else if (dir === "R") {
|
||||
hx++;
|
||||
}
|
||||
|
||||
head.unshift([hx, hy]);
|
||||
tail.unshift(walk(hx, hy, tx, ty));
|
||||
}
|
||||
}
|
||||
|
||||
let strings = [];
|
||||
for (pos of tail) {
|
||||
strings.push(pos.join('-'));
|
||||
}
|
||||
let touched = [...new Set(strings)];
|
||||
|
||||
console.log(touched.length);
|
||||
|
||||
// Part Two
|
||||
|
||||
const h = [[0, 0]];
|
||||
const one = [[0, 0]];
|
||||
const two = [[0, 0]];
|
||||
const three = [[0, 0]];
|
||||
const four = [[0, 0]];
|
||||
const five = [[0, 0]];
|
||||
const six = [[0, 0]];
|
||||
const seven = [[0, 0]];
|
||||
const eight = [[0, 0]];
|
||||
const t = [[0, 0]];
|
||||
|
||||
for (i in inputArray) {
|
||||
const contents = inputArray[i].split(" ");
|
||||
const dir = contents[0];
|
||||
const dist = contents[1];
|
||||
|
||||
for (m = 1; m <= dist; m++) {
|
||||
let hx = h[0][0];
|
||||
let hy = h[0][1];
|
||||
|
||||
if (dir === 'U') {
|
||||
hy++;
|
||||
} else if (dir === "D") {
|
||||
hy--;
|
||||
} else if (dir === "L") {
|
||||
hx--;
|
||||
} else if (dir === "R") {
|
||||
hx++;
|
||||
}
|
||||
|
||||
h.unshift([hx, hy]);
|
||||
one.unshift(walk(hx, hy, one[0][0], one[0][1]));
|
||||
two.unshift(walk(one[0][0], one[0][1], two[0][0], two[0][1]));
|
||||
three.unshift(walk(two[0][0], two[0][1], three[0][0], three[0][1]));
|
||||
four.unshift(walk(three[0][0], three[0][1], four[0][0], four[0][1]));
|
||||
five.unshift(walk(four[0][0], four[0][1], five[0][0], five[0][1]));
|
||||
six.unshift(walk(five[0][0], five[0][1], six[0][0], six[0][1]));
|
||||
seven.unshift(walk(six[0][0], six[0][1], seven[0][0], seven[0][1]));
|
||||
eight.unshift(walk(seven[0][0], seven[0][1], eight[0][0], eight[0][1]));
|
||||
t.unshift(walk(eight[0][0], eight[0][1], t[0][0], t[0][1]));
|
||||
}
|
||||
}
|
||||
|
||||
strings = [];
|
||||
for (pos of t) {
|
||||
strings.push(pos.join('-'));
|
||||
}
|
||||
touched = [...new Set(strings)];
|
||||
|
||||
console.log(touched.length);
|
||||
|
||||
// functions
|
||||
|
||||
function walk(hx, hy, tx, ty) {
|
||||
if ((hx > tx) && Math.abs(hx - tx) >= 2) {
|
||||
tx++;
|
||||
if (hy > ty) {
|
||||
ty++;
|
||||
} else if (ty > hy) {
|
||||
ty--;
|
||||
}
|
||||
} else if ((tx > hx) && Math.abs(tx - hx) >= 2) {
|
||||
tx--;
|
||||
if (hy > ty) {
|
||||
ty++;
|
||||
} else if (ty > hy) {
|
||||
ty--;
|
||||
}
|
||||
} else if ((hy > ty) && Math.abs(hy - ty) >= 2) {
|
||||
ty++;
|
||||
if (hx > tx) {
|
||||
tx++;
|
||||
} else if (tx > hx) {
|
||||
tx--;
|
||||
}
|
||||
} else if ((ty > hy) && Math.abs(ty - hy) >= 2) {
|
||||
ty--;
|
||||
if (hx > tx) {
|
||||
tx++;
|
||||
} else if (tx > hx) {
|
||||
tx--;
|
||||
}
|
||||
}
|
||||
|
||||
return [tx, ty]
|
||||
}
|
2000
09/input.txt
Normal file
2000
09/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
8
09/sample.txt
Normal file
8
09/sample.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
68
10/code.js
Normal file
68
10/code.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const fs = require('fs');
|
||||
|
||||
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
let x = 1;
|
||||
let cycle = 0;
|
||||
let samples = [];
|
||||
|
||||
for (i in inputArray) {
|
||||
cycle++;
|
||||
getSample();
|
||||
const contents = inputArray[i].split(" ");
|
||||
const instruction = contents[0];
|
||||
if (instruction === 'addx') {
|
||||
cycle++;
|
||||
getSample();
|
||||
x += parseInt(contents[1], 10);
|
||||
}
|
||||
}
|
||||
|
||||
const countedSamples = samples.slice(0, 7);
|
||||
console.log(countedSamples.reduce((partialSum, a) => partialSum + a, 0));
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
x = 1;
|
||||
cycle = 0;
|
||||
let pixels = [];
|
||||
|
||||
for (i in inputArray) {
|
||||
cycle++;
|
||||
setPixel();
|
||||
const contents = inputArray[i].split(" ");
|
||||
const instruction = contents[0];
|
||||
if (instruction === 'addx') {
|
||||
cycle++;
|
||||
setPixel();
|
||||
x += parseInt(contents[1], 10);
|
||||
}
|
||||
}
|
||||
|
||||
const screen = [];
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const line = pixels.slice(i * 40, (i + 1) * 40).join('');
|
||||
screen.push(line)
|
||||
}
|
||||
console.log(screen);
|
||||
|
||||
// functions
|
||||
|
||||
function getSample() {
|
||||
if (cycle === 20 || (cycle - 20) % 40 === 0) {
|
||||
samples.push(cycle * x);
|
||||
}
|
||||
}
|
||||
|
||||
function setPixel() {
|
||||
const position = cycle % 40;
|
||||
if (position >= x && position <= x + 2) {
|
||||
pixels.push('#');
|
||||
} else {
|
||||
pixels.push('.');
|
||||
}
|
||||
}
|
139
10/input.txt
Normal file
139
10/input.txt
Normal file
@@ -0,0 +1,139 @@
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx 1
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx -1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx -38
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx -5
|
||||
addx 12
|
||||
addx 2
|
||||
addx 27
|
||||
addx -40
|
||||
addx 19
|
||||
addx 2
|
||||
addx 19
|
||||
addx -18
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -23
|
||||
addx 22
|
||||
addx 4
|
||||
addx -34
|
||||
addx -1
|
||||
addx 5
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
addx 20
|
||||
addx -17
|
||||
noop
|
||||
addx 25
|
||||
addx -17
|
||||
addx -2
|
||||
noop
|
||||
addx 3
|
||||
addx 19
|
||||
addx -12
|
||||
addx 3
|
||||
addx -2
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -37
|
||||
addx 3
|
||||
addx 4
|
||||
noop
|
||||
addx 24
|
||||
addx -6
|
||||
addx -15
|
||||
addx 2
|
||||
noop
|
||||
addx 6
|
||||
addx -2
|
||||
addx 6
|
||||
addx -12
|
||||
addx -2
|
||||
addx 19
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx -24
|
||||
addx -11
|
||||
addx 4
|
||||
addx 3
|
||||
addx -2
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx -35
|
||||
addx 35
|
||||
addx -21
|
||||
addx -14
|
||||
noop
|
||||
addx 5
|
||||
addx 2
|
||||
addx 33
|
||||
addx -7
|
||||
addx -23
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx -23
|
||||
addx 30
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
addx -17
|
||||
addx 11
|
||||
noop
|
||||
noop
|
146
10/sample.txt
Normal file
146
10/sample.txt
Normal file
@@ -0,0 +1,146 @@
|
||||
addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop
|
163
11/code.js
Normal file
163
11/code.js
Normal file
@@ -0,0 +1,163 @@
|
||||
|
||||
const sampleMonkeys = [
|
||||
[
|
||||
[79, 98],
|
||||
['*', 19],
|
||||
[23, 2, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[54, 65, 75, 74],
|
||||
['+', 6],
|
||||
[19, 2, 0],
|
||||
0
|
||||
],
|
||||
[
|
||||
[79, 60, 97],
|
||||
['*', 'self'],
|
||||
[13, 1, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[74],
|
||||
['+', 3],
|
||||
[17, 0, 1],
|
||||
0
|
||||
],
|
||||
]
|
||||
|
||||
const inputMonkeys = [
|
||||
[
|
||||
[91, 58, 52, 69, 95, 54],
|
||||
['*', 13],
|
||||
[7, 1, 5],
|
||||
0
|
||||
],
|
||||
[
|
||||
[80, 80, 97, 84],
|
||||
['*', 'self'],
|
||||
[3, 3, 5],
|
||||
0
|
||||
],
|
||||
[
|
||||
[86, 92, 71],
|
||||
['+', 7],
|
||||
[2, 0, 4],
|
||||
0
|
||||
],
|
||||
[
|
||||
[96, 90, 99, 76, 79, 85, 98, 61],
|
||||
['+', 4],
|
||||
[11, 7, 6],
|
||||
0
|
||||
],
|
||||
[
|
||||
[60, 83, 68, 64, 73],
|
||||
['*', 19],
|
||||
[17, 1, 0],
|
||||
0
|
||||
],
|
||||
[
|
||||
[96, 52, 52, 94, 76, 51, 57],
|
||||
['+', 3],
|
||||
[5, 7, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[75],
|
||||
['+', 5],
|
||||
[13, 4, 2],
|
||||
0
|
||||
],
|
||||
[
|
||||
[83, 75],
|
||||
['+', 1],
|
||||
[19, 2, 6],
|
||||
0
|
||||
]
|
||||
]
|
||||
|
||||
// Part One
|
||||
//let monkeys = JSON.parse(JSON.stringify(sampleMonkeys));
|
||||
let monkeys = JSON.parse(JSON.stringify(inputMonkeys));
|
||||
|
||||
for (let i = 0; i < 20; i++) {
|
||||
for (const m in monkeys) {
|
||||
const items = monkeys[m][0];
|
||||
const op = monkeys[m][1];
|
||||
const test = monkeys[m][2];
|
||||
for (const item of items) {
|
||||
monkeys[m][3]++;
|
||||
let newLevel;
|
||||
if (op[0] === '*') {
|
||||
if (op[1] === 'self') {
|
||||
newLevel = item * item;
|
||||
} else {
|
||||
newLevel = item * op[1];
|
||||
}
|
||||
} else {
|
||||
newLevel = item + op[1];
|
||||
}
|
||||
newLevel = Math.floor(newLevel / 3);
|
||||
if (newLevel % test[0] === 0) {
|
||||
monkeys[test[1]][0].push(newLevel);
|
||||
} else {
|
||||
monkeys[test[2]][0].push(newLevel);
|
||||
}
|
||||
}
|
||||
monkeys[m][0] = [];
|
||||
}
|
||||
}
|
||||
|
||||
let counts = monkeys.map(m => m[3]);
|
||||
counts.sort(function(a, b) {
|
||||
return b - a;
|
||||
});
|
||||
|
||||
console.log(counts[0] * counts[1]);
|
||||
|
||||
|
||||
// Part Two
|
||||
//monkeys = JSON.parse(JSON.stringify(sampleMonkeys));
|
||||
monkeys = JSON.parse(JSON.stringify(inputMonkeys));
|
||||
|
||||
// Common denominator trick to keep numbers small outright stolen from
|
||||
// https://git.genehack.net/genehack/advent-of-code-2022/src/branch/main/11/day11_2.js
|
||||
let COMMON = 1;
|
||||
for (const monkey of monkeys) {
|
||||
COMMON *= monkey[2][0];
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
for (const m in monkeys) {
|
||||
const items = monkeys[m][0];
|
||||
const op = monkeys[m][1];
|
||||
const test = monkeys[m][2];
|
||||
for (const n in items) {
|
||||
monkeys[m][3] = monkeys[m][3] + 1;
|
||||
let newLevel;
|
||||
if (op[0] === '*') {
|
||||
if (op[1] === 'self') {
|
||||
newLevel = monkeys[m][0][n] ** 2;
|
||||
} else {
|
||||
newLevel = monkeys[m][0][n] * op[1];
|
||||
}
|
||||
} else {
|
||||
newLevel = monkeys[m][0][n] + op[1];
|
||||
}
|
||||
newLevel = newLevel % COMMON
|
||||
const pass = newLevel % (test[0]) == 0 ? true : false;
|
||||
monkeys[pass ? test[1] : test[2]][0].push(newLevel);
|
||||
}
|
||||
monkeys[m][0] = [];
|
||||
}
|
||||
}
|
||||
|
||||
counts = monkeys.map(m => m[3]);
|
||||
counts.sort(function(a, b) {
|
||||
return b - a;
|
||||
});
|
||||
|
||||
console.log(counts[0] * counts[1]);
|
||||
|
||||
// functions
|
106
11/input.txt
Normal file
106
11/input.txt
Normal file
@@ -0,0 +1,106 @@
|
||||
const inputMonkeys = [
|
||||
[
|
||||
[91, 58, 52, 69, 95, 54],
|
||||
['*' 13],
|
||||
[7, 1, 5],
|
||||
0
|
||||
],
|
||||
[
|
||||
[80, 80, 97, 84],
|
||||
['*' 'self'],
|
||||
[3, 3, 5],
|
||||
0
|
||||
],
|
||||
[
|
||||
[86, 92, 71],
|
||||
['+' 7],
|
||||
[2, 0, 4],
|
||||
0
|
||||
],
|
||||
[
|
||||
[96, 90, 99, 76, 79, 85, 98, 61],
|
||||
['+' 4],
|
||||
[11, 7, 6],
|
||||
0
|
||||
],
|
||||
[
|
||||
[60, 83, 68, 64, 73],
|
||||
['*' 19],
|
||||
[17, 1, 0],
|
||||
0
|
||||
],
|
||||
[
|
||||
[96, 52, 52, 94, 76, 51, 57],
|
||||
['+' 3],
|
||||
[5, 7, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[75],
|
||||
['+' 5],
|
||||
[13, 4, 2],
|
||||
0
|
||||
],
|
||||
[
|
||||
[83, 75],
|
||||
['+' 1],
|
||||
[19, 2, 6],
|
||||
0
|
||||
]
|
||||
]
|
||||
|
||||
Monkey 0:
|
||||
Starting items: 91, 58, 52, 69, 95, 54
|
||||
Operation: new = old * 13
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 5
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 80, 80, 97, 84
|
||||
Operation: new = old * old
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 5
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 86, 92, 71
|
||||
Operation: new = old + 7
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 96, 90, 99, 76, 79, 85, 98, 61
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 6
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 60, 83, 68, 64, 73
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 96, 52, 52, 94, 76, 51, 57
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 75
|
||||
Operation: new = old + 5
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 4
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 83, 75
|
||||
Operation: new = old + 1
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 6
|
54
11/sample.txt
Normal file
54
11/sample.txt
Normal file
@@ -0,0 +1,54 @@
|
||||
Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 54, 65, 75, 74
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 79, 60, 97
|
||||
Operation: new = old * old
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 74
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 1
|
||||
|
||||
const sampleMonkeys = [
|
||||
[
|
||||
[79, 98],
|
||||
['*', 19],
|
||||
[23, 2, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[54, 65, 75, 74],
|
||||
['+', 6],
|
||||
[19, 2, 0],
|
||||
0
|
||||
],
|
||||
[
|
||||
[79, 60, 97],
|
||||
['*', 'self'],
|
||||
[13, 1, 3],
|
||||
0
|
||||
],
|
||||
[
|
||||
[74],
|
||||
['+', 3],
|
||||
[17, 0, 1],
|
||||
0
|
||||
],
|
||||
]
|
Reference in New Issue
Block a user