Skip to content

Instantly share code, notes, and snippets.

@cyppan
Created December 19, 2023 15:26
Show Gist options
  • Save cyppan/60e582b966666dd4ca859beae414416d to your computer and use it in GitHub Desktop.
Save cyppan/60e582b966666dd4ca859beae414416d to your computer and use it in GitHub Desktop.
DAY 1
in⎕nget'01.txt'1
digitsStr'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine'
mask{(9)+.×()¨}
part1+/{10(,¯1)( mask '123456789')~0}¨in
part2+/{10(,¯1)(( mask '123456789') mask digitsStr)~0}¨in
DAY 2
in⎕nget'02.txt'1
extractMax{/{10¯1+(11|('0123456789'))~0}¨{(/¨()¨matches)/matches((31)+.×';' ':' ','(.))}}
day2part1+/(in)+.×{(14'blue' extractMax ) (13'green' extractMax ) (12'red' extractMax )}¨in
day2part2+/{('blue' extractMax ) × ('green' extractMax ) × ('red' extractMax )}¨in
DAY 3
in⎕nget'03.txt'1
safeDigitsMask,/(0,)¨(⎕D)¨in prepend rows with 0 before partitioning groups
assign unique ids (incremented integer) to number groups
groups(1)¨(0 1+in)safeDigitsMask×0,+\2</safeDigitsMask
digitsMask(1)¨(0 1+in)safeDigitsMask
numbers(,digitsMask),in
groupsPerCell{numbers[0~,]}3 3groups windowing with a 3x3 wrapping matrix
part1+/{+/¨}¨(~(,in)⎕D,'.')/,groupsPerCell
part2+/{×/¨}¨(((,in)'*')(2=¨,groupsPerCell))/,groupsPerCell
DAY 4
in⎕nget'04.txt'1
To{+¯1+1+-}
pairs((¨' ' ())¨)¨('|'()9)¨in
gains ex: 4 2 2 1 0 0 - first card 4 gains, second 2, ...
gains¨{winning deck (deckwinning)/deck}¨pairs
part1+/gains
m ex:
0 1 1 1 1 0
0 0 1 1 0 0
0 0 0 1 1 0
0 0 0 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
m{1(+@((+1)To +gains[]))(gains)0}¨(gains)
i(gains)1 identity matrix
(i+m+.×⊢)⍣≡i expands to i + m + (m+.×m) + (m+.×m+.×m) + ... until it stabilizes
part2+/(i+m+.×)i
multiply matrix rows by a vector =>
1 2 3 4 5 6 (×0 1) {1(+@((+1)To +part2[]))60}¨6
DAY 5 (unfinished)
in⎕nget'05.txt'1
FindIndex{(d s r) {¯1 (d+-s)[1+(>s)(<s+r)]}}
{(d s r) {¯1 (d+-s)[1+(>s)(<s+r)]}}¨¨{¨' '()}¨¨(2)¨(('')¨in)in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment