Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/aoc2015/day12/input.txt

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions scripts/aoc2015/day12/puzzle1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
--- Day 12: JSAbacusFramework.io ---
Santa's Accounting-Elves need help balancing the books after a recent order. Unfortunately, their accounting software uses a peculiar storage format. That's where you come in.

They have a JSON document which contains a variety of things: arrays ([1,2,3]), objects ({"a":1, "b":2}), numbers, and strings. Your first job is to simply find all of the numbers throughout the document and add them together.

For example:

[1,2,3] and {"a":2,"b":4} both have a sum of 6.
[[[3]]] and {"a":{"b":4},"c":-1} both have a sum of 3.
{"a":[-1,1]} and [-1,{"a":1}] both have a sum of 0.
[] and {} both have a sum of 0.
You will not encounter any strings containing numbers.

What is the sum of all numbers in the document?

*/

import { getNumbersFromText } from "./utils.ts";

async function processFile(filename: string): Promise<number> {
// read the file string
const input = await Deno.readTextFile(filename);
const allNumbers = getNumbersFromText(input);
// sum all digits together
return allNumbers.reduce(
(accumulator: number, number: number) => accumulator + number,
0,
);
}

processFile("./simpleInput.txt").then((result) =>
console.log("result: ", result)
);

processFile("./input.txt").then((result) => console.log("result: ", result));
37 changes: 37 additions & 0 deletions scripts/aoc2015/day12/puzzle2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
--- Part Two ---
Uh oh - the Accounting-Elves have realized that they double-counted everything red.

Ignore any object (and all of its children) which has any property with the value "red". Do this only for objects ({...}), not arrays ([...]).

[1,2,3] still has a sum of 6.
[1,{"c":"red","b":2},3] now has a sum of 4, because the middle object is ignored.
{"d":"red","e":[1,2,3,4],"f":5} now has a sum of 0, because the entire structure is ignored.
[1,"red",5] has a sum of 6, because "red" in an array has no effect.
*/

// read input
// parseJson

// let have a counter

// sumNumbersOfObject:

// if a value of object is "red" - return 0 for the object
// if value is of type number - add it to counter
// if value of object is array
// - sum all it's numbers
// - call all values of type object and sumNumberOfObject and sum them
// if value of object is another object, transform it with sumNumbersOfObject

import { sumNumbersOfObject } from "./utils.ts";

async function processFile(filename: string): Promise<number> {
const input = await Deno.readTextFile(filename);
const o = JSON.parse(input);
return sumNumbersOfObject(o);
}

processFile("./input.txt").then((result) => {
console.log("result: ", result);
});
1 change: 1 addition & 0 deletions scripts/aoc2015/day12/simpleInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":{"b":4},"c":-1}
72 changes: 72 additions & 0 deletions scripts/aoc2015/day12/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it } from "@std/testing/bdd";
import { assertEquals } from "@std/assert";
import { getNumbersFromText, sumNumbersOfObject } from "./utils.ts";

describe("getNumbersFromText", () => {
it("should return empty array if text has no digits", function () {
assertEquals(getNumbersFromText("abc"), []);
});
it("should return array of numbers for text with numbers", function () {
assertEquals(getNumbersFromText("[1,-2,3]"), [1, -2, 3]);
assertEquals(getNumbersFromText(`{"a":2,"b":4}`), [2, 4]);
assertEquals(getNumbersFromText("[[[3]]]"), [3]);
assertEquals(getNumbersFromText(`{"a":{"b":4},"c":-1}`), [4, -1]);
assertEquals(getNumbersFromText(`{"a":[-1,1]}`), [-1, 1]);
assertEquals(getNumbersFromText(`[-1,{"a":1}]`), [-1, 1]);
});
});

describe("sumNumbersOfObject", () => {
it("should return 0 for empty object", function () {
assertEquals(sumNumbersOfObject({}), 0);
});
it("should return 3 for non-empty object", function () {
assertEquals(
sumNumbersOfObject({
a: 1,
b: 2,
}),
3,
);
});

it("should return count for object with array", function () {
assertEquals(
sumNumbersOfObject({
a: [1, 2],
}),
3,
);
});
it("should return 0 for object with 'red' value", function () {
assertEquals(
sumNumbersOfObject({
a: 3,
b: "red",
}),
0,
);
});
it("should return count for nested objects", function () {
assertEquals(
sumNumbersOfObject({
a: {
aa: 1,
ab: 2,
},
}),
3,
);
});
it("should return count for objects nested in arrays", function () {
assertEquals(
sumNumbersOfObject({
a: [1, 2, {
aa: 1,
ab: 2,
}],
}),
6,
);
});
});
42 changes: 42 additions & 0 deletions scripts/aoc2015/day12/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const numbersRegex = /(-)?([0-9]+)/g;

export function getNumbersFromText(text: string): number[] {
const result: number[] = [];
const allMatchesIterator = text.matchAll(numbersRegex);
for (const match of allMatchesIterator) {
const num = parseInt(match[0], 10);
if (!Number.isNaN(num)) {
result.push(num);
}
}
return result;
}

export function sumNumbersOfArray(arr: any[], count: number): number {
for (const item of arr) {
if (typeof item === "number") {
count = count + item;
} else if (Array.isArray(item)) {
count = sumNumbersOfArray(item, count);
} else if (typeof item === "object") {
count = sumNumbersOfObject(item, count);
}
}
return count;
}

export function sumNumbersOfObject(o: object, count = 0): number {
if (Object.values(o).find((v) => v === "red")) {
return count;
}
for (const value of Object.values(o)) {
if (typeof value === "number") {
count = count + value;
} else if (Array.isArray(value)) {
count = sumNumbersOfArray(value, count);
} else if (typeof value === "object") {
count = sumNumbersOfObject(value, count);
}
}
return count;
}
Loading