Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AoC - Day10 #7

Merged
merged 5 commits into from
Sep 20, 2024
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
59 changes: 59 additions & 0 deletions scripts/aoc2015/day10/puzzle1.ts
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions scripts/aoc2015/day10/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
42 changes: 42 additions & 0 deletions scripts/aoc2015/day10/utils.ts
Original file line number Diff line number Diff line change
@@ -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]}`;
}
Loading