From 60e8a827266f5282008a168ee73df2aff8966f3c Mon Sep 17 00:00:00 2001 From: Simponic Date: Mon, 12 Dec 2022 00:04:40 -0700 Subject: [PATCH] Day 05 and 06 --- README.md | 9 ++-- day-05/sol.exs | 28 +++++++++++ day-06/package.json | 3 ++ day-06/sol.js | 115 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 day-05/sol.exs create mode 100644 day-06/package.json create mode 100644 day-06/sol.js diff --git a/README.md b/README.md index 3f77a72..8f0a13c 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,16 @@ - [x] C++ - [Day 4](./day-4) - [] Dart -- [] Elixir +- [x] Elixir + - [Day 5](./day-5) - [] Emacs Lisp - [] Haskell - [] Java -- [] JavaScript +- [x] JavaScript + - [Day 6](./day-6) - [] Kotlin - [] PHP -- [] Python +- [x] Python + - [Day 11](./day-11) - [] Ruby - [] TypeScript diff --git a/day-05/sol.exs b/day-05/sol.exs new file mode 100644 index 0000000..e1dab2b --- /dev/null +++ b/day-05/sol.exs @@ -0,0 +1,28 @@ +defmodule Solution do + def solve(input, chunk) do + (String.split(input, "", trim: true) + |> Enum.chunk_every(chunk, 1, :discard) + |> Enum.map(fn window -> + Enum.reduce(window, %{}, fn char, acc -> + Map.put(acc, char, Map.get(acc, char, 0) + 1) + end) + end) + |> Enum.find_index(fn letter_counts -> + Enum.all?(letter_counts, fn {_key, value} -> + value == 1 + end) + end)) + chunk + end + + def main do + input = File.read!("input") + + solve(input, 4) + |> IO.inspect() + + solve(input, 14) + |> IO.inspect() + end +end + +Solution.main() diff --git a/day-06/package.json b/day-06/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/day-06/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/day-06/sol.js b/day-06/sol.js new file mode 100644 index 0000000..28b7b0d --- /dev/null +++ b/day-06/sol.js @@ -0,0 +1,115 @@ +import input from "fs"; + +class File { + constructor(name, size, parent) { + this.name = name; + this.fileSize = size; + this.parent = parent; + } + + size() { + return this.fileSize; + } +} + +class Directory { + constructor(name, parent) { + this.children = []; + this.name = name; + this.parent = parent; + } + + add(child) { + this.children.push(child); + } + + size() { + return this.children.reduce((acc, child) => acc + child.size(), 0); + } + + searchDirectoresOfSizePredicate(sizeP) { + const dirs = []; + for (const child of this.children) { + if (child instanceof Directory) { + if (sizeP(child.size())) { + dirs.push(child); + } + dirs.push(...child.searchDirectoresOfSizePredicate(sizeP)); + } + } + return dirs; + } +} + +class OS { + constructor() { + this.root = new Directory("/", null); + + this.current = this.root; + } + + cd(path) { + if (path === "/") { + this.current = this.root; + } else if (path === ".." && this.current.parent) { + this.current = this.current.parent; + } else { + this.current = this.current.children.find((child) => child.name === path); + } + } + + printFs() { + const print = (dir, depth) => { + for (const child of dir.children) { + console.log(" ".repeat(depth) + child.name); + if (child instanceof Directory) { + print(child, depth + 1); + } + } + }; + + print(this.root, 0); + } +} + +const main = () => { + const os = new OS(); + const lines = input.readFileSync("input", "utf8").split("\n"); + + let currentCommand = null; + for (const line of lines) { + if (line.startsWith("$")) { + const [command, path] = line.split(" ").splice(1); + if (command == "cd") os.cd(path); + currentCommand = command; + } else if (currentCommand == "ls") { + const [dirOrSize, name] = line.split(" "); + if (dirOrSize === "dir") { + const dir = new Directory(name, os.current); + os.current.add(dir); + } else { + const file = new File(name, parseInt(dirOrSize), os.current); + os.current.add(file); + } + } + } + + //os.printFs(); + + console.log( + os.root + .searchDirectoresOfSizePredicate((size) => size <= 100000) + .reduce((a, x) => a + x.size(), 0) + ); + + const rootSize = os.root.size(); + const freeSpace = 70000000 - rootSize; + console.log( + [...os.root.searchDirectoresOfSizePredicate(() => true), os.root] + .map((x) => x.size()) + .filter((x) => freeSpace + x >= 30000000) + .reduce((a, x) => Math.min(a, x), Infinity) + ); +}; + +main();