Skip to content

Instantly share code, notes, and snippets.

@bennydictor
Created December 7, 2018 15:01
Show Gist options
  • Save bennydictor/87f361b1a9e10e84986da891912e00c5 to your computer and use it in GitHub Desktop.
Save bennydictor/87f361b1a9e10e84986da891912e00c5 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 7 Part 1 in GNU Make
RULES := $(file < input)
RULES := $(subst Step,,$(RULES))
RULES := $(subst must be finished before step,,$(RULES))
RULES := $(subst can begin.,,$(RULES))
RULES := $(strip $(RULES))
TARGETS := $(sort $(RULES))
set_get = $(strip $(__set_$(1)))
define set_append
__set_$(1) := $$(__set_$(1)) $(2)
endef
define set_filterout
__set_$(1) := $$(filter-out $(2),$$(__set_$(1)))
endef
RULES_I := $(RULES)
define loop
ifneq ($(RULES_I),)
CUR := $(wordlist 1,2,$(RULES_I))
RULES_I := $(wordlist 3,1000000,$(RULES_I))
$(eval $(call set_append,$(word 2,$(CUR)),$(word 1,$(CUR))))
$(eval $(value loop))
endif
endef
$(eval $(value loop))
ANSWER :=
define loop
ifneq ($(TARGETS),)
TARGETS_I := $(TARGETS)
define loop1
ifneq ($(TARGETS_I),)
CUR := $(word 1,$(TARGETS_I))
TARGETS_I := $(wordlist 2,1000000,$(TARGETS_I))
ifneq ($(call set_get,$(CUR)),)
$(eval $(value loop1))
endif
endif
endef
$(eval $(value loop1))
ANSWER := $(ANSWER)$(CUR)
TARGETS := $(filter-out $(CUR),$(TARGETS))
$(foreach T,$(TARGETS),$(eval $(call set_filterout,$(T),$(CUR))))
$(eval $(value loop))
endif
endef
$(eval $(value loop))
$(info $(ANSWER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment