Day 07

Posted on Tue 07 December 2021 in aoc2021 • Tagged with js, python

See problem here.

The basic idea is to calculate the total cost for some positions to position m.

function fuel_cost(positions, m, method) {
    var cost = 0;
    for (var c of positions) {
        if (method === "constant") {
            cost += Math.abs(m - c);
        }
        else { // "step"
            cost += Math.abs(m - c) * (Math.abs(m - c …

Continue reading

Day 06

Posted on Mon 06 December 2021 in aoc2021 • Tagged with js, python

See problem here.

My initial approach was to track all of the fish. We decrease number of days for each fish by 1 until it reaches 0. Then we reset it to 6 for existing fish and add a new fish with value 8. This works fine but it is …


Continue reading

Day 05

Posted on Sun 05 December 2021 in aoc2021 • Tagged with js

See problem here.

First let's consider how to parse the input:

function parse_input(pi) {
    var arr = pi.split('\n').map(String);
    var segments = [];
    for (var line of arr) {
        var ab = line.split('->');
        var a = ab[0].split(',');
        var b = ab[1].split(',');
        segments.push({
            "x1": parseInt(a[0]), "y1": parseInt …

Continue reading

Day 04

Posted on Sat 04 December 2021 in aoc2021 • Tagged with js

See problem here.

For part1, we need to do the following:

  • define a function which can parse the order and boards from the puzzle input, i.e. parse_input
  • define a function which marks squares equal to e in bingo board as null, i.e. mark_board
  • define a function which can …

Continue reading

Day 03

Posted on Fri 03 December 2021 in aoc2021 • Tagged with js

See problem here.

For part1, we need to loop through the lines column by column and count the frequency of each digit in the column. If zero is more common, our first string s1 appends a zero. Otherwise, our first string s1 appends a one. Our second string is the …


Continue reading

Day 02

Posted on Thu 02 December 2021 in aoc2021 • Tagged with js

See problem here.

For part1, we need to keep track of horizontal position x and depth y.

function part1(pi) {
    var arr = pi.split('\n').map(String);
    var x = 0;
    var y = 0;
    for(var command of arr) {
        var comp = command.split(" ");
        var direction = comp[0];
        var magnitude = parseInt(comp …

Continue reading

Day 01

Posted on Wed 01 December 2021 in aoc2021 • Tagged with js

See problem here.

For part1, we need to determine the number of entries larger than the previous entry.

function part1(pi) {
    var arr = pi.split('\n').map(Number);
    var ans = 0;
    var pe = null;
    for (var e of arr) {
        if (pe != null && e > pe) ans += 1;
        pe = e;
    }
    console.log …

Continue reading