-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.bqn
68 lines (53 loc) · 2.91 KB
/
day2.bqn
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env cbqn
# Written after finishing the "Combinators" tutorial, although not fully
# digesting the end (how do I read +˜⊸+˜´∘⌽, what is ·, for example).
e1 ← ⟨
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green"
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue"
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red"
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red"
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"
⟩
colors ← "red"‿"green"‿"blue"
targets ← 12‿13‿14
# Taken from BQNcrate
# Split list 𝕩 at any sequence of characters in 𝕨
Split ← {𝕨 ((⊢-˜¬×·+`»⊸<)∘∊˜⊔⊢) 𝕩}
# ⊐ index of, ↓ drop N
GameData ← ↓˜⟜(+⟜1∘⊐⟜':')
# Drop the ⟨⟩ at the front, which comes from the space
Records ← (1⊸↓", "⊸Split)¨ ∘ (";"⊸Split)
# I'm sure I could do this better if I knew how to reshape stuff
# ⊏ select, colors after number so +1
RoundNums ← {(2×↕2÷˜≠𝕩) ⊏ 𝕩}
RoundCols ← {(1+ 2×↕2÷˜≠𝕩) ⊏ 𝕩}
# Count the color 𝕨 in a round 𝕩 ⟨ "3" "blue" "4" "red" ⟩
RoundCount ← {•ParseFloat¨ (𝕨⊸≡¨ RoundCols 𝕩) / RoundNums 𝕩}
# Maximum count of color 𝕨 in a game 𝕩 ⟨round, round, ...⟩
GameMax ← {⌈´ ∾ 𝕨⊸RoundCount¨ 𝕩}
ColorCount ← {GameMax⟜𝕩¨ colors}
PossibleGame ← ∧´ targets ≥ ColorCount
Part1 ← {+´ (PossibleGame∘Records∘GameData¨𝕩) / (1+↕ ≠𝕩)}
•Show ⟨"Example Part 1", Part1 e1⟩
Power ← ×´ ColorCount
•Show ⟨"Example Part 2", +´ Power∘Records∘GameData¨ e1⟩
# Thoughts:
# We've got cbqn! How to get https://github.com/x86y/beacon eludes me. It
# mentioned `nix` so I've setup a nix environment via devcontainers. It was only
# when I got that working did I notce that it uses flakes, so I've no idea how
# to integrate it.
# So in the end, welp, I've done the editing in BQNPAD again, taking care not to
# exceed the character limit.
# This time I really struggled after reshaping the data. Reading up to
# combinators took a lot longer to digest than I expected, so without any
# knowledge of how to reshape or index multi dimensional data I was stuck
# resorting to create two functions, `RoundNums` and `RoundCols` to index into
# the parsed data. I'm sure there's a better way to do this, especially given
# how they're literally the same function, just with a different index.
# I did manage to refactor `ColorCount` from calling and joining it twice, but
# it needs to be the other way around in order to do away with the {} and 𝕩.
# There's one tutorial article left but a suspiciously large amount of symbols
# that I've still no idea about aside from seeing them in BQNcrate and the like.
input ← •FLines "inputs/day2.txt"
•Show ⟨"Part 1", Part1 input⟩
•Show ⟨"Part 2", +´ Power∘Records∘GameData¨ input⟩