-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_day01_p1.ts
executable file
·70 lines (60 loc) · 2.07 KB
/
aoc_day01_p1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env -S deno run --quiet --allow-read=./
/**
* @file aoc_day01_p1.ts
* @brief Advent of Code (AOC) 2015 Puzzle solution for: Day 01 Part 1.
*
* @author simon rowe <[email protected]>
* @license open-source released under "MIT License"
*
* @date originally created: 14 March 2022
*
* @details Advent of Code (AOC) 2015 Puzzle solution. See: https://adventofcode.com/2015/
*
* @note The program can be run with Deno using the command:
* @code deno run --quiet --allow-read ./aoc_day01_p1.ts
*/
//--------------------------------
// MODULE IMPORTS
//--------------------------------
// import modules to support program:
import { existsFile } from "https://deno.land/x/[email protected]/mod.ts";
//--------------------------------
// MAIN
//--------------------------------
if (import.meta.main) {
//********************************
// APPLICATION CONFIGURATION
//********************************
// define the base location for Deno application directories:
const aocDay = "01";
const aocPart = "01";
// SET INPUT PUZZLE DATA FILE NAME:
// Puzzle data:
const inputFile = `./data/day${aocDay}-input.txt`;
// Puzzle data 'TEST':
//const inputFile = `./data/day${aocDay}-TEST-input.txt`;
//********************************
// Display startup message
console.log(`\nAdvent Of Code 2015 : Day ${aocDay} Part ${aocPart}\n`);
// Ensure the 'puzzle input' file can be found and loaded
if (!await existsFile(inputFile)) {
console.error(
`\nERROR: failed to find the puzzle input file: '${inputFile}'. Exit.\n`,
);
Deno.exit(1);
}
// set up tracking counters
let up = 0;
let down = 0;
let floor = 0;
// Read in the puzzle data
const puzzleData = await Deno.readTextFile(inputFile);
// read puzzle data into lines of text
[...puzzleData].forEach((c) =>
c === "(" ? up++ && floor++ : down++ && floor--
);
console.log(` » Moved up '${up}' floors and down '${down}' floors`);
console.log(` » Floor changes analysed: ${up + down}`);
console.log(` » PUZZLE ANSWER: Final floor reached: ${floor}\n`);
console.log("DONE");
}