From 079548b237ecf351657684dd053f3e14a55ff0ea Mon Sep 17 00:00:00 2001 From: An0nSaiko Date: Mon, 12 Dec 2022 00:43:39 +0200 Subject: Day 11 --- day10/example.txt | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/input.txt | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/solution.nim | 70 +++++++++++++++++++++++++ day11/input.txt | 55 ++++++++++++++++++++ day11/solution.nim | 0 5 files changed, 415 insertions(+) create mode 100644 day10/example.txt create mode 100644 day10/input.txt create mode 100644 day10/solution.nim create mode 100644 day11/input.txt create mode 100644 day11/solution.nim diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/day10/example.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..77d563b --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,144 @@ +noop +noop +addx 6 +addx -1 +noop +addx 5 +addx 3 +noop +addx 3 +addx -1 +addx -13 +addx 17 +addx 3 +addx 3 +noop +noop +noop +addx 5 +addx 1 +noop +addx 4 +addx 1 +noop +addx -38 +addx 5 +noop +addx 2 +addx 3 +noop +addx 2 +addx 2 +addx 3 +addx -2 +addx 5 +addx 2 +addx -18 +addx 6 +addx 15 +addx 5 +addx 2 +addx -22 +noop +noop +addx 30 +noop +noop +addx -39 +addx 1 +addx 19 +addx -16 +addx 35 +addx -28 +addx -1 +addx 12 +addx -8 +noop +addx 3 +addx 4 +noop +addx -3 +addx 6 +addx 5 +addx 2 +noop +noop +noop +noop +noop +addx 7 +addx -39 +noop +noop +addx 5 +addx 2 +addx 2 +addx -1 +addx 2 +addx 2 +addx 5 +addx 1 +noop +addx 4 +addx -13 +addx 18 +noop +noop +noop +addx 12 +addx -9 +addx 8 +noop +noop +addx -2 +addx -36 +noop +noop +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx 19 +addx -14 +noop +addx 2 +addx 3 +noop +addx -29 +addx 34 +noop +addx -35 +noop +addx -2 +addx 2 +noop +addx 6 +noop +noop +noop +noop +addx 2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 1 +noop +addx 4 +addx -17 +addx 18 +addx 4 +noop +addx 1 +addx 4 +noop +addx 1 +noop +noop diff --git a/day10/solution.nim b/day10/solution.nim new file mode 100644 index 0000000..4277696 --- /dev/null +++ b/day10/solution.nim @@ -0,0 +1,70 @@ +import strutils +import sequtils +import sugar + +type + Instruction = tuple + opcode: string + arg: int + + Program = seq[Instruction] + +func instructionFromString(str: string): Instruction = + let tokens = str.split() + let opcode = tokens[0] + var arg: int + if tokens.len() > 1: + arg = tokens[1].parseInt() + else: + arg = 0 + + (opcode, arg) + +func parseProgram(content: seq[string]): Program = + result = map( + content, + (line) => instructionFromString(line) + ) + +proc checkSignal(cycle: int, x: int): int = + # cycle+1 because cycle is supposed to be starting at 0 + if cycle+1 in [20, 60, 100, 140, 180, 220]: + result = x * (cycle+1) + +var CRT: array[0 .. 5, array[0 .. 39, char]] +proc draw(cycle: int, x: int): void = + let + col = cycle mod 40 + row = cycle div 40 + if col in [x-1, x, x+1]: + CRT[row][col] = '#' + +proc execute(program: Program): int = + var + x = 1 + cycle = 0 + + for i, instr in program: + result += checkSignal(cycle, x) + draw(cycle, x) + case instr.opcode: + of "addx": + result += checkSignal(cycle+1, x) + draw(cycle+1, x) + x += instr.arg + cycle += 2 + of "noop": + cycle += 1 + else: + assert(false) + +let content = readFile("./input.txt").strip().splitLines() +let program = parseProgram(content) + +for row in 0 ..< 6: + for col in 0 ..< 40: + CRT[row][col] = ' ' + +echo execute(program) +for row in CRT: + echo row.join() diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..f6d24aa --- /dev/null +++ b/day11/input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 76, 88, 96, 97, 58, 61, 67 + Operation: new = old * 19 + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 93, 71, 79, 83, 69, 70, 94, 98 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 50, 74, 67, 92, 61, 76 + Operation: new = old * 13 + Test: divisible by 19 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 76, 92 + Operation: new = old + 6 + Test: divisible by 5 + If true: throw to monkey 1 + If false: throw to monkey 6 + +Monkey 4: + Starting items: 74, 94, 55, 87, 62 + Operation: new = old + 5 + Test: divisible by 2 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 59, 62, 53, 62 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 6: + Starting items: 62 + Operation: new = old + 2 + Test: divisible by 17 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 7: + Starting items: 85, 54, 53 + Operation: new = old + 3 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 0 diff --git a/day11/solution.nim b/day11/solution.nim new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3