initial commit
This commit is contained in:
commit
c1e8e3f1c3
14 changed files with 437 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.in
|
||||
*.ex
|
0
day03.s
Normal file
0
day03.s
Normal file
19
day04.py
Normal file
19
day04.py
Normal file
|
@ -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()
|
22
day05.py
Normal file
22
day05.py
Normal file
|
@ -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)
|
83
day06a.cob
Normal file
83
day06a.cob
Normal file
|
@ -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.
|
85
day06b.cob
Normal file
85
day06b.cob
Normal file
|
@ -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.
|
48
day07.py
Normal file
48
day07.py
Normal file
|
@ -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)
|
10
day08.py
Normal file
10
day08.py
Normal file
|
@ -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)
|
23
day09.py
Normal file
23
day09.py
Normal file
|
@ -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()
|
13
day09.rb
Normal file
13
day09.rb
Normal file
|
@ -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
|
||||
|
||||
|
27
day10.py
Normal file
27
day10.py
Normal file
|
@ -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)
|
||||
|
27
day10.rb
Normal file
27
day10.rb
Normal file
|
@ -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)
|
44
day11.py
Normal file
44
day11.py
Normal file
|
@ -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)
|
34
day12.py
Normal file
34
day12.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue