From ede81a630c8f67cad53ce17af86bdf4fc1ff9307 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 22 Oct 2019 00:18:37 +0100 Subject: Day 12, part1 only --- day12/Dockerfile | 7 ++++++ day12/Makefile | 17 ++++++++++++++ day12/entrypoint | 13 +++++++++++ day12/input | 34 ++++++++++++++++++++++++++++ day12/part1 | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day12/part2 | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 209 insertions(+) create mode 100644 day12/Dockerfile create mode 100644 day12/Makefile create mode 100755 day12/entrypoint create mode 100644 day12/input create mode 100755 day12/part1 create mode 100755 day12/part2 (limited to 'day12') diff --git a/day12/Dockerfile b/day12/Dockerfile new file mode 100644 index 0000000..5e0a8b7 --- /dev/null +++ b/day12/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day12/Makefile b/day12/Makefile new file mode 100644 index 0000000..9ab5150 --- /dev/null +++ b/day12/Makefile @@ -0,0 +1,17 @@ +DAY = 12 + +.PHONY: run clean push + +run: build + docker run -it --rm aoc2018day$(DAY) + +build: part* input + docker build -t aoc2018day$(DAY) . + touch build + +clean: + rm -f build + +push: build + docker tag aoc2018day$(DAY) advent.4574.co.uk/aoc2018day$(DAY) + docker push advent.4574.co.uk/aoc2018day$(DAY) diff --git a/day12/entrypoint b/day12/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day12/entrypoint @@ -0,0 +1,13 @@ +#!/bin/bash + +if [ -x part1 ] ; then + echo -ne "Part 1:\n\t" + time ./part1 < input +fi +if [ -x part1 -a -x part2 ] ; then + echo +fi +if [ -x part2 ] ; then + echo -ne "Part 2:\n\t" + time ./part2 < input +fi diff --git a/day12/input b/day12/input new file mode 100644 index 0000000..db5b30b --- /dev/null +++ b/day12/input @@ -0,0 +1,34 @@ +initial state: #....#.#....#....#######..##....###.##....##.#.#.##...##.##.#...#..###....#.#...##.###.##.###...#..# + +#..#. => # +#...# => # +.##.# => # +#.... => . +..#.. => . +#.##. => . +##... => # +##.#. => # +.#.## => # +.#.#. => . +###.. => . +#..## => . +###.# => . +...## => . +#.#.. => # +..... => . +##### => # +..### => . +..#.# => # +....# => . +...#. => # +####. => # +.#... => # +#.#.# => # +.##.. => # +..##. => . +##..# => . +.#..# => # +##.## => # +.#### => . +.###. => # +#.### => . diff --git a/day12/part1 b/day12/part1 new file mode 100755 index 0000000..8d8cbe8 --- /dev/null +++ b/day12/part1 @@ -0,0 +1,69 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map(&:strip) + +is = input[0].split(" ")[2] +rs = input[2..-1] + +class Plants + def initialize(initial_state, rules) + @state = Hash.new(false) + parse_state_string(initial_state) + @rules = [] + parse_rules_lines(rules) + end + + def step(t = 1) + t.times do + newstate = Hash.new(false) + + min = @state.keys.min + max = @state.keys.max + ((min-5)..(max+5)).each do |i| + res = false + @rules.each do |r| + ires = true + (-2..2).each do |o| + if @state[i + o] != r[o + 2] then + ires = false + end + end + if ires then + res = true + end + end + if res then + newstate[i] = true + end + end + + @state = newstate + end + end + + def sum + @state.select { |_, s| s }.keys.sum + end + + private + def parse_state_string(state) + state.chars.each_with_index do |s, i| + @state[i] = s == "#" + end + end + + def parse_rules_lines(rules) + rules.each do |rule| + c, r = rule.split(" => ") + if r == "#" then + @rules << c.chars.map { |p| p == "#" } + end + end + end +end + +m = Plants.new(is, rs) + +m.step(20) + +puts m.sum diff --git a/day12/part2 b/day12/part2 new file mode 100755 index 0000000..e1af0ed --- /dev/null +++ b/day12/part2 @@ -0,0 +1,69 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map(&:strip) + +is = input[0].split(" ")[2] +rs = input[2..-1] + +class Plants + def initialize(initial_state, rules) + @state = Hash.new(false) + parse_state_string(initial_state) + @rules = [] + parse_rules_lines(rules) + end + + def step(t = 1) + t.times do + newstate = Hash.new(false) + + min = @state.keys.min + max = @state.keys.max + ((min-5)..(max+5)).each do |i| + res = false + @rules.each do |r| + ires = true + (-2..2).each do |o| + if @state[i + o] != r[o + 2] then + ires = false + end + end + if ires then + res = true + end + end + if res then + newstate[i] = true + end + end + + @state = newstate + end + end + + def sum + @state.select { |_, s| s }.keys.sum + end + + private + def parse_state_string(state) + state.chars.each_with_index do |s, i| + @state[i] = s == "#" + end + end + + def parse_rules_lines(rules) + rules.each do |rule| + c, r = rule.split(" => ") + if r == "#" then + @rules << c.chars.map { |p| p == "#" } + end + end + end +end + +m = Plants.new(is, rs) + +m.step(50000000000) + +puts m.sum -- cgit v1.2.1