advent of code day 3: i might be r-slurred edition

idk how to do multi line spoilers lol



letters = ['','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

total_score = 0

team = []

for line in open("input.txt"):
    team.append(line.strip())

for i in range(2, len(team), 3):
    for letter in team[i]:
        if letter in team[i-2] and letter in team[i-1]:
            total_score += letters.index(letter)
            break

print(total_score)

some real caveman shit but it works

80
Jump in the discussion.

No email address required.

alright guys, here's my enterprise grade, production ready, world deployable ts answer. notice how type safe it is.

import * as fs from 'fs'`;

console.log(
  fs.readFileSync(process.argv[2], 'utf-8')
    .split('\n')
    .reduce((pl, l) => {
      let h: any = {};
      let ch = l.split('')
        .reduce((pc,c,i) => i<l.length/2 ? ((h[c]=1)&&0) : (pc||(h[c]?c.charCodeAt(0):0)), 0);
      return pl+ch-(ch<=90?38:96)
    }, 0)
);

PART 2, no declared vars. so let's get crunk

import * as fs from 'fs'`;

console.log(
  fs.readFileSync(process.argv[2], 'utf-8')
    .split('\n')
    .reduce(([pl, h], l, i) => [
      pl + l.split('').reduce((pc,c) => (
        i % 3 === 0 ? ((h[c]=1) &&0) : 
        i % 3 === 1 ? ((h[c]&&=2) &&0) :
        pc || (h[c]===2&&(c.charCodeAt(0)-(c.charCodeAt(0)<=90?38:96)))
      ) || 0, 0),
      i % 3 === 2 ? {} : h
    ], [0, {} as any])
);
Jump in the discussion.

No email address required.

>assigning variables inside a ternary operator inside a reduce function

This is pretty cool.

:#lemindblown:

Jump in the discussion.

No email address required.

is it very cool.

for maximum coolness, make sure to do it whenever and wherever possible.

:marseyokay:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.