Clean up some solutions.

This commit is contained in:
Danila Fedorin 2020-12-07 23:38:26 -08:00
parent eafb73cfc1
commit af8170a3e6
3 changed files with 28 additions and 75 deletions

16
day6.cr
View File

@ -2,20 +2,12 @@ require "advent"
INPUT = input(2020, 6).split("\n\n") INPUT = input(2020, 6).split("\n\n")
def part1 def part1
input = INPUT.clone INPUT.sum &.chars.uniq!.count(&.ascii_letter?)
answers = input.map do |i|
i.chars.uniq!.count &.ascii_letter?
end
puts answers.sum
end end
def part2 def part2
input = INPUT.clone INPUT.sum &.lines.map(&.chars.to_set).intersect.size
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
part1 puts part1
part2 puts part2

View File

@ -2,18 +2,19 @@ require "advent"
INPUT = input(2020, 7).lines.map(&.chomp).map do |s| INPUT = input(2020, 7).lines.map(&.chomp).map do |s|
data = s.match(/^([a-z ]+) bags contain (.*).$/).not_nil! data = s.match(/^([a-z ]+) bags contain (.*).$/).not_nil!
{data[1], data[2].split(", ").map(&.rchop(" bags").rchop(" bag")) } 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 end
def build_graph(input) def build_graph(input)
gr = Graph(String).new gr = Graph(String).new
seen = Set(String).new seen = Set(String).new
input.each do |i| input.each do |i|
next if i[1] == ["no other"]
seen << i[0] seen << i[0]
i[1].each do |to| i[1].each do |to|
n, _, m = to.partition(" ") n, m = to
n = n.to_i32
gr.add_edge(i[0], m, n) gr.add_edge(i[0], m, n)
end end
end end

74
day8.cr
View File

@ -1,31 +1,7 @@
require "advent" require "advent"
INPUT = input(2020, 8).lines INPUT = input(2020, 8).lines.map do |s|
def part1
acc = 0
pc = 0
visited = Set(Int32).new
input = INPUT.clone.map do |s|
code, int = s.split(" ") code, int = s.split(" ")
{code, int.to_i32} {code, int.to_i32}
end
loop do
if visited.includes? pc
break
end
visited << pc
code, int = input[pc]
case input[pc][0]
when "acc"
acc += int
pc += 1
when "jmp"
pc += int
when "nop"
pc += 1
end
end
end end
def run(prog) def run(prog)
@ -33,50 +9,34 @@ def run(prog)
pc = 0 pc = 0
visited = Set(Int32).new visited = Set(Int32).new
loop do loop do
if visited.includes? pc return {:term, acc} if pc >= prog.size
return false return {:inf, acc} if visited.includes? pc
end
return acc if pc >= prog.size
visited << pc visited << pc
code, int = prog[pc] code, int = prog[pc]
case prog[pc][0] case code
when "acc" when "acc"
acc += int acc += int
pc += 1
when "jmp" when "jmp"
pc += int pc += int - 1
when "nop"
pc += 1
end end
pc += 1
end end
end end
def part1
run(INPUT)[1]
end
def part2 def part2
input = INPUT.clone.map do |s| jnp = INPUT.find_indices { |e| e[0] == "jmp" || e[0] == "nop" }
code, int = s.split(" ")
{code, int.to_i32}
end
jnp = [] of Int32
input.each_with_index do |e, i|
op, int = e
jnp << i if op == "jmp" || op == "nop"
end
jnp.each do |i| jnp.each do |i|
prog = input.clone prog = INPUT.clone
if prog[i][0] == "jmp" op, int = prog[i]
prog[i] = {"nop", prog[i][1]} prog[i] = {op.tr("jmpnop", "nopjmp"), int}
else
prog[i] = {"jmp", prog[i][1]}
end
case i = run(prog) code, acc = run(prog)
when Bool return acc if code == :term
next
when Int32
return i
end
end end
end end