AdventOfCode 2022 Day 25

Part One

5进制的数字之和,不过5进制并非0 ~ 4,而是-2 ~ 2,-代表-1,=代表-2

1=-0-2
12111
2=0=

5进制转10进制

比较简单,直接运算即可。

WITH recursive input AS (
  SELECT row_number() over() as rn, line, length(line) as len
  FROM lance_input
), origin AS (
  SELECT rn as _row, x.idx as _col, x.word as pos
  FROM (
    SELECT row_number() over() as rn, line
    FROM lance_input
  ) t, regexp_split_to_table(line, '') with ordinality as x(word, idx)
), power_result AS (
  SELECT x._row, (CASE WHEN pos = '-' THEN -1 WHEN pos = '=' THEN -2 ELSE pos :: INTEGER END) * power(5, idx) as result
  FROM (
    SELECT x._row, y.len - x._col as idx, pos
    FROM origin x
    JOIN input y
    ON x._row = y._row
  ) t
)
select sum(result) From power_result;

10进制转5进制

10进制转5进制会比较棘手,不过先根据正常的转换算法求出后,再从低到高依次转换即可,按照如下规律:

  • 3转换为1=,即进一位后再减去2
  • 4转换为1-,即进一位后再减去1
  • 012无需转换

由于进位产生的3和4继续按照规律计算即可。

Part Two

完结

发表评论