diff --git a/2015/README.md b/2015/README.md index 643eb3d..5cd7c25 100644 --- a/2015/README.md +++ b/2015/README.md @@ -14,7 +14,7 @@ | ✓ | 10 | Ruby | ✓ | 11 | Ruby | ✓ | 12 | Ruby -| | 13 | +| ✓ | 13 | Ruby | | 14 | | | 15 | | | 16 | diff --git a/2015/day-13/inputs/puzzle.txt b/2015/day-13/inputs/puzzle.txt new file mode 100644 index 0000000..a6f9c66 --- /dev/null +++ b/2015/day-13/inputs/puzzle.txt @@ -0,0 +1,56 @@ +Alice would gain 2 happiness units by sitting next to Bob. +Alice would gain 26 happiness units by sitting next to Carol. +Alice would lose 82 happiness units by sitting next to David. +Alice would lose 75 happiness units by sitting next to Eric. +Alice would gain 42 happiness units by sitting next to Frank. +Alice would gain 38 happiness units by sitting next to George. +Alice would gain 39 happiness units by sitting next to Mallory. +Bob would gain 40 happiness units by sitting next to Alice. +Bob would lose 61 happiness units by sitting next to Carol. +Bob would lose 15 happiness units by sitting next to David. +Bob would gain 63 happiness units by sitting next to Eric. +Bob would gain 41 happiness units by sitting next to Frank. +Bob would gain 30 happiness units by sitting next to George. +Bob would gain 87 happiness units by sitting next to Mallory. +Carol would lose 35 happiness units by sitting next to Alice. +Carol would lose 99 happiness units by sitting next to Bob. +Carol would lose 51 happiness units by sitting next to David. +Carol would gain 95 happiness units by sitting next to Eric. +Carol would gain 90 happiness units by sitting next to Frank. +Carol would lose 16 happiness units by sitting next to George. +Carol would gain 94 happiness units by sitting next to Mallory. +David would gain 36 happiness units by sitting next to Alice. +David would lose 18 happiness units by sitting next to Bob. +David would lose 65 happiness units by sitting next to Carol. +David would lose 18 happiness units by sitting next to Eric. +David would lose 22 happiness units by sitting next to Frank. +David would gain 2 happiness units by sitting next to George. +David would gain 42 happiness units by sitting next to Mallory. +Eric would lose 65 happiness units by sitting next to Alice. +Eric would gain 24 happiness units by sitting next to Bob. +Eric would gain 100 happiness units by sitting next to Carol. +Eric would gain 51 happiness units by sitting next to David. +Eric would gain 21 happiness units by sitting next to Frank. +Eric would gain 55 happiness units by sitting next to George. +Eric would lose 44 happiness units by sitting next to Mallory. +Frank would lose 48 happiness units by sitting next to Alice. +Frank would gain 91 happiness units by sitting next to Bob. +Frank would gain 8 happiness units by sitting next to Carol. +Frank would lose 66 happiness units by sitting next to David. +Frank would gain 97 happiness units by sitting next to Eric. +Frank would lose 9 happiness units by sitting next to George. +Frank would lose 92 happiness units by sitting next to Mallory. +George would lose 44 happiness units by sitting next to Alice. +George would lose 25 happiness units by sitting next to Bob. +George would gain 17 happiness units by sitting next to Carol. +George would gain 92 happiness units by sitting next to David. +George would lose 92 happiness units by sitting next to Eric. +George would gain 18 happiness units by sitting next to Frank. +George would gain 97 happiness units by sitting next to Mallory. +Mallory would gain 92 happiness units by sitting next to Alice. +Mallory would lose 96 happiness units by sitting next to Bob. +Mallory would lose 51 happiness units by sitting next to Carol. +Mallory would lose 81 happiness units by sitting next to David. +Mallory would gain 31 happiness units by sitting next to Eric. +Mallory would lose 73 happiness units by sitting next to Frank. +Mallory would lose 89 happiness units by sitting next to George. diff --git a/2015/day-13/inputs/test.txt b/2015/day-13/inputs/test.txt new file mode 100644 index 0000000..b67c3c6 --- /dev/null +++ b/2015/day-13/inputs/test.txt @@ -0,0 +1,12 @@ +Alice would gain 54 happiness units by sitting next to Bob. +Alice would lose 79 happiness units by sitting next to Carol. +Alice would lose 2 happiness units by sitting next to David. +Bob would gain 83 happiness units by sitting next to Alice. +Bob would lose 7 happiness units by sitting next to Carol. +Bob would lose 63 happiness units by sitting next to David. +Carol would lose 62 happiness units by sitting next to Alice. +Carol would gain 60 happiness units by sitting next to Bob. +Carol would gain 55 happiness units by sitting next to David. +David would gain 46 happiness units by sitting next to Alice. +David would lose 7 happiness units by sitting next to Bob. +David would gain 41 happiness units by sitting next to Carol. diff --git a/2015/day-13/main.rb b/2015/day-13/main.rb new file mode 100755 index 0000000..e8d62f0 --- /dev/null +++ b/2015/day-13/main.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "pathname" +require "set" + +def parse_file(file) + file.each_line.each_with_object({}) do |line, people| + name, _, rest = line.chomp.partition(" would ") + change, neighbor = rest.split(" happiness units by sitting next to ") + change = change.split.then { |v| v.first == "gain" ? v.last.to_i : -v.last.to_i } + people[name] ||= {} + people[name][neighbor.delete_suffix(".")] = change + end +end + +def optimized_seating(people) + people.map do |start| + visited = Set.new + happiness = {} + + name, neighbors = start + start_name = name + + loop do + visited.add(name) + happiness[name] ||= 0 + + nb_name, hn_change = neighbors + .reject { |n, _| visited.include?(n) } + .max { |(an, ac), (bn, bc)| (ac + people[an][name]) <=> (bc + people[bn][name]) } + + nb_name ||= start_name + hn_change ||= neighbors[start_name] + + happiness[nb_name] ||= 0 + happiness[name] += hn_change + happiness[nb_name] += people[nb_name][name] + + name = nb_name + neighbors = people[name] + + break if visited.size == people.size + end + + happiness.values.sum + end +end + +def main(file_paths) + file_paths.each do |file_path| + puts "File: #{file_path}" + people = parse_file(file_path) + + solution1 = optimized_seating(people).max + puts " solution 1: #{solution1}" + + people["Self"] = people.each_with_object({}) { |(n, _), o| o[n] = 0 } + people.each { |(_, nb)| nb["Self"] = 0 } + solution2 = optimized_seating(people).max + puts " solution 2: #{solution2}" + end +end + +main(ARGV.map { |x| Pathname(x) })