The Problem Description
You need to paint a wooden fence between your house and your neighbour’s house. You need the area of the fence, in order to determine how much paint you will use.
However, the fence is made out of N non-uniform pieces of wood, and your neighbour believes that they have an artistic flair. In particular, the pieces of wood may be of various widths. The bottom of each piece of wood will be horizontal, both sides will be vertical, but its top may be cut on an angle. Two such pieces of wood are shown right:
Thankfully, the fence has been constructed so that adjacent pieces of wood have the same height on the sides where they touch, which makes the fence more visually appealing.
Input Specification
For this problem we will read from a file and place each line of that file into an array. JavaScript makes that easy (NodeJS has a package and WebJS has a simple function).
Note: read through the Code File Specification section carefully.
The first line of the input will be a positive integer N, where N ≤ 10 000.
The second line of input will containg N+1 space-separated integers describing the left and right heights of each piece of wood.
The third line of input will contain N space-separated integers describing the width of the ith piece of wood.
Code File Specification
Create a new file called ~/js/package.json (this is a set of "rules" for NodeJS)
The contents of this file must be:
{
"type": "module"
}
Create the executable file ~/js/fences.js
The top of the file must be a block comment with your name.
Optionally include the date and a brief description of the program.
/**
* Author: Mr. Squirrel
* Date: Feb. 2025
* Description: Crazy Fences (Sr. CCC 2021)
**/
NEW! The first two lines of code must be:
import { readFileSync } from 'fs';
'use strict';
Now add the file-reading function:
function read_file(path) {
return readFileSync(path, 'UTF8').toString().split('\n');
}
To read a file into an array, just call the read_lines function with a filename / path:
let lines = read_file("test.txt"); // test.txt must be in the same folder
Output Specification
Output the total area of the fence.
Sample Input 1 (in1.txt)
3
2 3 6 2
4 1 1
Output for Sample Input 1
18.5
Explanation of Output for Sample Input 1
See the second image up-right which represents a visual of this input.
Sample Input 2 (in2.txt)
4
6 4 9 7 3
5 2 4 1
Output for Sample Input 2
75
Explanation of Output for Sample Input 2
See the final image up-right which represents a visual of this input.
Contest Input / Outputs
If you think you have this working perfectly, you can test it with the files found in this folder (download them to your Linux Files area into the appropriate folder).