Skip to content

Instantly share code, notes, and snippets.

@robinpokorny
Last active April 15, 2025 12:01
Show Gist options
  • Save robinpokorny/f2df2eeb1d3415f2521bd480261c9aaa to your computer and use it in GitHub Desktop.
Save robinpokorny/f2df2eeb1d3415f2521bd480261c9aaa to your computer and use it in GitHub Desktop.

Internationalization Puzzles 2025

Puzzle 1

const textEncoder = new TextEncoder();

input.split('\n').map(line => [textEncoder.encode(line).length <= 160, line.length <= 140]).map(([s, t]) => s ? (t ? 13 : 11) : (t ? 7 : 0)).reduce((a, b) => a + b, 0)

Puzzle 2

Object.entries(Object.groupBy(input.split('\n'), date => new Date(Date.parse(date)).toISOString())).find(([_, arr]) => arr.length >= 4)[0].replace('.000Z', '+00:00')

Puzzle 3

const isValid = (p) =>
  [
    (p) => p.length >= 4 && p.length <= 12,
    (p) => /\d/.test(p),
    (p) => /\p{Uppercase_Letter}/u.test(p),
    (p) => /\p{Lowercase_Letter}/u.test(p),
    (p) => [...p].some((c) => c.charCodeAt(0) > 127),
  ].every((rule) => rule(p));

Puzzle 4

(Using Temporal API)

const parseDateWithTimeZone = (dateString, tz) => {
  const date = new Date(dateString);

  return Temporal.PlainDateTime.from({
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate(),
    hour: date.getHours(),
    minute: date.getMinutes(),
  })
    .toZonedDateTime(tz)
};

const lineRegex = /^.*:\s+(\S+)\s+(.+)$/;

const totalMinutes = input
  .split('\n\n')
  .map(group => 
    group.split('\n')
         .map(line => line.match(lineRegex).slice(1))
         .map(([tz, dateString]) => parseDateWithTimeZone(dateString, tz))
  )
  .map(([departure, arrival]) => departure.until(arrival).total('minutes'))
  .reduce((sum, minutes) => sum + minutes, 0);

Puzzle 7

(Using Temporal API)

const lineRegex = /^(\S+)\s+(\S+)\s+(.+)$/;

const result = input
  .split("\n")
  .map((line) => line.match(lineRegex).slice(1))
  .map(([datetime, correct, wrong]) => {
    let t;
    try {
      t = Temporal.ZonedDateTime.from(`${datetime}[America/Halifax]`, { offset: "reject" });
    } catch (e) {
      t = Temporal.ZonedDateTime.from(`${datetime}[America/Santiago]`);
    }

    return [t, Number.parseInt(correct, 10), Number.parseInt(wrong, 10)];
  })
  .map(([t, correct, wrong]) =>
    t.subtract({ minutes: wrong }).add({ minutes: correct }),
  )
  .reduce((sum, dt, i) => sum + dt.hour * (i + 1), 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment