-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.q
28 lines (20 loc) · 743 Bytes
/
16.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/ 16.q
/ Advent of Code 2019
/ Public domain as declared by Sturm Mabie
\l 0.q
input:("J"$) each flip read0 `:16.txt
base:0 1 0 -1
/ generate pattern given a length and index
pattern:{[len; idx] 1_(len+1)#base where 4#idx}
/ calculate a single digit of the new phase
single:{[len; xs; idx]; mod[;10] abs sum pattern[len; idx]*xs}
/ generate the new phase
phase:{[len; xs; tils] single[len; xs;] peach tils}
/ find the 8 digits for phase n
find:{[n; xs] tils:1+til len:count xs; 10 sv 8#n phase[len;;tils]/ xs}
/ find the 8 digits for phase n at the first 7 digit offset
find2:{[n; xs] ys:reverse (10 sv 7#xs) _ (10000*count xs)#xs;
10 sv 8#reverse n {mod[;10] peach sums x}/ ys}
part1 find[100; input]
part2 find2[100; input]
exit 0