From 429e4c64eb4913625ac2e3a61031e6b0e476fa62 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Wed, 7 Dec 2022 00:56:50 -0500 Subject: [PATCH] day 7 --- .aocrunner.json | 12 ++-- README.md | 12 ++-- src/day07/README.md | 9 +++ src/day07/index.ts | 156 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 src/day07/README.md create mode 100644 src/day07/index.ts diff --git a/.aocrunner.json b/.aocrunner.json index 72d5821..c9df013 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -89,16 +89,16 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "1517599", "attempts": [], - "time": null + "time": 1.210338 }, "part2": { - "solved": false, - "result": null, + "solved": true, + "result": "2481982", "attempts": [], - "time": null + "time": 1.761511 } }, { diff --git a/README.md b/README.md index 0d4d44a..e01e26a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ [![Day](https://badgen.net/badge/04/%E2%98%85%E2%98%85/green)](src/day04) [![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05) [![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06) -![Day](https://badgen.net/badge/07/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07) ![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray) @@ -111,9 +111,9 @@ Both parts: 4.566356ms ``` Day 07 -Time part 1: - -Time part 2: - -Both parts: - +Time part 1: 1.21ms +Time part 2: 1.762ms +Both parts: 2.9718489999999997ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 12/50 -Total time: 20.937ms +Total stars: 14/50 +Total time: 23.909ms ``` diff --git a/src/day07/README.md b/src/day07/README.md new file mode 100644 index 0000000..45f4380 --- /dev/null +++ b/src/day07/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2022 - day 7 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2022/day/7) + +## Notes + +... \ No newline at end of file diff --git a/src/day07/index.ts b/src/day07/index.ts new file mode 100644 index 0000000..6184e8b --- /dev/null +++ b/src/day07/index.ts @@ -0,0 +1,156 @@ +import run from "aocrunner" +import { dir } from "console" + +type file = { name: string; type: "file"; size: number } +type dir = { name: string; type: "dir"; contents: (dir | file)[] } + +const parseInput = (rawInput: string) => { + const lines = rawInput.split("\n") + let tree: dir = { name: "/", type: "dir", contents: [] } + let currentDir = tree + let currentParents: dir[] = [] + for (let n = 0; n < lines.length; n++) { + const [_prompt, command, param] = lines[n].split(" ") as [ + "$", + "cd" | "ls", + string?, + ] + if (command === "cd") { + if (param === "/") { + currentDir = tree + } else if (param === "..") { + // TODO error checking + currentDir = currentParents.pop()! + } else { + // TODO error checking + currentParents.push(currentDir) + currentDir = currentDir.contents.find((x) => x.name === param) as dir + } + } + if (command === "ls") { + while (n < lines.length - 1 && lines[n + 1].charAt(0) !== "$") { + const [a, b] = lines[n + 1].split(" ") + if (a === "dir") { + // it's a directory + currentDir.contents.push({ + name: b, + type: "dir", + contents: [], + }) + } else { + // it's a file + currentDir.contents.push({ + name: b, + type: "file", + size: Number(a), + }) + } + n++ + } + } + } + return tree +} + +const part1 = (rawInput: string) => { + const input = parseInput(rawInput) + let total = 0 + function getSize(x: dir | file): number { + if (x.type === "file") { + return x.size + } else { + const size = x.contents.reduce((acc, cur) => acc + getSize(cur), 0) + if (size <= 100000) { + total += size + } + return size + } + } + getSize(input) + return total +} + +const part2 = (rawInput: string) => { + const input = parseInput(rawInput) + const candidates: number[] = [] + function getSize(x: dir | file): number { + if (x.type === "file") { + return x.size + } else { + const size = x.contents.reduce((acc, cur) => acc + getSize(cur), 0) + candidates.push(size) + return size + } + } + const totalSize = getSize(input) + const unused = 70000000 - totalSize + const needed = 30000000 - unused + return candidates.filter((x) => x >= needed).sort((a, b) => a - b)[0] +} + +run({ + part1: { + tests: [ + { + input: `$ 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`, + expected: 95437, + }, + ], + solution: part1, + }, + part2: { + tests: [ + { + input: `$ 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`, + expected: 24933642, + }, + ], + solution: part2, + }, + trimTestInputs: true, + onlyTests: false, +})