In the spirit of celebrating holidays with friends near and far, The Rocky Linux Community and Bates College will be hosting a casual weekly Advent of Code meetup via Zoom and the AoC forum thread each Wednesday evenings of December, 4:30-6pm EST. All are welcome to tune in to hang out, exchange esolang solutions, puzzle through technical minutiae and enjoy visiting with a friendly, welcoming cohort.
About the AoC:
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like. - AoC Website
Please share your name, timezone and pronouns if you’d like to participate- synchronously via Zoom otherwise! I am planning on sending participation stickers / pins to anyone who’d like to play ball.
I’ll begin-
My name is Jess Sullivan- I am based in Maine, US (EST timezone). My pronouns are she/her.
I’ve decided to play my AoC this year in TypeScript: repo. Here’s my solution to the first question:
import { readFileSync } from "fs";
let arr_a: number[] = [];
let arr_b: number[] = [];
let result_part_1: number = 0
let result_part_2: number = 0
const arr_f = readFileSync(process.argv.slice(2)[0]).toString().replace( /\n/g, " " ).split(" ");
for (let i = 0; i < arr_f.length-1; i=i+2) {
arr_a.push(Number(arr_f[i]));
arr_b.push(Number(arr_f[i+1]));
}
arr_a.sort(function (a, b){return a-b})
arr_b.sort(function (a, b){return a-b})
for (let i = 0; i < arr_f.length/2; i++) {
let diff = arr_a[i] - arr_b[i]
result_part_1 += diff < 0 ? -diff : diff;
}
console.log('the answer to problem 1, part 1 is: ' + result_part_1)
const reps = {};
arr_b.forEach(function (x) { reps[x] = (reps[x] || 0) + 1; });
for (let i = 0; i < arr_f.length/2; i++) {
result_part_2 += reps[arr_a[i]] !== undefined ? arr_a[i] * reps[arr_a[i]] : 0
}
console.log('the answer to problem 1, part 2 is: ' + result_part_2)