aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAn0nSaiko <porfeas12@gmail.com>2022-12-12 00:43:39 +0200
committerAn0nSaiko <porfeas12@gmail.com>2022-12-12 00:43:39 +0200
commit079548b237ecf351657684dd053f3e14a55ff0ea (patch)
tree7d07c919f7e33f348c453ef5e38aec47cf999744
parentDay 9 (diff)
downloadaoc22-079548b237ecf351657684dd053f3e14a55ff0ea.tar.gz
aoc22-079548b237ecf351657684dd053f3e14a55ff0ea.zip
Day 11
-rw-r--r--day10/example.txt146
-rw-r--r--day10/input.txt144
-rw-r--r--day10/solution.nim70
-rw-r--r--day11/input.txt55
-rw-r--r--day11/solution.nim0
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 @@
1addx 15
2addx -11
3addx 6
4addx -3
5addx 5
6addx -1
7addx -8
8addx 13
9addx 4
10noop
11addx -1
12addx 5
13addx -1
14addx 5
15addx -1
16addx 5
17addx -1
18addx 5
19addx -1
20addx -35
21addx 1
22addx 24
23addx -19
24addx 1
25addx 16
26addx -11
27noop
28noop
29addx 21
30addx -15
31noop
32noop
33addx -3
34addx 9
35addx 1
36addx -3
37addx 8
38addx 1
39addx 5
40noop
41noop
42noop
43noop
44noop
45addx -36
46noop
47addx 1
48addx 7
49noop
50noop
51noop
52addx 2
53addx 6
54noop
55noop
56noop
57noop
58noop
59addx 1
60noop
61noop
62addx 7
63addx 1
64noop
65addx -13
66addx 13
67addx 7
68noop
69addx 1
70addx -33
71noop
72noop
73noop
74addx 2
75noop
76noop
77noop
78addx 8
79noop
80addx -1
81addx 2
82addx 1
83noop
84addx 17
85addx -9
86addx 1
87addx 1
88addx -3
89addx 11
90noop
91noop
92addx 1
93noop
94addx 1
95noop
96noop
97addx -13
98addx -19
99addx 1
100addx 3
101addx 26
102addx -30
103addx 12
104addx -1
105addx 3
106addx 1
107noop
108noop
109noop
110addx -9
111addx 18
112addx 1
113addx 2
114noop
115noop
116addx 9
117noop
118noop
119noop
120addx -1
121addx 2
122addx -37
123addx 1
124addx 3
125noop
126addx 15
127addx -21
128addx 22
129addx -6
130addx 1
131noop
132addx 2
133addx 1
134noop
135addx -10
136noop
137noop
138addx 20
139addx 1
140addx 2
141addx 2
142addx -6
143addx -11
144noop
145noop
146noop
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 @@
1noop
2noop
3addx 6
4addx -1
5noop
6addx 5
7addx 3
8noop
9addx 3
10addx -1
11addx -13
12addx 17
13addx 3
14addx 3
15noop
16noop
17noop
18addx 5
19addx 1
20noop
21addx 4
22addx 1
23noop
24addx -38
25addx 5
26noop
27addx 2
28addx 3
29noop
30addx 2
31addx 2
32addx 3
33addx -2
34addx 5
35addx 2
36addx -18
37addx 6
38addx 15
39addx 5
40addx 2
41addx -22
42noop
43noop
44addx 30
45noop
46noop
47addx -39
48addx 1
49addx 19
50addx -16
51addx 35
52addx -28
53addx -1
54addx 12
55addx -8
56noop
57addx 3
58addx 4
59noop
60addx -3
61addx 6
62addx 5
63addx 2
64noop
65noop
66noop
67noop
68noop
69addx 7
70addx -39
71noop
72noop
73addx 5
74addx 2
75addx 2
76addx -1
77addx 2
78addx 2
79addx 5
80addx 1
81noop
82addx 4
83addx -13
84addx 18
85noop
86noop
87noop
88addx 12
89addx -9
90addx 8
91noop
92noop
93addx -2
94addx -36
95noop
96noop
97addx 5
98addx 2
99addx 3
100addx -2
101addx 2
102addx 2
103noop
104addx 3
105addx 5
106addx 2
107addx 19
108addx -14
109noop
110addx 2
111addx 3
112noop
113addx -29
114addx 34
115noop
116addx -35
117noop
118addx -2
119addx 2
120noop
121addx 6
122noop
123noop
124noop
125noop
126addx 2
127noop
128addx 3
129addx 2
130addx 5
131addx 2
132addx 1
133noop
134addx 4
135addx -17
136addx 18
137addx 4
138noop
139addx 1
140addx 4
141noop
142addx 1
143noop
144noop
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 @@
1import strutils
2import sequtils
3import sugar
4
5type
6 Instruction = tuple
7 opcode: string
8 arg: int
9
10 Program = seq[Instruction]
11
12func 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
23func parseProgram(content: seq[string]): Program =
24 result = map(
25 content,
26 (line) => instructionFromString(line)
27 )
28
29proc 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
34var CRT: array[0 .. 5, array[0 .. 39, char]]
35proc 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
42proc 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
61let content = readFile("./input.txt").strip().splitLines()
62let program = parseProgram(content)
63
64for row in 0 ..< 6:
65 for col in 0 ..< 40:
66 CRT[row][col] = ' '
67
68echo execute(program)
69for 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 @@
1Monkey 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
8Monkey 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
15Monkey 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
22Monkey 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
29Monkey 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
36Monkey 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
43Monkey 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
50Monkey 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