From c1e8e3f1c3ddb84d496d59e0f5a00386ae3fe4fd Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sun, 4 Dec 2022 03:42:29 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ day03.s | 0 day04.py | 19 ++++++++++++ day05.py | 22 ++++++++++++++ day06a.cob | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day06b.cob | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day07.py | 48 ++++++++++++++++++++++++++++++ day08.py | 10 +++++++ day09.py | 23 +++++++++++++++ day09.rb | 13 +++++++++ day10.py | 27 +++++++++++++++++ day10.rb | 27 +++++++++++++++++ day11.py | 44 ++++++++++++++++++++++++++++ day12.py | 34 ++++++++++++++++++++++ 14 files changed, 437 insertions(+) create mode 100644 .gitignore create mode 100644 day03.s create mode 100644 day04.py create mode 100644 day05.py create mode 100644 day06a.cob create mode 100644 day06b.cob create mode 100644 day07.py create mode 100644 day08.py create mode 100644 day09.py create mode 100644 day09.rb create mode 100644 day10.py create mode 100644 day10.rb create mode 100644 day11.py create mode 100644 day12.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be85439 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.in +*.ex diff --git a/day03.s b/day03.s new file mode 100644 index 0000000..e69de29 diff --git a/day04.py b/day04.py new file mode 100644 index 0000000..8087724 --- /dev/null +++ b/day04.py @@ -0,0 +1,19 @@ +import hashlib + +def main(): + INPUT = b"yzbqklnj" + DIFFICULTY = 5 + i = 0 + + END = "0" * DIFFICULTY + + while True: + message = INPUT + str(i).encode("ascii") + digest = hashlib.md5(message).hexdigest() + if digest[:DIFFICULTY] == END: + print(i) + break + + i += 1 + +main() diff --git a/day05.py b/day05.py new file mode 100644 index 0000000..fb09b4d --- /dev/null +++ b/day05.py @@ -0,0 +1,22 @@ +import fileinput + +part1 = 0 +part2 = 0 + +for line in fileinput.input(): + if ( + sum(1 for c in line if c in "aeiou") >= 3 + and any(line[i] == line[i + 1] for i in range(len(line) - 1)) + and not ("ab" in line or "cd" in line or "pq" in line or "xy" in line) + ): + part1 += 1 + + if any(line[i] == line[i + 2] for i in range(len(line) - 2)) and any( + line[i : i + 2] == line[j : j + 2] + for i in range(len(line) - 4) + for j in range(i + 2, len(line) - 2) + ): + part2 += 1 + + +print(part1, part2) diff --git a/day06a.cob b/day06a.cob new file mode 100644 index 0000000..0b21428 --- /dev/null +++ b/day06a.cob @@ -0,0 +1,83 @@ + identification division. + program-id. day06. + + data division. + working-storage section. + 01 input-line pic x(100). + + 01 action pic x(10). + 01 coords pic x(100). + 01 start-coords pic x(100). + 01 end-coords pic x(100). + + 01 start-x pic 9(10). + 01 end-x pic 9(10). + 01 start-y pic 9(10). + 01 end-y pic 9(10). + + 01 x pic 9(10). + 01 y pic 9(10). + + 01 temp pic 9. + 01 matrix-table. + 05 occurs 1000 times. + 10 occurs 1000 times. + 15 matrix pic 9 value 0. + + procedure division. + accept input-line. + perform input-loop-para until input-line = SPACE. + + * Count the lit lights + move 0 to x. + inspect matrix-table tallying x for all '1'. + display x. + + stop run. + + input-loop-para. + display input-line. + + if input-line(1:7) = "turn on" then + move "turn on" to action + move input-line(9:) to coords + end-if. + if input-line(1:8) = "turn off" then + move "turn off" to action + move input-line(10:) to coords + end-if. + if input-line(1:7) = "toggle" then + move "toggle" to action + move input-line(8:) to coords + end-if. + + unstring coords delimited by " through " + into start-coords, end-coords. + + unstring start-coords delimited by "," into start-x, start-y + unstring end-coords delimited by "," into end-x, end-y + + move start-y to y. + perform outer-loop-para until y > end-y. + + accept input-line. + + outer-loop-para. + move start-x to x. + perform inner-loop-para until x > end-x. + add 1 to y. + + inner-loop-para. + if action = "turn on" then + move 1 to matrix(y + 1, x + 1). + + if action = "turn off" then + move 0 to matrix(y + 1, x + 1). + + if action = "toggle" then + move 1 to temp + subtract matrix(y + 1, x + 1) from temp + move temp to matrix(y + 1, x + 1) + end-if. + + add 1 to x. diff --git a/day06b.cob b/day06b.cob new file mode 100644 index 0000000..268a5c9 --- /dev/null +++ b/day06b.cob @@ -0,0 +1,85 @@ + identification division. + program-id. day06. + + data division. + working-storage section. + 01 input-line pic x(40). + + 01 action pic x(8). + 01 coords pic x(30). + 01 start-coords pic x(7). + 01 end-coords pic x(7). + + 01 start-x pic 9(3) comp-x. + 01 end-x pic 9(3) comp-x. + 01 start-y pic 9(3) comp-x. + 01 end-y pic 9(3) comp-x. + + 01 x pic 9(4) comp-x. + 01 y pic 9(4) comp-x. + + 01 matrix-table. + 05 occurs 1000 times. + 10 occurs 1000 times. + 15 matrix pic 9(2) comp-x value 0. + + 01 brightness pic 9(9) comp-x. + + procedure division. + accept input-line. + perform input-loop-para until input-line = SPACE. + + move 1 to y. + perform count-outer-para until y > 1000. + display brightness. + + stop run. + + input-loop-para. + if input-line(1:7) = "turn on" then + move "turn on" to action + move input-line(9:) to coords + else if input-line(1:8) = "turn off" then + move "turn off" to action + move input-line(10:) to coords + else if input-line(1:7) = "toggle" then + move "toggle" to action + move input-line(8:) to coords + end-if. + + unstring coords delimited by " through " + into start-coords, end-coords. + + unstring start-coords delimited by "," into start-x, start-y + unstring end-coords delimited by "," into end-x, end-y + + move start-y to y. + perform outer-loop-para until y > end-y. + + accept input-line. + + outer-loop-para. + add 1 to y. + + move start-x to x. + perform inner-loop-para until x > end-x. + + inner-loop-para. + add 1 to x. + + if action = "turn on" then + add 1 to matrix(y, x) + else if action = "turn off" and matrix(y, x) > 0 then + subtract 1 from matrix(y, x) + else if action = "toggle" then + add 2 to matrix(y, x) + end-if. + + count-outer-para. + move 1 to x. + perform count-inner-para until x > 1000. + add 1 to y. + + count-inner-para. + add matrix(y, x) to brightness. + add 1 to x. diff --git a/day07.py b/day07.py new file mode 100644 index 0000000..fa70070 --- /dev/null +++ b/day07.py @@ -0,0 +1,48 @@ +import fileinput +from collections import deque + + +input = [] +for line in fileinput.input(): + expression, wire = line.strip().split(" -> ") + input.append((expression.split(), wire)) + + +def simulate(wires): + def get(signal): + if signal.isdigit(): + return int(signal) + else: + return wires[signal] + + queue = deque(input) + while queue: + expression, wire = queue.popleft() + if wire in wires: + continue + + try: + match expression: + case [signal]: + wires[wire] = get(signal) + case ["NOT", signal]: + wires[wire] = ~get(signal) + case [left, "AND", right]: + wires[wire] = get(left) & get(right) + case [left, "OR", right]: + wires[wire] = get(left) | get(right) + case [left, "LSHIFT", right]: + wires[wire] = get(left) << get(right) + case [left, "RSHIFT", right]: + wires[wire] = get(left) >> get(right) + except KeyError: + queue.append((expression, wire)) + + return wires["a"] + + +part1 = simulate({}) +print("part 1:", part1) + +part2 = simulate({"b": part1}) +print("part 2:", part2) diff --git a/day08.py b/day08.py new file mode 100644 index 0000000..f2f639e --- /dev/null +++ b/day08.py @@ -0,0 +1,10 @@ +import fileinput + +part1, part2 = 0, 0 + +for line in fileinput.input(): + line = line.strip() + part1 += len(line) - len(eval(line)) + part2 += line.count("\\") + line.count('"') + 2 + +print(part1, part2) diff --git a/day09.py b/day09.py new file mode 100644 index 0000000..a71330e --- /dev/null +++ b/day09.py @@ -0,0 +1,23 @@ +import fileinput +from itertools import permutations + + +def main(): + graph = {} + cities = set() + + for line in fileinput.input(): + route, distance = line.split(" = ", 1) + source, dest = route.split(" to ", 1) + graph[(source, dest)] = graph[(dest, source)] = int(distance) + cities.add(source) + cities.add(dest) + + lengths = [ + sum(graph[(route[i], route[i + 1])] for i in range(len(route) - 1)) + for route in permutations(cities) + ] + + print(min(lengths), max(lengths)) + +main() diff --git a/day09.rb b/day09.rb new file mode 100644 index 0000000..1bc408e --- /dev/null +++ b/day09.rb @@ -0,0 +1,13 @@ +lines = ARGF.read.split "\n" +graph = Hash.new { |h, k| h[k] = [] } + +# parse input and build graph +lines.each do |line| + line.match /(\w+) to (\w+) = (\d+)/ do |m| + graph[m[1]] << [m[2], m[3].to_i] + end +end + +puts graph.inspect + + diff --git a/day10.py b/day10.py new file mode 100644 index 0000000..6f37d21 --- /dev/null +++ b/day10.py @@ -0,0 +1,27 @@ +import fileinput + +input = [int(i) for i in next(fileinput.input()).strip()] + +def solve(n): + global input + + for _ in range(n): + new_input = [] + current = input[0] + count = 0 + + for c in input: + if c == current: + count += 1 + else: + new_input.extend((count, current)) + current, count = c, 1 + + new_input.extend((count, current)) + input = new_input + + print(len(input)) + +solve(40) +solve(10) + diff --git a/day10.rb b/day10.rb new file mode 100644 index 0000000..38d8856 --- /dev/null +++ b/day10.rb @@ -0,0 +1,27 @@ +$input = ARGF.read.chomp.chars.map { |c| c.to_i } + +def solve(n) + n.times do + new_input = [] + current = $input[0] + count = 0 + + $input.each do |c| + if c == current then + count += 1 + else + new_input << count << current + current = c + count = 1 + end + end + + new_input << count << current + $input = new_input + end + + puts $input.size +end + +solve(40) +solve(10) diff --git a/day11.py b/day11.py new file mode 100644 index 0000000..40dd429 --- /dev/null +++ b/day11.py @@ -0,0 +1,44 @@ +input = "cqjxjnds" + +pw = [ord(c) - ord("a") for c in input] +pw[-1] += 1 + +part1, part2 = None, None + +while not part2: + # increment pw + pw[-1] += 1 + for i in range(len(pw) - 1, 0, -1): + if pw[i] >= 26: + pw[i - 1] += 1 + pw[i] = 0 + else: + break + + if 8 in pw: + pw[pw.index(8)] += 1 + if 11 in pw: + pw[pw.index(11)] += 1 + if 14 in pw: + pw[pw.index(14)] += 1 + + for i in range(len(pw) - 2): + if pw[i] + 2 == pw[i + 1] + 1 == pw[i + 2]: + break + else: + continue + + pairs = 0 + for i in range(len(pw) - 1): + c = pw[i] + if c == pw[i + 1] and (i == 0 or c != pw[i - 1]): + pairs += 1 + if pairs < 2: + continue + + if not part1: + part1 = "".join(chr(c + ord("a")) for c in pw) + else: + part2 = "".join(chr(c + ord("a")) for c in pw) + +print(part1, part2) diff --git a/day12.py b/day12.py new file mode 100644 index 0000000..1d1874d --- /dev/null +++ b/day12.py @@ -0,0 +1,34 @@ +import sys +import json +from pprint import pprint +import re +from typing import Any + +doc = json.load(sys.stdin) +part1, part2 = 0, 0 + +def iter(x): + if type(x) is list: + return list, list(enumerate(x)) + else: + return dict, list(x.items()) + +stack: Any = [(type(doc) is dict and "red" in doc.values(), doc)] +while stack: + red, x = stack.pop() + + if type(x) is dict: + red = red or "red" in x.values() + for v in x.values(): + stack.append((red, v)) + elif type(x) is list: + for v in x: + stack.append((red, v)) + elif type(x) is int: + part1 += x + if not red: + part2 += x + + + +print(part1, part2)