From 4dffda42ca94611bfb0cca9a895c0e7794b557af Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 10 Dec 2019 14:15:38 -0600 Subject: [PATCH] WIP day 2 --- 2018/3.moon | 7 +++ 2018/common.moon | 2 + 2018/elixir/test/advent_of_code_2018_test.exs | 4 +- 2018/{elixir/inputs/one.txt => input/1.txt} | 0 2018/{elixir/inputs/two.txt => input/2.txt} | 0 2018/{elixir/inputs/three.txt => input/3.txt} | 0 2019/2.moon | 63 +++++++++++++++++++ 2019/common.moon | 18 +----- 2019/input/2.txt | 1 + 2019/intcode.moon | 44 +++++++++++++ lib/shared.moon | 24 +++++++ 11 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 2018/3.moon create mode 100644 2018/common.moon rename 2018/{elixir/inputs/one.txt => input/1.txt} (100%) rename 2018/{elixir/inputs/two.txt => input/2.txt} (100%) rename 2018/{elixir/inputs/three.txt => input/3.txt} (100%) create mode 100644 2019/2.moon create mode 100644 2019/input/2.txt create mode 100644 2019/intcode.moon create mode 100644 lib/shared.moon diff --git a/2018/3.moon b/2018/3.moon new file mode 100644 index 0000000..cf9cde9 --- /dev/null +++ b/2018/3.moon @@ -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 diff --git a/2018/common.moon b/2018/common.moon new file mode 100644 index 0000000..43cd134 --- /dev/null +++ b/2018/common.moon @@ -0,0 +1,2 @@ +package.moonpath = "../lib/?.moon;" .. package.moonpath +require "shared" diff --git a/2018/elixir/test/advent_of_code_2018_test.exs b/2018/elixir/test/advent_of_code_2018_test.exs index 915123b..75413da 100644 --- a/2018/elixir/test/advent_of_code_2018_test.exs +++ b/2018/elixir/test/advent_of_code_2018_test.exs @@ -4,7 +4,7 @@ defmodule AdventOfCode2018Test do @doc "https://adventofcode.com/2018/day/1" test "Day 1" do input_stream = - File.stream!("./inputs/one.txt", [:read]) + File.stream!("../inputs/1.txt", [:read]) |> Stream.map(&String.trim_trailing/1) |> Stream.map(&String.to_integer/1) @@ -14,7 +14,7 @@ defmodule AdventOfCode2018Test do @doc "https://adventofcode.com/2018/day/2" 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.part2(input_stream) == "bvnfawcnyoeyudzrpgslimtkj" diff --git a/2018/elixir/inputs/one.txt b/2018/input/1.txt similarity index 100% rename from 2018/elixir/inputs/one.txt rename to 2018/input/1.txt diff --git a/2018/elixir/inputs/two.txt b/2018/input/2.txt similarity index 100% rename from 2018/elixir/inputs/two.txt rename to 2018/input/2.txt diff --git a/2018/elixir/inputs/three.txt b/2018/input/3.txt similarity index 100% rename from 2018/elixir/inputs/three.txt rename to 2018/input/3.txt diff --git a/2019/2.moon b/2019/2.moon new file mode 100644 index 0000000..1c6bf2e --- /dev/null +++ b/2019/2.moon @@ -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] diff --git a/2019/common.moon b/2019/common.moon index 76b472e..43cd134 100644 --- a/2019/common.moon +++ b/2019/common.moon @@ -1,16 +1,2 @@ -export input_for_day, reduce - -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 +package.moonpath = "../lib/?.moon;" .. package.moonpath +require "shared" diff --git a/2019/input/2.txt b/2019/input/2.txt new file mode 100644 index 0000000..5b6c9cb --- /dev/null +++ b/2019/input/2.txt @@ -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 diff --git a/2019/intcode.moon b/2019/intcode.moon new file mode 100644 index 0000000..efd76f9 --- /dev/null +++ b/2019/intcode.moon @@ -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 + @ diff --git a/lib/shared.moon b/lib/shared.moon new file mode 100644 index 0000000..a685393 --- /dev/null +++ b/lib/shared.moon @@ -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 +