WIP day 2
This commit is contained in:
parent
7557328316
commit
4dffda42ca
7
2018/3.moon
Normal file
7
2018/3.moon
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
require "common"
|
||||||
|
|
||||||
|
pattern = "#(%d+)%s*@%s*(%d+),(%d+):%s*(%d+)x(%d+)"
|
||||||
|
input = -> input_matcher 3, pattern
|
||||||
|
reduce input!, 0, (n, x, y, w, h) =>
|
||||||
|
print @, n, x, y, w, h
|
||||||
|
0
|
2
2018/common.moon
Normal file
2
2018/common.moon
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
package.moonpath = "../lib/?.moon;" .. package.moonpath
|
||||||
|
require "shared"
|
|
@ -4,7 +4,7 @@ defmodule AdventOfCode2018Test do
|
||||||
@doc "https://adventofcode.com/2018/day/1"
|
@doc "https://adventofcode.com/2018/day/1"
|
||||||
test "Day 1" do
|
test "Day 1" do
|
||||||
input_stream =
|
input_stream =
|
||||||
File.stream!("./inputs/one.txt", [:read])
|
File.stream!("../inputs/1.txt", [:read])
|
||||||
|> Stream.map(&String.trim_trailing/1)
|
|> Stream.map(&String.trim_trailing/1)
|
||||||
|> Stream.map(&String.to_integer/1)
|
|> Stream.map(&String.to_integer/1)
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ defmodule AdventOfCode2018Test do
|
||||||
|
|
||||||
@doc "https://adventofcode.com/2018/day/2"
|
@doc "https://adventofcode.com/2018/day/2"
|
||||||
test "Day 2" do
|
test "Day 2" do
|
||||||
input_stream = File.stream!("./inputs/two.txt", [:read])
|
input_stream = File.stream!("../inputs/2.txt", [:read])
|
||||||
|
|
||||||
assert Two.part1(input_stream) == 6696
|
assert Two.part1(input_stream) == 6696
|
||||||
assert Two.part2(input_stream) == "bvnfawcnyoeyudzrpgslimtkj"
|
assert Two.part2(input_stream) == "bvnfawcnyoeyudzrpgslimtkj"
|
||||||
|
|
63
2019/2.moon
Normal file
63
2019/2.moon
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
-- https://adventofcode.com/2019/day/2
|
||||||
|
|
||||||
|
require "common"
|
||||||
|
|
||||||
|
EXIT = "EXIT"
|
||||||
|
|
||||||
|
-- setup intcode tape
|
||||||
|
build_tape = =>
|
||||||
|
counter = 1
|
||||||
|
tape = {}
|
||||||
|
for n in @\gmatch "%d+"
|
||||||
|
tape[counter] = tonumber n
|
||||||
|
counter += 1
|
||||||
|
tape
|
||||||
|
|
||||||
|
clone_tape = => {k,v for k,v in ipairs @}
|
||||||
|
|
||||||
|
-- gets the value at the index indicated by the value at the given index
|
||||||
|
get_by_ival = (n) => @[@[n] + 1]
|
||||||
|
|
||||||
|
-- sets the value at the index indicated by the value at the given index
|
||||||
|
set_by_ival = (n, v) => @[@[n] + 1] = v
|
||||||
|
|
||||||
|
-- returns a function that applies the given function against the values found
|
||||||
|
-- at the indexes indicated in the next two elements of the tape in the tape and
|
||||||
|
-- stores the result in the index indicated by the value at 3 plus the current
|
||||||
|
-- index
|
||||||
|
-- binary store operation
|
||||||
|
binary_store_op = (f) ->
|
||||||
|
(p) =>
|
||||||
|
result = f get_by_ival(@, p + 1), get_by_ival(@, p + 2)
|
||||||
|
set_by_ival @, p + 3, result
|
||||||
|
result, 4
|
||||||
|
|
||||||
|
run = (ops) =>
|
||||||
|
i = 1
|
||||||
|
while i <= #@
|
||||||
|
print "TAPE"
|
||||||
|
s = ""
|
||||||
|
for i, v in ipairs @
|
||||||
|
s = s .. tostring(v) .. " "
|
||||||
|
print s
|
||||||
|
print "END TAPE"
|
||||||
|
print #@
|
||||||
|
print @[i], @[i + 1], @[i + 2], @[@[i + 1] + 1], @[@[i + 2] + 1], @[i + 3]
|
||||||
|
result, jump = ops[@[i]](@, i)
|
||||||
|
if result == EXIT
|
||||||
|
print EXIT
|
||||||
|
break
|
||||||
|
i += math.max jump, 1
|
||||||
|
@
|
||||||
|
|
||||||
|
ops =
|
||||||
|
[1]: binary_store_op (x, y) -> x + y
|
||||||
|
[2]: binary_store_op (x, y) -> x * y
|
||||||
|
[99]: -> EXIT, 1
|
||||||
|
|
||||||
|
tape = build_tape input_for_day 2
|
||||||
|
-- tape = build_tape "1,9,10,70,2,3,11,0,99,30,40,50"
|
||||||
|
-- tape = build_tape "1,1,1,4,99,5,6,0,99"
|
||||||
|
new_tape = run clone_tape(tape), ops
|
||||||
|
|
||||||
|
print "Day 2, Part 1", new_tape[1]
|
|
@ -1,16 +1,2 @@
|
||||||
export input_for_day, reduce
|
package.moonpath = "../lib/?.moon;" .. package.moonpath
|
||||||
|
require "shared"
|
||||||
read_whole_file = =>
|
|
||||||
f = assert io.open @, "rb"
|
|
||||||
content = f\read "*all"
|
|
||||||
f\close()
|
|
||||||
content
|
|
||||||
|
|
||||||
input_for_day = =>
|
|
||||||
read_whole_file "input/#{@}.txt"
|
|
||||||
|
|
||||||
reduce = (initial, f) =>
|
|
||||||
acc = initial
|
|
||||||
for e in @
|
|
||||||
acc = f acc, e
|
|
||||||
acc
|
|
||||||
|
|
1
2019/input/2.txt
Normal file
1
2019/input/2.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,19,5,23,1,23,9,27,2,27,6,31,1,31,6,35,2,35,9,39,1,6,39,43,2,10,43,47,1,47,9,51,1,51,6,55,1,55,6,59,2,59,10,63,1,6,63,67,2,6,67,71,1,71,5,75,2,13,75,79,1,10,79,83,1,5,83,87,2,87,10,91,1,5,91,95,2,95,6,99,1,99,6,103,2,103,6,107,2,107,9,111,1,111,5,115,1,115,6,119,2,6,119,123,1,5,123,127,1,127,13,131,1,2,131,135,1,135,10,0,99,2,14,0,0
|
44
2019/intcode.moon
Normal file
44
2019/intcode.moon
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
class Intcode
|
||||||
|
from_tape: (tape) =>
|
||||||
|
@tape = tape
|
||||||
|
@ops = {
|
||||||
|
[99]: -> EXIT, 1
|
||||||
|
}
|
||||||
|
|
||||||
|
new: (code) =>
|
||||||
|
counter = 1
|
||||||
|
tape = {}
|
||||||
|
for n in code\gmatch "%d+"
|
||||||
|
tape[counter] = tonumber n
|
||||||
|
counter += 1
|
||||||
|
@from_tape tape
|
||||||
|
|
||||||
|
clone: (target) =>
|
||||||
|
@from_tape {k,v for k,v in ipairs target.tape}
|
||||||
|
|
||||||
|
deref: (n) => @tape[@tape[n] + 1]
|
||||||
|
deref_set: (n, v) => @tape[@tape[n] + 1] = v
|
||||||
|
|
||||||
|
binary_store_op: (f) ->
|
||||||
|
(p) =>
|
||||||
|
result = f get_by_ival(@, p + 1), get_by_ival(@, p + 2)
|
||||||
|
set_by_ival @, p + 3, result
|
||||||
|
result, 4
|
||||||
|
|
||||||
|
run: (ops) =>
|
||||||
|
i = 1
|
||||||
|
while i <= #@tape
|
||||||
|
print "TAPE"
|
||||||
|
s = ""
|
||||||
|
for i, v in ipairs @tape
|
||||||
|
s = s .. tostring(v) .. " "
|
||||||
|
print s
|
||||||
|
print "END TAPE"
|
||||||
|
print #@tape
|
||||||
|
print @tape[i], @tape[i + 1], @tape[i + 2], @tape[@tape[i + 1] + 1], @tape[@tape[i + 2] + 1], @tape[i + 3]
|
||||||
|
result, jump = ops[@tape[i]](@tape, i)
|
||||||
|
if result == EXIT
|
||||||
|
print EXIT
|
||||||
|
break
|
||||||
|
i += math.max jump, 1
|
||||||
|
@
|
24
lib/shared.moon
Normal file
24
lib/shared.moon
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
export input_for_day, input_matcher, reduce
|
||||||
|
|
||||||
|
read_whole_file = =>
|
||||||
|
f = assert io.open @, "rb"
|
||||||
|
content = f\read "*all"
|
||||||
|
f\close()
|
||||||
|
content
|
||||||
|
|
||||||
|
input_for_day = =>
|
||||||
|
read_whole_file "input/#{@}.txt"
|
||||||
|
|
||||||
|
input_matcher = (pattern) =>
|
||||||
|
contents = read_whole_file "input/#{@}.txt"
|
||||||
|
contents\gmatch pattern
|
||||||
|
|
||||||
|
reduce = (initial, f) =>
|
||||||
|
acc = initial
|
||||||
|
for e in @
|
||||||
|
if "table" == type e
|
||||||
|
acc = f acc, unpack e
|
||||||
|
else
|
||||||
|
acc = f acc, e
|
||||||
|
acc
|
||||||
|
|
Loading…
Reference in a new issue