Last active
July 8, 2025 17:21
-
-
Save Nostromos/f6c150a8e2e14e3bdb1042a51f00970f to your computer and use it in GitHub Desktop.
AoC 2017 - Day 9 - Mike Monaghan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import loadInput from "@/utils"; | |
const PATH = "2017/Days/9/Input.txt"; // Relative to project root | |
const raw = loadInput(PATH); | |
const input = raw.split("") | |
// ----------------------------------------- | |
// --- Day 9: Stream Processing --- | |
// ----------------------------------------- | |
export function Day9(stream: string[]) { | |
let score = 0; | |
let garbage = false; | |
let depth = 0; | |
let garbageCount = 0; | |
for (let i = 0; i < stream.length; i++) { | |
let cur = stream[i]; | |
if (!garbage) { | |
if (cur == '{') { | |
depth++; | |
} else if (cur == '}') { | |
score += depth | |
depth--; | |
} else if (cur == '<') { | |
garbage = true; | |
} | |
} else { | |
if (cur == '!') { | |
i++; | |
} else if (cur == '>') { | |
garbage = false; | |
} else { | |
garbageCount++; | |
} | |
} | |
} | |
return { | |
score: score, | |
garbage: garbageCount | |
}; | |
} | |
console.log(Day9(input)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import loadInput from "@/utils"; | |
const PATH = "2017/Days/9/Input.txt"; // Relative to project root | |
const raw = loadInput(PATH); | |
// ----------------------------------------- | |
// --- Day 9: Stream Processing --- | |
// ----------------------------------------- | |
export function Day9(stream: string) { | |
let score = 0; | |
let garbage = false; | |
let depth = 0; | |
let garbageCount = 0; | |
for (let i = 0; i < stream.length; i++) { | |
let cur = stream[i]; | |
if (!garbage) { | |
if (cur == '{') { | |
depth++; | |
} else if (cur == '}') { | |
score += depth | |
depth--; | |
} else if (cur == '<') { | |
garbage = true; | |
} | |
} else { | |
if (cur == '!') { | |
i++; | |
} else if (cur == '>') { | |
garbage = false; | |
} else { | |
garbageCount++; | |
} | |
} | |
} | |
return { | |
score: score, | |
garbage: garbageCount | |
}; | |
} | |
console.log(Day9(raw)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment