diff options
| author | An0nSaiko <porfeas12@gmail.com> | 2022-12-12 00:43:39 +0200 |
|---|---|---|
| committer | An0nSaiko <porfeas12@gmail.com> | 2022-12-12 00:43:39 +0200 |
| commit | 079548b237ecf351657684dd053f3e14a55ff0ea (patch) | |
| tree | 7d07c919f7e33f348c453ef5e38aec47cf999744 | |
| parent | Day 9 (diff) | |
| download | aoc22-079548b237ecf351657684dd053f3e14a55ff0ea.tar.gz aoc22-079548b237ecf351657684dd053f3e14a55ff0ea.zip | |
Day 11
| -rw-r--r-- | day10/example.txt | 146 | ||||
| -rw-r--r-- | day10/input.txt | 144 | ||||
| -rw-r--r-- | day10/solution.nim | 70 | ||||
| -rw-r--r-- | day11/input.txt | 55 | ||||
| -rw-r--r-- | day11/solution.nim | 0 |
5 files changed, 415 insertions, 0 deletions
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 @@ | |||
| 1 | addx 15 | ||
| 2 | addx -11 | ||
| 3 | addx 6 | ||
| 4 | addx -3 | ||
| 5 | addx 5 | ||
| 6 | addx -1 | ||
| 7 | addx -8 | ||
| 8 | addx 13 | ||
| 9 | addx 4 | ||
| 10 | noop | ||
| 11 | addx -1 | ||
| 12 | addx 5 | ||
| 13 | addx -1 | ||
| 14 | addx 5 | ||
| 15 | addx -1 | ||
| 16 | addx 5 | ||
| 17 | addx -1 | ||
| 18 | addx 5 | ||
| 19 | addx -1 | ||
| 20 | addx -35 | ||
| 21 | addx 1 | ||
| 22 | addx 24 | ||
| 23 | addx -19 | ||
| 24 | addx 1 | ||
| 25 | addx 16 | ||
| 26 | addx -11 | ||
| 27 | noop | ||
| 28 | noop | ||
| 29 | addx 21 | ||
| 30 | addx -15 | ||
| 31 | noop | ||
| 32 | noop | ||
| 33 | addx -3 | ||
| 34 | addx 9 | ||
| 35 | addx 1 | ||
| 36 | addx -3 | ||
| 37 | addx 8 | ||
| 38 | addx 1 | ||
| 39 | addx 5 | ||
| 40 | noop | ||
| 41 | noop | ||
| 42 | noop | ||
| 43 | noop | ||
| 44 | noop | ||
| 45 | addx -36 | ||
| 46 | noop | ||
| 47 | addx 1 | ||
| 48 | addx 7 | ||
| 49 | noop | ||
| 50 | noop | ||
| 51 | noop | ||
| 52 | addx 2 | ||
| 53 | addx 6 | ||
| 54 | noop | ||
| 55 | noop | ||
| 56 | noop | ||
| 57 | noop | ||
| 58 | noop | ||
| 59 | addx 1 | ||
| 60 | noop | ||
| 61 | noop | ||
| 62 | addx 7 | ||
| 63 | addx 1 | ||
| 64 | noop | ||
| 65 | addx -13 | ||
| 66 | addx 13 | ||
| 67 | addx 7 | ||
| 68 | noop | ||
| 69 | addx 1 | ||
| 70 | addx -33 | ||
| 71 | noop | ||
| 72 | noop | ||
| 73 | noop | ||
| 74 | addx 2 | ||
| 75 | noop | ||
| 76 | noop | ||
| 77 | noop | ||
| 78 | addx 8 | ||
| 79 | noop | ||
| 80 | addx -1 | ||
| 81 | addx 2 | ||
| 82 | addx 1 | ||
| 83 | noop | ||
| 84 | addx 17 | ||
| 85 | addx -9 | ||
| 86 | addx 1 | ||
| 87 | addx 1 | ||
| 88 | addx -3 | ||
| 89 | addx 11 | ||
| 90 | noop | ||
| 91 | noop | ||
| 92 | addx 1 | ||
| 93 | noop | ||
| 94 | addx 1 | ||
| 95 | noop | ||
| 96 | noop | ||
| 97 | addx -13 | ||
| 98 | addx -19 | ||
| 99 | addx 1 | ||
| 100 | addx 3 | ||
| 101 | addx 26 | ||
| 102 | addx -30 | ||
| 103 | addx 12 | ||
| 104 | addx -1 | ||
| 105 | addx 3 | ||
| 106 | addx 1 | ||
| 107 | noop | ||
| 108 | noop | ||
| 109 | noop | ||
| 110 | addx -9 | ||
| 111 | addx 18 | ||
| 112 | addx 1 | ||
| 113 | addx 2 | ||
| 114 | noop | ||
| 115 | noop | ||
| 116 | addx 9 | ||
| 117 | noop | ||
| 118 | noop | ||
| 119 | noop | ||
| 120 | addx -1 | ||
| 121 | addx 2 | ||
| 122 | addx -37 | ||
| 123 | addx 1 | ||
| 124 | addx 3 | ||
| 125 | noop | ||
| 126 | addx 15 | ||
| 127 | addx -21 | ||
| 128 | addx 22 | ||
| 129 | addx -6 | ||
| 130 | addx 1 | ||
| 131 | noop | ||
| 132 | addx 2 | ||
| 133 | addx 1 | ||
| 134 | noop | ||
| 135 | addx -10 | ||
| 136 | noop | ||
| 137 | noop | ||
| 138 | addx 20 | ||
| 139 | addx 1 | ||
| 140 | addx 2 | ||
| 141 | addx 2 | ||
| 142 | addx -6 | ||
| 143 | addx -11 | ||
| 144 | noop | ||
| 145 | noop | ||
| 146 | 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 @@ | |||
| 1 | noop | ||
| 2 | noop | ||
| 3 | addx 6 | ||
| 4 | addx -1 | ||
| 5 | noop | ||
| 6 | addx 5 | ||
| 7 | addx 3 | ||
| 8 | noop | ||
| 9 | addx 3 | ||
| 10 | addx -1 | ||
| 11 | addx -13 | ||
| 12 | addx 17 | ||
| 13 | addx 3 | ||
| 14 | addx 3 | ||
| 15 | noop | ||
| 16 | noop | ||
| 17 | noop | ||
| 18 | addx 5 | ||
| 19 | addx 1 | ||
| 20 | noop | ||
| 21 | addx 4 | ||
| 22 | addx 1 | ||
| 23 | noop | ||
| 24 | addx -38 | ||
| 25 | addx 5 | ||
| 26 | noop | ||
| 27 | addx 2 | ||
| 28 | addx 3 | ||
| 29 | noop | ||
| 30 | addx 2 | ||
| 31 | addx 2 | ||
| 32 | addx 3 | ||
| 33 | addx -2 | ||
| 34 | addx 5 | ||
| 35 | addx 2 | ||
| 36 | addx -18 | ||
| 37 | addx 6 | ||
| 38 | addx 15 | ||
| 39 | addx 5 | ||
| 40 | addx 2 | ||
| 41 | addx -22 | ||
| 42 | noop | ||
| 43 | noop | ||
| 44 | addx 30 | ||
| 45 | noop | ||
| 46 | noop | ||
| 47 | addx -39 | ||
| 48 | addx 1 | ||
| 49 | addx 19 | ||
| 50 | addx -16 | ||
| 51 | addx 35 | ||
| 52 | addx -28 | ||
| 53 | addx -1 | ||
| 54 | addx 12 | ||
| 55 | addx -8 | ||
| 56 | noop | ||
| 57 | addx 3 | ||
| 58 | addx 4 | ||
| 59 | noop | ||
| 60 | addx -3 | ||
| 61 | addx 6 | ||
| 62 | addx 5 | ||
| 63 | addx 2 | ||
| 64 | noop | ||
| 65 | noop | ||
| 66 | noop | ||
| 67 | noop | ||
| 68 | noop | ||
| 69 | addx 7 | ||
| 70 | addx -39 | ||
| 71 | noop | ||
| 72 | noop | ||
| 73 | addx 5 | ||
| 74 | addx 2 | ||
| 75 | addx 2 | ||
| 76 | addx -1 | ||
| 77 | addx 2 | ||
| 78 | addx 2 | ||
| 79 | addx 5 | ||
| 80 | addx 1 | ||
| 81 | noop | ||
| 82 | addx 4 | ||
| 83 | addx -13 | ||
| 84 | addx 18 | ||
| 85 | noop | ||
| 86 | noop | ||
| 87 | noop | ||
| 88 | addx 12 | ||
| 89 | addx -9 | ||
| 90 | addx 8 | ||
| 91 | noop | ||
| 92 | noop | ||
| 93 | addx -2 | ||
| 94 | addx -36 | ||
| 95 | noop | ||
| 96 | noop | ||
| 97 | addx 5 | ||
| 98 | addx 2 | ||
| 99 | addx 3 | ||
| 100 | addx -2 | ||
| 101 | addx 2 | ||
| 102 | addx 2 | ||
| 103 | noop | ||
| 104 | addx 3 | ||
| 105 | addx 5 | ||
| 106 | addx 2 | ||
| 107 | addx 19 | ||
| 108 | addx -14 | ||
| 109 | noop | ||
| 110 | addx 2 | ||
| 111 | addx 3 | ||
| 112 | noop | ||
| 113 | addx -29 | ||
| 114 | addx 34 | ||
| 115 | noop | ||
| 116 | addx -35 | ||
| 117 | noop | ||
| 118 | addx -2 | ||
| 119 | addx 2 | ||
| 120 | noop | ||
| 121 | addx 6 | ||
| 122 | noop | ||
| 123 | noop | ||
| 124 | noop | ||
| 125 | noop | ||
| 126 | addx 2 | ||
| 127 | noop | ||
| 128 | addx 3 | ||
| 129 | addx 2 | ||
| 130 | addx 5 | ||
| 131 | addx 2 | ||
| 132 | addx 1 | ||
| 133 | noop | ||
| 134 | addx 4 | ||
| 135 | addx -17 | ||
| 136 | addx 18 | ||
| 137 | addx 4 | ||
| 138 | noop | ||
| 139 | addx 1 | ||
| 140 | addx 4 | ||
| 141 | noop | ||
| 142 | addx 1 | ||
| 143 | noop | ||
| 144 | 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 @@ | |||
| 1 | import strutils | ||
| 2 | import sequtils | ||
| 3 | import sugar | ||
| 4 | |||
| 5 | type | ||
| 6 | Instruction = tuple | ||
| 7 | opcode: string | ||
| 8 | arg: int | ||
| 9 | |||
| 10 | Program = seq[Instruction] | ||
| 11 | |||
| 12 | func instructionFromString(str: string): Instruction = | ||
| 13 | let tokens = str.split() | ||
| 14 | let opcode = tokens[0] | ||
| 15 | var arg: int | ||
| 16 | if tokens.len() > 1: | ||
| 17 | arg = tokens[1].parseInt() | ||
| 18 | else: | ||
| 19 | arg = 0 | ||
| 20 | |||
| 21 | (opcode, arg) | ||
| 22 | |||
| 23 | func parseProgram(content: seq[string]): Program = | ||
| 24 | result = map( | ||
| 25 | content, | ||
| 26 | (line) => instructionFromString(line) | ||
| 27 | ) | ||
| 28 | |||
| 29 | proc checkSignal(cycle: int, x: int): int = | ||
| 30 | # cycle+1 because cycle is supposed to be starting at 0 | ||
| 31 | if cycle+1 in [20, 60, 100, 140, 180, 220]: | ||
| 32 | result = x * (cycle+1) | ||
| 33 | |||
| 34 | var CRT: array[0 .. 5, array[0 .. 39, char]] | ||
| 35 | proc draw(cycle: int, x: int): void = | ||
| 36 | let | ||
| 37 | col = cycle mod 40 | ||
| 38 | row = cycle div 40 | ||
| 39 | if col in [x-1, x, x+1]: | ||
| 40 | CRT[row][col] = '#' | ||
| 41 | |||
| 42 | proc execute(program: Program): int = | ||
| 43 | var | ||
| 44 | x = 1 | ||
| 45 | cycle = 0 | ||
| 46 | |||
| 47 | for i, instr in program: | ||
| 48 | result += checkSignal(cycle, x) | ||
| 49 | draw(cycle, x) | ||
| 50 | case instr.opcode: | ||
| 51 | of "addx": | ||
| 52 | result += checkSignal(cycle+1, x) | ||
| 53 | draw(cycle+1, x) | ||
| 54 | x += instr.arg | ||
| 55 | cycle += 2 | ||
| 56 | of "noop": | ||
| 57 | cycle += 1 | ||
| 58 | else: | ||
| 59 | assert(false) | ||
| 60 | |||
| 61 | let content = readFile("./input.txt").strip().splitLines() | ||
| 62 | let program = parseProgram(content) | ||
| 63 | |||
| 64 | for row in 0 ..< 6: | ||
| 65 | for col in 0 ..< 40: | ||
| 66 | CRT[row][col] = ' ' | ||
| 67 | |||
| 68 | echo execute(program) | ||
| 69 | for row in CRT: | ||
| 70 | 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 @@ | |||
| 1 | Monkey 0: | ||
| 2 | Starting items: 76, 88, 96, 97, 58, 61, 67 | ||
| 3 | Operation: new = old * 19 | ||
| 4 | Test: divisible by 3 | ||
| 5 | If true: throw to monkey 2 | ||
| 6 | If false: throw to monkey 3 | ||
| 7 | |||
| 8 | Monkey 1: | ||
| 9 | Starting items: 93, 71, 79, 83, 69, 70, 94, 98 | ||
| 10 | Operation: new = old + 8 | ||
| 11 | Test: divisible by 11 | ||
| 12 | If true: throw to monkey 5 | ||
| 13 | If false: throw to monkey 6 | ||
| 14 | |||
| 15 | Monkey 2: | ||
| 16 | Starting items: 50, 74, 67, 92, 61, 76 | ||
| 17 | Operation: new = old * 13 | ||
| 18 | Test: divisible by 19 | ||
| 19 | If true: throw to monkey 3 | ||
| 20 | If false: throw to monkey 1 | ||
| 21 | |||
| 22 | Monkey 3: | ||
| 23 | Starting items: 76, 92 | ||
| 24 | Operation: new = old + 6 | ||
| 25 | Test: divisible by 5 | ||
| 26 | If true: throw to monkey 1 | ||
| 27 | If false: throw to monkey 6 | ||
| 28 | |||
| 29 | Monkey 4: | ||
| 30 | Starting items: 74, 94, 55, 87, 62 | ||
| 31 | Operation: new = old + 5 | ||
| 32 | Test: divisible by 2 | ||
| 33 | If true: throw to monkey 2 | ||
| 34 | If false: throw to monkey 0 | ||
| 35 | |||
| 36 | Monkey 5: | ||
| 37 | Starting items: 59, 62, 53, 62 | ||
| 38 | Operation: new = old * old | ||
| 39 | Test: divisible by 7 | ||
| 40 | If true: throw to monkey 4 | ||
| 41 | If false: throw to monkey 7 | ||
| 42 | |||
| 43 | Monkey 6: | ||
| 44 | Starting items: 62 | ||
| 45 | Operation: new = old + 2 | ||
| 46 | Test: divisible by 17 | ||
| 47 | If true: throw to monkey 5 | ||
| 48 | If false: throw to monkey 7 | ||
| 49 | |||
| 50 | Monkey 7: | ||
| 51 | Starting items: 85, 54, 53 | ||
| 52 | Operation: new = old + 3 | ||
| 53 | Test: divisible by 13 | ||
| 54 | If true: throw to monkey 4 | ||
| 55 | If false: throw to monkey 0 | ||
diff --git a/day11/solution.nim b/day11/solution.nim new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/day11/solution.nim | |||
