This commit is contained in:
Joshua Seigler 2022-12-07 00:56:50 -05:00
parent 4e68a90792
commit 429e4c64eb
4 changed files with 177 additions and 12 deletions

View file

@ -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
}
},
{

View file

@ -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
```
<!--/RESULTS-->

9
src/day07/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2022 - day 7 🎄
## Info
Task description: [link](https://adventofcode.com/2022/day/7)
## Notes
...

156
src/day07/index.ts Normal file
View file

@ -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,
})