Skip to content

Instantly share code, notes, and snippets.

@tankorsmash
Created November 17, 2024 05:19
Show Gist options
  • Select an option

  • Save tankorsmash/77ca7a6a8f1f02324381941523e08b87 to your computer and use it in GitHub Desktop.

Select an option

Save tankorsmash/77ca7a6a8f1f02324381941523e08b87 to your computer and use it in GitHub Desktop.
data•file.Lines "../../samples/2020/day7.txt"
data•file.Lines "../../samples/2020/day7_alt.txt"
data•file.Lines "../../inputs/2020/day7.txt"
Log ← {
msgval𝕩
•Show msgval
val
}
# remove substring from string
StripSubstring ← {
full_find𝕨(⊢↑⍷)𝕩
sub_range↕≠𝕨
full_range↕≠𝕩
# characters not in the substring
maskfull_range ¬∊⥊ (sub_range+)˘ full_find/full_range
mask/𝕩
}
# find which character is a digit character
IsNonNumericChar ← {
nums'0'+↕10
res>¬⌈˝ nums= 𝕩
}
# •Show IsNonNumericChar <"99i 2 am 55 years old"
MarkAllowed ← {
full_find𝕨(⊢↑⍷)𝕩
sub_range↕≠𝕨
full_range↕≠𝕩
# characters not in the substring
maskfull_range ¬∊⥊ (sub_range+)˘ full_find/full_range
}
ParseAllNumbers ← {
# 1s where a digit is found
mask ← (¬IsNonNumericChar < 𝕩)
# start index and end index of each number within the string
bounds2⥊/0 (∾≠∾˜) mask
# the start index plus its offset
join ← (⊏∾-˜´)˘bounds
# each index in the string a digit is at
idxs ← (<↕(1)+˘)˘join
# pull out each char at each index, and parse the whole number
parsed ←(•ParseFloat 𝕩 )¨idxs
parsed
}
ParseBagNew ← {
# mostly from bqnrate, splits on mask 𝕨
Gather+`(וShow)¬
Split ← ((0¨)/⊢)((⊢-˜Gather))
# run many precleaners on the input
preclean𝕩 StripSubstring´".", " bag", " bags"
mask¬" contain " MarkAllowed preclean
containerraw_children← (mask) Split preclean
comma_mask¬ "," MarkAllowed raw_children
# •Show comma_mask
DropLeadingSpace ← ((' '=⊏)↓⊢)
JoinFuncParseAllNumbers∾(<DropLeadingSpace IsNonNumericChar</⊢)
children ← (JoinFunc DropLeadingSpace)¨ comma_mask Split raw_children
containerchildren
}
# which bags can eventually contain a shiny gold bag?
BreakIntoParentChildPairs ← {
bagsParseBagNew¨ 𝕩
ConcatChildren ← {
containerchildren𝕩
((<container)<)¨ children
}
parent_child_pairs∾⥊˘ (>ConcatChildren)¨ bags
parent_child_pairs
}
Sep ← {
num10
𝕩
•Out ´ ´ ((num/"-")‿(•Repr 𝕩)‿(num/"-"))
}
PartOne ← {
parent_child_pairsBreakIntoParentChildPairs 𝕩
all_found_bags ← ⟨⟩
containing_target ← ⟨⟩
FindBags ← {
target𝕩
bags_containing_target_mask ← (target1)˘ parent_child_pairs
containing_target˘ bags_containing_target_mask/parent_child_pairs
found_new_containers ← (0≠≠ containing_target)
{ found_new_containers ?
{ all_found_bagsall_found_bags containing_target}
; 0
}
containing_target
}
new_bags ← ⟨"shiny gold", "shiny gold"
Massage ← {
foundFindBags 𝕩
found
}
Body ← {
raw_new_bags ← ((<Massage)˘) 𝕩
new_bags({ "shape"‿(𝕩)}⊢⊢) (∾⥊) raw_new_bags
new_bags
}
Condition ← {
𝕩
# •Show "loop check"
res0≠≠ new_bags
res
}
Body_while_ Condition new_bags
all_found_bags
•Show all_found_bags
}
Gather ← {((⊏⊏)(+´1⊏⍉))¨ ((⊐⊏˘)⊔⊢) 𝕩}
null ← @+0
PartTwo ← {
parent_child_pairsBreakIntoParentChildPairs 𝕩
parent_scores ← (null˜ 0)˘ parent_child_pairs
# normalize the no other bags to 0 value
NormalizeNoOther ← {
bag_name‿· ← 𝕩
(("no other")(1𝕩))
? ⟨bag_name, 0"no other"
; 𝕩
}
parent_child_pairsNormalizeNoOther˘ parent_child_pairs
•Show parent_child_pairs
total_contained0
targets ← ⟨"shiny gold"
# •Show (⊐⟜targets⊸⊏)˝ ⍉parent_child_pairs
•Show 1(1⊏⍉parent_child_pairs) ˜(⊢/↕)(⊏⍉parent_child_pairs) targets
vowels"aeiou"
# RemVow ← ((¬ vowels⊸(∊˜))/⊢)
#
# Shorten ← {𝕊word:
# •Show word
# ⍷((32⊸(-˜)⊏)∾((1⊸⊏∾¯1⊸⊏)∘RemVow))word
# }
# •Show ↑‿1⥊((Shorten(' '⊸≠)/⊢)¨ ((+` ' '⊸=)⊔⊢))¨⍷⊏˘ parent_child_pairs
old_targets ← ⟨⟩
total_children1
Iter ← {𝕊target :
old_targetsold_targets(<target)
matches ← (((/(target)¨⊏⍉)(1·⍉⊏)) parent_child_pairs
total_childrentotal_children + (matches)
matches
}
SplitIter ← {
countbag𝕩
count/(bag)
}
targets ↩ {
SplitIter¨ · ´ Iter¨ 𝕩}•_while_(0≠≠) targets
# •Show old_targets
•Show "solution:"‿(1-˜ old_targets)
}
# PartOne data
PartTwo data
@tankorsmash
Copy link
Copy Markdown
Author

tankorsmash commented Nov 17, 2024

https://adventofcode.com/2020/day/7

It doesn't run without the input files but should run otherwise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment