Advent of Code 2022

Trying out Advent of Code for the first time. Below are my solutions for each of the days I've completed, using Javascript and Node.js.

Day 1

fs = require('fs');

function fileProcess(linesFn) {
  fs.readFile("01.txt", 'utf8', function (err, data) {
    if (err) { return console.log(err) }
    const lines = data.split("\n")
    linesFn(lines)
  })  
}

function a() {
  fileProcess(lines => {
    let max = 0
    let elf = 0
    for (var i=0; i<lines.length; ++i) {
      const l = lines[i]
      if (l == '') {
        if (elf > max) {
          max = elf
        }
        elf = 0
      }
      else {
        elf += parseInt(l)
      }
    }
    console.log("A: " + max)
  })
}

function b() {
  fileProcess(lines => {
    let max = [0, 0, 0]
    let elf = 0
    for (var i=0; i<lines.length; ++i) {
      const l = lines[i]
      if (l == '') {
        for (var j=0; j<max.length; ++j) {
          if (elf > max[j]) {
            max[j] = elf
            elf = 0
            max.sort((a, b) => a - b)
            break
          }
        }
        elf = 0
      }
      else {
        elf += parseInt(l)
      }
    }
    const bb = max.reduce((total, current) => total + current, 0)
    console.log("B: " + bb)
  })
}

a()
b()

Day 2

fs = require('fs');

function fileProcess(linesFn) {
  fs.readFile("02.txt", 'utf8', function (err, data) {
    if (err) { return console.log(err) }
    // console.log(data)
    const lines = data.split("\n")
    linesFn(lines)
  })  
}

function winScore(yourMove, myMove) {
  return myMove == yourMove ? 3 : 
    myMove == 1 && yourMove == 3 ? 6 : 
    myMove == 3 && yourMove == 1 ? 0 : 
    myMove > yourMove ? 6 : 0
}

function a() {
  fileProcess(lines => {
    let myTotal = 0
    for (var i=0; i<lines.length; ++i) {
      const [you, me] = lines[i].split(" ")
      const yourMove = ["A", "B", "C"].indexOf(you) + 1
      const myMove = ["X", "Y", "Z"].indexOf(me) + 1
      myTotal += myMove + winScore(yourMove, myMove)
    }
    
    console.log("A: " + myTotal)
  })
}

function b() {
  fileProcess(lines => {
    let myTotal = 0
    for (var i=0; i<lines.length; ++i) {
      const [you, outcome] = lines[i].split(" ")
      const yourMove = ["A", "B", "C"].indexOf(you) + 1
      let myMove = 0
      switch (outcome) {
        case "X":
          myMove = yourMove == 1 ? 3 : yourMove - 1
          break
        case "Y": // draw
          myMove = yourMove
          break
        default:
          myMove = yourMove == 3 ? 1 : yourMove + 1
          break
      }
      
      myTotal += myMove + winScore(yourMove, myMove)
    }
    
    console.log("B: " + myTotal)
  })
}

a()
b()

Day 3

function fileProcess(linesFn) {
  require('fs').readFile("03.txt", 'utf8', function (err, data) {
    if (err) { return console.log(err) }
    const lines = data.split("\n")
    linesFn(lines)
  })  
}

const priority = (code) => code > 96 ? code - 96 : code - 38

function a() {
  fileProcess(lines => {
    let total = 0
    for (var i=0; i<lines.length; ++i) {
      const line = lines[i]
      const half = line.length / 2
      for (var j=0; j<half; ++j) {
        const item = line.charAt(j)
        if (line.indexOf(item, half) >= 0) {
          total += priority(item.charCodeAt(0))
          break
        }
      }
    }
    
    console.log("A: " + total)
  })
}

function b() {
  fileProcess(lines => {
    let total = 0
    let groupCount = lines.length / 3
    for (var i=0; i<groupCount; ++i) {
      const x = lines[i*3]
      const y = lines[i*3 + 1]
      const z = lines[i*3 + 2]
      for (var j=0; j<x.length; ++j) {
        const item = x.charAt(j)
        if (y.indexOf(item) >= 0 && z.indexOf(item) >= 0) {
          total += priority(item.charCodeAt(0))
          break
        }
      }
    }
    console.log("B: " + total)
  })
}

a()
b()

Day 4

function fileProcess(linesFn) {
  require('fs').readFile("04.txt", 'utf8', function (err, data) {
    if (err) { return console.log(err) }
    linesFn(data.split("\n"))
  })  
}

function parse(line) {
  const [f, g] = line.split(",")
  const k = f.split("-")
  const l = g.split("-")
  return [parseInt(k[0]), parseInt(k[1]), parseInt(l[0]), parseInt(l[1])]
}

const contained = x => (x[0] <= x[2] && x[1] >= x[3]) || (x[2] <= x[0] && x[3] >= x[1])
const overlap = x => contained(x) || 
  (x[0] >= x[2] && x[0] <= x[3]) || 
  (x[1] >= x[2] && x[1] <= x[3])

function a() {
  fileProcess(lines => {
    let total = 0
    for (var i=0; i<lines.length; ++i) {
      const x = parse(lines[i])
      if (contained(x)) {
        total += 1
      }      
    }    
    console.log("A: " + total)
  })
}

function b() {
  fileProcess(lines => {
    let total = 0
    for (var i=0; i<lines.length; ++i) {
      const x = parse(lines[i])
      if (overlap(x)) {
        total += 1
      }      
    }
    console.log("B: " + total)
  })
}

a()
b()