Compare commits
No commits in common. "af8170a3e6fcddfbc052776145d830f96c3e54a8" and "e79b0344fdfe165480715a6c465de396c583112a" have entirely different histories.
af8170a3e6
...
e79b0344fd
16
day6.cr
16
day6.cr
@ -2,12 +2,20 @@ require "advent"
|
|||||||
INPUT = input(2020, 6).split("\n\n")
|
INPUT = input(2020, 6).split("\n\n")
|
||||||
|
|
||||||
def part1
|
def part1
|
||||||
INPUT.sum &.chars.uniq!.count(&.ascii_letter?)
|
input = INPUT.clone
|
||||||
|
answers = input.map do |i|
|
||||||
|
i.chars.uniq!.count &.ascii_letter?
|
||||||
|
end
|
||||||
|
puts answers.sum
|
||||||
end
|
end
|
||||||
|
|
||||||
def part2
|
def part2
|
||||||
INPUT.sum &.lines.map(&.chars.to_set).intersect.size
|
input = INPUT.clone
|
||||||
|
sol = input.map do |i|
|
||||||
|
i.split("\n").reject(&.empty?).map(&.chars.to_set).reduce { |s1, s2| s1 & s2 }
|
||||||
|
end
|
||||||
|
puts(sol.map(&.size).sum)
|
||||||
end
|
end
|
||||||
|
|
||||||
puts part1
|
part1
|
||||||
puts part2
|
part2
|
||||||
|
45
day7.cr
45
day7.cr
@ -1,45 +0,0 @@
|
|||||||
require "advent"
|
|
||||||
|
|
||||||
INPUT = input(2020, 7).lines.map(&.chomp).map do |s|
|
|
||||||
data = s.match(/^([a-z ]+) bags contain (.*).$/).not_nil!
|
|
||||||
contents = data[2].split(", ").compact_map do |s|
|
|
||||||
s.match(/(\d+) (.*) bags?/).try { |m| {m[1].to_i32, m[2]} }
|
|
||||||
end
|
|
||||||
{data[1], contents}
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_graph(input)
|
|
||||||
gr = Graph(String).new
|
|
||||||
seen = Set(String).new
|
|
||||||
input.each do |i|
|
|
||||||
seen << i[0]
|
|
||||||
i[1].each do |to|
|
|
||||||
n, m = to
|
|
||||||
gr.add_edge(i[0], m, n)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
{seen, gr}
|
|
||||||
end
|
|
||||||
|
|
||||||
def part1
|
|
||||||
seen, gr = build_graph(INPUT)
|
|
||||||
seen.count do |s|
|
|
||||||
gr.find_path(s, "shiny gold") && s != "shiny gold"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def count_bags(gr, current)
|
|
||||||
return 1 unless es = gr.@edges[current]?
|
|
||||||
1 + es.sum do |e|
|
|
||||||
t, k = e
|
|
||||||
k * count_bags(gr, t)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def part2
|
|
||||||
seen, gr = build_graph(INPUT)
|
|
||||||
count_bags(gr, "shiny gold") - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
puts part1
|
|
||||||
puts part2
|
|
44
day8.cr
44
day8.cr
@ -1,44 +0,0 @@
|
|||||||
require "advent"
|
|
||||||
INPUT = input(2020, 8).lines.map do |s|
|
|
||||||
code, int = s.split(" ")
|
|
||||||
{code, int.to_i32}
|
|
||||||
end
|
|
||||||
|
|
||||||
def run(prog)
|
|
||||||
acc = 0
|
|
||||||
pc = 0
|
|
||||||
visited = Set(Int32).new
|
|
||||||
loop do
|
|
||||||
return {:term, acc} if pc >= prog.size
|
|
||||||
return {:inf, acc} if visited.includes? pc
|
|
||||||
|
|
||||||
visited << pc
|
|
||||||
code, int = prog[pc]
|
|
||||||
case code
|
|
||||||
when "acc"
|
|
||||||
acc += int
|
|
||||||
when "jmp"
|
|
||||||
pc += int - 1
|
|
||||||
end
|
|
||||||
pc += 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def part1
|
|
||||||
run(INPUT)[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
def part2
|
|
||||||
jnp = INPUT.find_indices { |e| e[0] == "jmp" || e[0] == "nop" }
|
|
||||||
jnp.each do |i|
|
|
||||||
prog = INPUT.clone
|
|
||||||
op, int = prog[i]
|
|
||||||
prog[i] = {op.tr("jmpnop", "nopjmp"), int}
|
|
||||||
|
|
||||||
code, acc = run(prog)
|
|
||||||
return acc if code == :term
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
puts part1
|
|
||||||
puts part2
|
|
Loading…
Reference in New Issue
Block a user