diff --git a/scripts/aoc2015/day10/puzzle1.ts b/scripts/aoc2015/day10/puzzle1.ts new file mode 100644 index 0000000..8fa197d --- /dev/null +++ b/scripts/aoc2015/day10/puzzle1.ts @@ -0,0 +1,59 @@ +/** +--- Day 10: Elves Look, Elves Say --- +Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s). + +Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1). + + For example: + + 1 becomes 11 (1 copy of digit 1). +11 becomes 21 (2 copies of digit 1). +21 becomes 1211 (one 2 followed by one 1). +1211 becomes 111221 (one 1, one 2, and two 1s). +111221 becomes 312211 (three 1s, two 2s, and one 1). +Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result? + + Your puzzle input is 3113322113. + +**/ +import { splitTextToGroups, transformGroup } from "./utils.ts"; + +// inputs +// Your puzzle input is 3113322113. + +// algorithm + +// 1. split a string to groups of same letters +// for input "111221" +// split to: "111", "22", "1" + +// 2. transform group based on group char and group length +// for input "111" char is "1" and length is "3", eg. transformed group is "31" + +// 3. merge groups together + +function processText(text: string): string { + const groups = splitTextToGroups(text); + const transformedGroups = groups.map((group) => transformGroup(group)); + return transformedGroups.join(""); +} + +let text = "3113322113"; +// we need to apply steps 1., 2., 3. 40 times. So we get result +for (let i = 0; i < 40; i++) { + text = processText(text); +} + +console.log(text.length); + +// expected output: length of process result applied 40 times. + +// puzzle 2 + +text = "3113322113"; +// we need to apply steps 1., 2., 3. 50 times. So we get result +for (let i = 0; i < 50; i++) { + text = processText(text); +} + +console.log(text.length); diff --git a/scripts/aoc2015/day10/utils.test.ts b/scripts/aoc2015/day10/utils.test.ts new file mode 100644 index 0000000..ab799fb --- /dev/null +++ b/scripts/aoc2015/day10/utils.test.ts @@ -0,0 +1,24 @@ +import { assertEquals } from "jsr:@std/assert@1"; +import { describe, it } from "@std/testing/bdd"; +import { splitTextToGroups } from "./utils.ts"; + +describe("splitTextToGroups", () => { + it("Should return empty array for empty string", function () { + assertEquals(splitTextToGroups(""), []); + }); + it('Should return ["1"] for string "1"', function () { + assertEquals(splitTextToGroups("1"), ["1"]); + }); + it('Should return ["11"] for string "11"', function () { + assertEquals(splitTextToGroups("11"), ["11"]); + }); + it('Should return ["2", "1"] for string "21"', function () { + assertEquals(splitTextToGroups("21"), ["2", "1"]); + }); + it('Should return ["1", "2", "11"] for string "1211"', function () { + assertEquals(splitTextToGroups("1211"), ["1", "2", "11"]); + }); + it('Should return ["111", "22", "1"] for string "111221"', function () { + assertEquals(splitTextToGroups("111221"), ["111", "22", "1"]); + }); +}); diff --git a/scripts/aoc2015/day10/utils.ts b/scripts/aoc2015/day10/utils.ts new file mode 100644 index 0000000..6c3a6f2 --- /dev/null +++ b/scripts/aoc2015/day10/utils.ts @@ -0,0 +1,42 @@ +// 1. split a string to groups of same letters +// for input "111221" +// split to: "111", "22", "1" + +export function splitTextToGroups(text: string): string[] { + // if text is empty, return empty array + if (!text) { + return []; + } + // while text is not empty do: + // loop + const resultArray: string[] = []; + while (text.length > 0) { + // make char at position 0 the reference char + const refChar = text[0]; + // set buffer to "" + let buffer = ""; + // loop + // if it is NOT equal to reference char + // end loop + while (text[0] === refChar) { + // add the ref char to the buffer + buffer = buffer + refChar; + text = text.slice(1); + // make text shorter + } + // add buffer to result array + resultArray.push(buffer); + } + // end loop + // return result array + return resultArray; +} + +// 2. transform group based on group char and group length +// for input "111" char is "1" and length is "3", eg. transformed group is "31" +export function transformGroup(group: string): string { + if (group.length === 0) { + throw new Error("Group length is 0"); + } + return `${group.length}${group[0]}`; +}