From bdd0d274d67d3a064be30ebe00d5ecaa0bd50636 Mon Sep 17 00:00:00 2001 From: Sean Metzgar Date: Mon, 5 Dec 2022 02:10:50 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=84Day=2005:=20Puzzle=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/day05.txt | 513 +++++++++++++++++++++++++++++++++++++++++++ src/puzzles/day05.ts | 78 +++++++ src/puzzles/index.ts | 4 +- src/types.ts | 2 +- 4 files changed, 595 insertions(+), 2 deletions(-) create mode 100644 data/day05.txt create mode 100644 src/puzzles/day05.ts diff --git a/data/day05.txt b/data/day05.txt new file mode 100644 index 0000000..02d0db7 --- /dev/null +++ b/data/day05.txt @@ -0,0 +1,513 @@ +[C] [S] [H] +[F] [B] [C] [S] [W] +[B] [W] [W] [M] [S] [B] +[L] [H] [G] [L] [P] [F] [Q] +[D] [P] [J] [F] [T] [G] [M] [T] +[P] [G] [B] [N] [L] [W] [P] [W] [R] +[Z] [V] [W] [J] [J] [C] [T] [S] [C] +[S] [N] [F] [G] [W] [B] [H] [F] [N] + 1 2 3 4 5 6 7 8 9 + +move 2 from 5 to 9 +move 3 from 1 to 7 +move 2 from 3 to 9 +move 6 from 9 to 5 +move 2 from 3 to 8 +move 9 from 7 to 8 +move 15 from 8 to 9 +move 3 from 1 to 6 +move 6 from 4 to 2 +move 6 from 5 to 6 +move 1 from 4 to 2 +move 14 from 6 to 2 +move 2 from 1 to 5 +move 1 from 7 to 3 +move 1 from 4 to 8 +move 2 from 5 to 6 +move 25 from 2 to 4 +move 2 from 6 to 4 +move 1 from 8 to 1 +move 2 from 9 to 1 +move 1 from 6 to 1 +move 2 from 1 to 7 +move 1 from 7 to 3 +move 2 from 1 to 8 +move 1 from 2 to 6 +move 1 from 3 to 8 +move 4 from 5 to 6 +move 1 from 5 to 3 +move 1 from 9 to 6 +move 2 from 3 to 4 +move 1 from 2 to 6 +move 12 from 9 to 7 +move 1 from 9 to 1 +move 1 from 5 to 8 +move 1 from 3 to 8 +move 28 from 4 to 5 +move 1 from 4 to 3 +move 1 from 2 to 6 +move 1 from 3 to 9 +move 12 from 7 to 2 +move 1 from 9 to 6 +move 6 from 6 to 4 +move 1 from 7 to 4 +move 1 from 1 to 2 +move 28 from 5 to 1 +move 2 from 2 to 8 +move 3 from 8 to 2 +move 7 from 4 to 1 +move 4 from 8 to 6 +move 9 from 2 to 8 +move 7 from 6 to 5 +move 3 from 5 to 9 +move 1 from 9 to 7 +move 1 from 7 to 1 +move 5 from 8 to 4 +move 4 from 1 to 9 +move 6 from 9 to 4 +move 5 from 1 to 5 +move 5 from 2 to 3 +move 4 from 8 to 2 +move 5 from 1 to 4 +move 4 from 5 to 9 +move 9 from 4 to 9 +move 10 from 9 to 8 +move 1 from 9 to 1 +move 2 from 2 to 8 +move 4 from 3 to 8 +move 1 from 2 to 3 +move 2 from 9 to 2 +move 1 from 2 to 6 +move 4 from 4 to 3 +move 3 from 5 to 1 +move 12 from 1 to 4 +move 1 from 5 to 3 +move 1 from 5 to 3 +move 5 from 8 to 5 +move 7 from 8 to 5 +move 8 from 3 to 4 +move 1 from 5 to 1 +move 1 from 6 to 7 +move 2 from 1 to 6 +move 8 from 5 to 9 +move 2 from 5 to 1 +move 9 from 1 to 4 +move 20 from 4 to 2 +move 1 from 5 to 2 +move 4 from 4 to 2 +move 5 from 9 to 2 +move 2 from 8 to 9 +move 23 from 2 to 4 +move 2 from 2 to 5 +move 5 from 1 to 2 +move 28 from 4 to 3 +move 2 from 8 to 1 +move 2 from 5 to 7 +move 1 from 6 to 9 +move 1 from 4 to 8 +move 1 from 8 to 9 +move 1 from 4 to 6 +move 2 from 7 to 2 +move 13 from 3 to 4 +move 5 from 9 to 7 +move 1 from 9 to 6 +move 14 from 2 to 6 +move 1 from 4 to 1 +move 10 from 3 to 2 +move 1 from 6 to 9 +move 2 from 3 to 2 +move 3 from 1 to 9 +move 1 from 3 to 5 +move 3 from 9 to 3 +move 6 from 7 to 4 +move 1 from 9 to 4 +move 1 from 9 to 2 +move 1 from 5 to 3 +move 5 from 3 to 1 +move 17 from 4 to 7 +move 2 from 2 to 8 +move 1 from 3 to 9 +move 1 from 8 to 2 +move 1 from 9 to 6 +move 4 from 6 to 2 +move 10 from 6 to 5 +move 4 from 1 to 5 +move 15 from 2 to 9 +move 1 from 8 to 6 +move 1 from 2 to 8 +move 6 from 9 to 2 +move 3 from 4 to 8 +move 11 from 7 to 1 +move 6 from 9 to 6 +move 1 from 6 to 2 +move 3 from 9 to 3 +move 6 from 2 to 7 +move 6 from 7 to 8 +move 7 from 1 to 9 +move 4 from 1 to 6 +move 2 from 1 to 2 +move 4 from 6 to 7 +move 1 from 2 to 9 +move 1 from 2 to 3 +move 1 from 2 to 1 +move 6 from 8 to 4 +move 2 from 6 to 7 +move 13 from 5 to 9 +move 1 from 5 to 4 +move 3 from 4 to 7 +move 1 from 1 to 7 +move 14 from 9 to 2 +move 2 from 9 to 3 +move 3 from 8 to 5 +move 4 from 3 to 4 +move 8 from 4 to 1 +move 7 from 1 to 9 +move 5 from 6 to 9 +move 4 from 9 to 2 +move 1 from 1 to 9 +move 17 from 2 to 4 +move 1 from 6 to 3 +move 4 from 7 to 5 +move 5 from 7 to 5 +move 1 from 6 to 4 +move 1 from 8 to 3 +move 5 from 7 to 1 +move 2 from 7 to 6 +move 2 from 3 to 6 +move 1 from 2 to 9 +move 7 from 9 to 6 +move 2 from 3 to 7 +move 8 from 6 to 4 +move 3 from 9 to 2 +move 1 from 6 to 4 +move 26 from 4 to 8 +move 2 from 7 to 8 +move 5 from 5 to 9 +move 2 from 6 to 7 +move 4 from 9 to 1 +move 2 from 7 to 5 +move 14 from 8 to 6 +move 3 from 2 to 8 +move 3 from 6 to 8 +move 3 from 6 to 1 +move 10 from 8 to 4 +move 5 from 9 to 4 +move 3 from 8 to 5 +move 1 from 8 to 2 +move 12 from 4 to 8 +move 1 from 9 to 3 +move 6 from 6 to 4 +move 6 from 8 to 2 +move 1 from 3 to 8 +move 1 from 8 to 4 +move 10 from 1 to 9 +move 2 from 1 to 3 +move 7 from 4 to 9 +move 1 from 2 to 1 +move 11 from 8 to 9 +move 1 from 3 to 9 +move 2 from 2 to 7 +move 1 from 3 to 6 +move 2 from 7 to 9 +move 2 from 4 to 6 +move 4 from 6 to 4 +move 2 from 2 to 8 +move 2 from 8 to 4 +move 1 from 1 to 7 +move 2 from 2 to 8 +move 9 from 5 to 2 +move 3 from 5 to 9 +move 1 from 8 to 3 +move 30 from 9 to 7 +move 1 from 6 to 2 +move 7 from 4 to 8 +move 13 from 7 to 2 +move 8 from 7 to 4 +move 2 from 4 to 8 +move 8 from 8 to 1 +move 1 from 8 to 3 +move 2 from 8 to 9 +move 1 from 3 to 7 +move 5 from 7 to 6 +move 1 from 3 to 1 +move 7 from 4 to 8 +move 20 from 2 to 6 +move 2 from 2 to 7 +move 1 from 9 to 5 +move 4 from 7 to 6 +move 3 from 7 to 8 +move 1 from 7 to 2 +move 7 from 8 to 6 +move 3 from 6 to 7 +move 4 from 9 to 1 +move 1 from 2 to 6 +move 1 from 9 to 7 +move 1 from 2 to 8 +move 1 from 7 to 6 +move 3 from 6 to 3 +move 4 from 8 to 1 +move 8 from 6 to 4 +move 3 from 7 to 2 +move 1 from 3 to 2 +move 1 from 4 to 5 +move 2 from 3 to 5 +move 1 from 4 to 6 +move 4 from 1 to 5 +move 4 from 2 to 9 +move 2 from 1 to 6 +move 4 from 9 to 2 +move 3 from 2 to 8 +move 2 from 8 to 4 +move 13 from 6 to 1 +move 4 from 5 to 2 +move 14 from 6 to 3 +move 1 from 2 to 7 +move 2 from 2 to 4 +move 1 from 8 to 6 +move 1 from 6 to 3 +move 1 from 7 to 4 +move 1 from 2 to 3 +move 1 from 2 to 6 +move 11 from 4 to 6 +move 2 from 5 to 4 +move 1 from 5 to 6 +move 12 from 3 to 6 +move 1 from 3 to 7 +move 1 from 5 to 7 +move 3 from 3 to 6 +move 2 from 7 to 5 +move 2 from 5 to 2 +move 8 from 6 to 7 +move 24 from 1 to 3 +move 1 from 4 to 6 +move 10 from 3 to 1 +move 6 from 1 to 8 +move 1 from 6 to 3 +move 1 from 4 to 2 +move 1 from 3 to 1 +move 2 from 2 to 1 +move 1 from 7 to 6 +move 2 from 7 to 5 +move 4 from 3 to 7 +move 1 from 2 to 3 +move 6 from 1 to 6 +move 3 from 7 to 5 +move 4 from 7 to 8 +move 1 from 1 to 2 +move 1 from 2 to 7 +move 8 from 3 to 4 +move 3 from 4 to 7 +move 6 from 8 to 6 +move 2 from 3 to 2 +move 1 from 3 to 9 +move 5 from 5 to 1 +move 2 from 8 to 2 +move 1 from 9 to 2 +move 4 from 1 to 3 +move 3 from 2 to 9 +move 1 from 1 to 2 +move 2 from 9 to 7 +move 2 from 2 to 9 +move 8 from 7 to 5 +move 33 from 6 to 5 +move 20 from 5 to 9 +move 21 from 5 to 7 +move 17 from 7 to 6 +move 10 from 6 to 9 +move 5 from 4 to 7 +move 2 from 3 to 9 +move 1 from 2 to 3 +move 2 from 7 to 3 +move 3 from 9 to 5 +move 23 from 9 to 7 +move 8 from 9 to 6 +move 1 from 9 to 1 +move 1 from 5 to 3 +move 1 from 8 to 9 +move 5 from 6 to 8 +move 1 from 9 to 6 +move 18 from 7 to 2 +move 6 from 7 to 4 +move 6 from 4 to 8 +move 5 from 7 to 4 +move 6 from 6 to 3 +move 1 from 4 to 2 +move 10 from 2 to 1 +move 1 from 2 to 4 +move 7 from 1 to 6 +move 1 from 7 to 1 +move 11 from 6 to 2 +move 1 from 6 to 8 +move 12 from 3 to 1 +move 8 from 1 to 8 +move 2 from 5 to 2 +move 12 from 8 to 6 +move 15 from 2 to 4 +move 7 from 4 to 5 +move 4 from 5 to 9 +move 4 from 9 to 4 +move 5 from 4 to 6 +move 2 from 5 to 2 +move 1 from 2 to 5 +move 2 from 5 to 4 +move 2 from 1 to 3 +move 4 from 1 to 5 +move 2 from 8 to 4 +move 5 from 2 to 9 +move 17 from 6 to 8 +move 1 from 3 to 2 +move 2 from 5 to 4 +move 1 from 3 to 8 +move 1 from 1 to 6 +move 2 from 5 to 6 +move 3 from 9 to 5 +move 1 from 5 to 1 +move 3 from 1 to 8 +move 26 from 8 to 4 +move 1 from 5 to 3 +move 3 from 2 to 7 +move 1 from 5 to 7 +move 21 from 4 to 9 +move 19 from 4 to 5 +move 3 from 4 to 3 +move 2 from 7 to 5 +move 1 from 8 to 2 +move 1 from 6 to 2 +move 1 from 8 to 9 +move 1 from 6 to 7 +move 1 from 2 to 4 +move 1 from 4 to 7 +move 1 from 2 to 7 +move 1 from 7 to 1 +move 1 from 1 to 6 +move 1 from 3 to 5 +move 2 from 6 to 3 +move 13 from 5 to 8 +move 1 from 4 to 2 +move 3 from 5 to 4 +move 5 from 5 to 4 +move 5 from 8 to 9 +move 9 from 9 to 3 +move 2 from 7 to 1 +move 6 from 4 to 2 +move 8 from 9 to 4 +move 1 from 2 to 7 +move 12 from 9 to 8 +move 1 from 4 to 2 +move 3 from 7 to 3 +move 11 from 8 to 5 +move 5 from 8 to 6 +move 3 from 6 to 5 +move 2 from 4 to 1 +move 13 from 5 to 3 +move 1 from 1 to 7 +move 2 from 1 to 8 +move 3 from 4 to 9 +move 1 from 1 to 7 +move 1 from 2 to 4 +move 2 from 7 to 3 +move 1 from 5 to 3 +move 4 from 4 to 2 +move 1 from 4 to 9 +move 30 from 3 to 2 +move 1 from 9 to 7 +move 6 from 8 to 6 +move 1 from 7 to 6 +move 1 from 5 to 1 +move 1 from 3 to 5 +move 30 from 2 to 3 +move 1 from 1 to 9 +move 2 from 9 to 2 +move 9 from 6 to 9 +move 2 from 2 to 9 +move 1 from 5 to 1 +move 5 from 9 to 7 +move 8 from 2 to 5 +move 1 from 1 to 9 +move 3 from 9 to 1 +move 5 from 3 to 6 +move 8 from 5 to 9 +move 13 from 3 to 9 +move 3 from 1 to 7 +move 5 from 7 to 9 +move 17 from 9 to 6 +move 1 from 7 to 6 +move 6 from 3 to 9 +move 1 from 2 to 1 +move 2 from 7 to 1 +move 1 from 2 to 5 +move 21 from 9 to 2 +move 4 from 3 to 6 +move 6 from 6 to 5 +move 7 from 5 to 9 +move 2 from 3 to 8 +move 3 from 1 to 3 +move 4 from 6 to 5 +move 1 from 8 to 1 +move 1 from 8 to 2 +move 4 from 5 to 2 +move 4 from 9 to 1 +move 4 from 3 to 5 +move 2 from 1 to 7 +move 1 from 7 to 4 +move 3 from 9 to 5 +move 25 from 2 to 9 +move 18 from 9 to 1 +move 1 from 4 to 5 +move 1 from 3 to 8 +move 4 from 5 to 6 +move 2 from 9 to 3 +move 17 from 1 to 5 +move 1 from 2 to 7 +move 2 from 3 to 5 +move 3 from 1 to 8 +move 5 from 9 to 2 +move 4 from 8 to 9 +move 12 from 5 to 2 +move 1 from 1 to 8 +move 3 from 9 to 5 +move 1 from 8 to 2 +move 2 from 7 to 2 +move 1 from 9 to 5 +move 9 from 5 to 2 +move 6 from 6 to 2 +move 15 from 6 to 2 +move 5 from 5 to 9 +move 1 from 5 to 9 +move 3 from 9 to 2 +move 3 from 9 to 1 +move 1 from 1 to 9 +move 1 from 9 to 1 +move 19 from 2 to 8 +move 2 from 1 to 9 +move 33 from 2 to 6 +move 4 from 6 to 4 +move 1 from 2 to 6 +move 1 from 9 to 8 +move 3 from 4 to 8 +move 18 from 8 to 3 +move 1 from 4 to 9 +move 10 from 3 to 9 +move 1 from 1 to 4 +move 24 from 6 to 3 +move 1 from 4 to 3 +move 2 from 8 to 7 +move 8 from 9 to 3 +move 5 from 6 to 7 +move 35 from 3 to 2 +move 7 from 7 to 1 +move 3 from 1 to 3 +move 33 from 2 to 6 +move 6 from 3 to 7 +move 5 from 7 to 3 +move 1 from 1 to 4 +move 1 from 7 to 8 +move 1 from 4 to 8 +move 1 from 3 to 2 +move 30 from 6 to 5 +move 2 from 1 to 6 +move 5 from 8 to 1 +move 1 from 9 to 2 +move 2 from 6 to 4 +move 4 from 1 to 7 +move 21 from 5 to 8 \ No newline at end of file diff --git a/src/puzzles/day05.ts b/src/puzzles/day05.ts new file mode 100644 index 0000000..ddf29d5 --- /dev/null +++ b/src/puzzles/day05.ts @@ -0,0 +1,78 @@ +import {Solution} from '../types'; + +interface Move { + items: number; + from: number; + to: number; +} + +const getMoves = (movesRaw: string): Move[] => { + const moves: Move[] = movesRaw.split('\n').map((move: string) => { + const matches: string[] | null = move.match(/move (\d+) from (\d+) to (\d+)/); + if (matches) { + return { + items: parseInt(matches[1]), + from: parseInt(matches[2]) - 1, + to: parseInt(matches[3]) - 1 + }; + } else { + return null; + } + } + ).filter((move: Move | null) => move !== null) as Move[]; + + return moves; +} +const getStacks = (stacksRaw: string): Array => { + const stacksData = stacksRaw.split('\n'); + stacksData.pop(); + const rows: Array = []; + if (stacksData) { + for (let i = 0; i < stacksData.length; i++) { + stacksData[i] += ' '; + const rowMatches = stacksData[i].match(/((\[[A-Z]\]\s)|(\s{4}))/g); + if (rowMatches) { + //strip out the spaces and brackets from each item in the row + const row = rowMatches.map((item) => item.replace(/\s|\[|\]/g, '')); + rows.push(row); + } + } + } + + //convert rows to columns + const columns: Array = []; + for (let i = rows.length - 1; i >= 0; i--) { + for (let j = 0; j < rows[i].length; j++) { + if (columns[j]) { + if (rows[i][j] != '') columns[j].push(rows[i][j]); + } + else { + if (rows[i][j] != '') columns[j] = [rows[i][j]]; + } + } + } + + return columns; +} + +export default (dataSet: string): Solution => { + + const data = dataSet.split('\n\n'); + const stacks = getStacks(data[0]); + const moves = getMoves(data[1]); + + moves.forEach((move: Move) => { + const items = stacks[move.from].splice(stacks[move.from].length - move.items, move.items); + //reverse the items so they are in the correct order when added to the new stack + items.reverse(); + //add the items to the new stack + stacks[move.to].push(...items); + }); + + console.log(stacks); + // Get last item of each stack and convert to string + const result = stacks.map((stack: string[]) => stack[stack.length - 1]).join(''); + + return { puzzle1: result }; + +} \ No newline at end of file diff --git a/src/puzzles/index.ts b/src/puzzles/index.ts index 26b5571..72b102b 100644 --- a/src/puzzles/index.ts +++ b/src/puzzles/index.ts @@ -4,10 +4,12 @@ import Day01 from './day01'; import Day02 from './day02'; import Day03 from './day03'; import Day04 from './day04'; +import Day05 from './day05'; export default { day01: Day01, day02: Day02, day03: Day03, - day04: Day04 + day04: Day04, + day05: Day05 } as Puzzles \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 9e8f13e..e9acee4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ export interface Puzzles { } export interface Solution { - puzzle1?: number; + puzzle1?: number | string; puzzle2?: number; }