Skip to content

Commit 061508c

Browse files
authored
Merge pull request devcordde#40 from JohnnyJayJay/master
Solutions for day 7 and 8
2 parents dbb2328 + f96f180 commit 061508c

File tree

5 files changed

+1370
-15
lines changed

5 files changed

+1370
-15
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(ns solution
2+
(:require [clojure.set :as sets]))
3+
4+
(def child-bag #"(\d+) (\w+ \w+) bags?(?:, )?")
5+
6+
(def bag-format #"(?m)^(\w+ \w+) bags contain (?:(?:no other bags)|(.+))\.$")
7+
8+
(defn parse-int [s]
9+
(Integer/parseInt s))
10+
11+
(defn parse-children [[_ children-str :as v]]
12+
(assoc
13+
v 1
14+
(if children-str
15+
(->> (re-seq child-bag children-str)
16+
(map (comp vec rseq #(update % 0 parse-int) #(subvec % 1)))
17+
(into {}))
18+
{})))
19+
20+
(defn parse-bag-rules [input]
21+
(->> (re-seq bag-format input)
22+
(map #(subvec % 1))
23+
(map parse-children)
24+
(into {})))
25+
26+
(defn create-parent-map [bag-rule-map]
27+
(reduce-kv (fn [m k v] (reduce #(update %1 %2 conj k) m (keys v)))
28+
(zipmap (keys bag-rule-map) (repeat #{}))
29+
bag-rule-map))
30+
31+
(defn parent-bags [bag-map bag-name]
32+
(let [parents (bag-map bag-name)]
33+
(if (seq parents)
34+
(reduce sets/union parents (map (partial parent-bags bag-map) parents))
35+
#{})))
36+
37+
(defn contained-bag-count [bag-map bag-name]
38+
(if-let [children (seq (bag-map bag-name))]
39+
(reduce + 1 (map (fn [[name count]] (* count (contained-bag-count bag-map name))) children))
40+
1))
41+
42+
(defn solve [input]
43+
(let [child-map (parse-bag-rules input)
44+
parent-map (create-parent-map child-map)]
45+
(println "There are" (count (parent-bags parent-map "shiny gold")) "bags that contain a shiny gold bag.")
46+
(println "There are" (dec (contained-bag-count child-map "shiny gold")) "bags within a single shiny gold bag.")))
47+
48+
(solve (slurp (first *command-line-args*)))

Day-08/c/JohnnyJayJay/Day08.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "../../../shared/JohnnyJayJay/aoc.h"
5+
6+
typedef struct {
7+
char* opcode;
8+
int arg;
9+
} instruction;
10+
11+
typedef struct {
12+
int* possible_corruption;
13+
int possible_corruption_len;
14+
int acc;
15+
int infinite_loop;
16+
} execution_result;
17+
18+
execution_result* run_program(int length, instruction* insts) {
19+
int* visits = calloc(sizeof(int), length);
20+
int acc = 0;
21+
int* possible_corruption = malloc(sizeof(int) * length);
22+
int possible_corruption_len = 0;
23+
int infinite_loop = 0;
24+
for (int i = 0; i < length; i++) {
25+
if (visits[i]) {
26+
infinite_loop = 1;
27+
break;
28+
}
29+
visits[i] = 1;
30+
instruction* inst = &insts[i];
31+
char* opcode = inst->opcode;
32+
int arg = inst->arg;
33+
if (strcmp(opcode, "acc") == 0) {
34+
acc += arg;
35+
} else {
36+
possible_corruption[possible_corruption_len] = i;
37+
possible_corruption_len++;
38+
if (strcmp(opcode, "jmp") == 0) {
39+
i += arg - 1;
40+
}
41+
}
42+
43+
}
44+
free(visits);
45+
execution_result* res = malloc(sizeof(execution_result));
46+
res->possible_corruption = possible_corruption;
47+
res->possible_corruption_len = possible_corruption_len;
48+
res->acc = acc;
49+
res->infinite_loop = infinite_loop;
50+
return res;
51+
}
52+
53+
void free_execution_result(execution_result* result) {
54+
free(result->possible_corruption);
55+
free(result);
56+
}
57+
58+
void fix_program(int length, instruction* insts, int* possible_corruption, int possible_corruption_len) {
59+
for (int i = 0; i < possible_corruption_len; i++) {
60+
int pos = possible_corruption[i];
61+
instruction old = insts[pos];
62+
instruction new = {strcmp(old.opcode, "jmp") == 0 ? "nop" : "jmp", old.arg};
63+
insts[pos] = new;
64+
execution_result* result = run_program(length, insts);
65+
int infinite_loop = result->infinite_loop;
66+
free_execution_result(result);
67+
if (infinite_loop) {
68+
insts[pos] = old;
69+
} else {
70+
return;
71+
}
72+
}
73+
}
74+
75+
76+
int main(int argc, char** argv) {
77+
FILE* file = fopen(argv[1], "r");
78+
int lines = count_lines(file);
79+
instruction* insts = malloc(sizeof(instruction) * lines);
80+
for (int i = 0; i < lines; i++) {
81+
char* opcode = malloc(4);
82+
int arg;
83+
fscanf(file, "%3s %d", opcode, &arg);
84+
instruction inst = {opcode, arg};
85+
insts[i] = inst;
86+
}
87+
88+
execution_result* result = run_program(lines, insts);
89+
printf("The accumulator is %d\n", result->acc);
90+
fix_program(lines, insts, result->possible_corruption, result->possible_corruption_len);
91+
result = run_program(lines, insts);
92+
printf("The fixed accumulator is %d\n", result->acc);
93+
}

0 commit comments

Comments
 (0)