remove aoc solutions
This commit is contained in:
@@ -1,38 +0,0 @@
|
|||||||
func part1[l1: Array, l2: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..l1->size
|
|
||||||
out += (l1->nth(i) as i64 - l2->nth(i) as i64)->abs()
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func part2[l1: Array, l2: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..l1->size
|
|
||||||
out += l1->nth(i) * l2->count(l1->nth(i))
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
|
|
||||||
l1 := []
|
|
||||||
l2 := []
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
parts := line->split(" ")
|
|
||||||
|
|
||||||
l1->push((parts->nth(0) as str)->parse_i64())
|
|
||||||
l2->push((parts->nth(1) as str)->parse_i64())
|
|
||||||
|
|
||||||
l1->quicksort()
|
|
||||||
l2->quicksort()
|
|
||||||
|
|
||||||
part1(l1, l2)
|
|
||||||
part2(l1, l2)
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
func check[report: Array] : bool
|
|
||||||
increasing := report->nth(0) < report->nth(1)
|
|
||||||
|
|
||||||
for i in 0..report->size-1
|
|
||||||
if report->nth(i) > report->nth(i + 1) && increasing
|
|
||||||
return false
|
|
||||||
if report->nth(i) < report->nth(i + 1) && !increasing
|
|
||||||
return false
|
|
||||||
|
|
||||||
diff := (report->nth(i) as i64 - report->nth(i + 1) as i64)->abs()
|
|
||||||
if diff < 1 || diff > 3
|
|
||||||
return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
|
|
||||||
func part1[data: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..data->size
|
|
||||||
if check(data->nth(i))
|
|
||||||
out += 1
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func part2[data: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..data->size
|
|
||||||
if check(data->nth(i))
|
|
||||||
out += 1
|
|
||||||
else
|
|
||||||
arr : Array = data->nth(i)
|
|
||||||
for j in 0..arr->size
|
|
||||||
sliced := arr->slice(0, j)->concat(arr->slice(j + 1, arr->size - (j + 1)))
|
|
||||||
|
|
||||||
if check(sliced)
|
|
||||||
out += 1
|
|
||||||
break
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
|
|
||||||
data := []
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
|
|
||||||
report := line->split(" ")
|
|
||||||
for i in 0..report->size
|
|
||||||
report->set(i, (report->nth(i) as str)->parse_i64())
|
|
||||||
data->push(report)
|
|
||||||
|
|
||||||
part1(data)
|
|
||||||
part2(data)
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
|
||||||
if x + dx * 3 < 0 || x + dx * 3 >= lines->size || y + dy * 3 < 0 || y + dy * 3 >= (lines->nth(0) as str)->len()
|
|
||||||
return false
|
|
||||||
|
|
||||||
if lines->nth(x)[y] != 'X'
|
|
||||||
return false
|
|
||||||
if lines->nth(x + dx)[y + dy] != 'M'
|
|
||||||
return false
|
|
||||||
if lines->nth(x + dx * 2)[y + dy * 2] != 'A'
|
|
||||||
return false
|
|
||||||
if lines->nth(x + dx * 3)[y + dy * 3] != 'S'
|
|
||||||
return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
|
|
||||||
func part1[lines: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for x in 0..lines->size
|
|
||||||
for y in 0..(lines->nth(x) as str)->len()
|
|
||||||
if check(lines, x, y, 0, 1)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, 0, -1)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, 1, 0)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, -1, 0)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, 1, 1)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, -1, -1)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, 1, -1)
|
|
||||||
out += 1
|
|
||||||
if check(lines, x, y, -1, 1)
|
|
||||||
out += 1
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func part2[lines: Array] : void
|
|
||||||
out := 0
|
|
||||||
s := _stackalloc(5) as str
|
|
||||||
|
|
||||||
for x in 1..lines->size-1
|
|
||||||
line_len := (lines->nth(x) as str)->len()
|
|
||||||
for y in 1..line_len-1
|
|
||||||
if lines->nth(x)[y] == 'A'
|
|
||||||
s[0] = lines->nth(x - 1)[y - 1]
|
|
||||||
s[1] = lines->nth(x + 1)[y - 1]
|
|
||||||
s[2] = lines->nth(x + 1)[y + 1]
|
|
||||||
s[3] = lines->nth(x - 1)[y + 1]
|
|
||||||
s[4] = 0
|
|
||||||
|
|
||||||
if s->equal("MSSM") || s->equal("SMMS") || s->equal("MMSS") || s->equal("SSMM")
|
|
||||||
out += 1
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
|
|
||||||
part1(lines)
|
|
||||||
part2(lines)
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
func rule_exists[rules_left: Array, rules_right: Array, a: i64, b: i64] : bool
|
|
||||||
for k in 0..rules_left->size
|
|
||||||
if rules_left->nth(k) == a && rules_right->nth(k) == b
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
||||||
func check[update: Array, rules_left: Array, rules_right: Array] : bool
|
|
||||||
for i in 0..update->size
|
|
||||||
for j in 0..i
|
|
||||||
if rule_exists(rules_left, rules_right, update->nth(i), update->nth(j))
|
|
||||||
return false
|
|
||||||
return true
|
|
||||||
|
|
||||||
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
|
|
||||||
swapped := true
|
|
||||||
|
|
||||||
while swapped
|
|
||||||
swapped = false
|
|
||||||
for i in 0..update->size-1
|
|
||||||
a : i64 = update->nth(i)
|
|
||||||
b : i64 = update->nth(i+1)
|
|
||||||
if rule_exists(rules_left, rules_right, b, a)
|
|
||||||
tmp := a
|
|
||||||
update->set(i, b)
|
|
||||||
update->set(i+1, tmp)
|
|
||||||
swapped = true
|
|
||||||
|
|
||||||
func part1[updates: Array, rules_left: Array, rules_right: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..updates->size
|
|
||||||
update : Array = updates->nth(i)
|
|
||||||
if check(update, rules_left, rules_right)
|
|
||||||
out += update->nth(update->size / 2)
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..updates->size
|
|
||||||
update : Array = updates->nth(i)
|
|
||||||
if !check(update, rules_left, rules_right)
|
|
||||||
sort_by_rules(update, rules_left, rules_right)
|
|
||||||
out += update->nth(update->size / 2)
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
data := input->split("\n\n")
|
|
||||||
|
|
||||||
rules_left := []
|
|
||||||
rules_right := []
|
|
||||||
rules_lines := (data->nth(0) as str)->split("\n")
|
|
||||||
for i in 0..rules_lines->size
|
|
||||||
line : str = rules_lines->nth(i)
|
|
||||||
parts := line->split("|")
|
|
||||||
|
|
||||||
rules_left->push((parts->nth(0) as str)->parse_i64())
|
|
||||||
rules_right->push((parts->nth(1) as str)->parse_i64())
|
|
||||||
|
|
||||||
updates := []
|
|
||||||
updates_lines := (data->nth(1) as str)->split("\n")
|
|
||||||
for i in 0..updates_lines->size
|
|
||||||
line : str = updates_lines->nth(i)
|
|
||||||
xs := line->split(",")
|
|
||||||
|
|
||||||
for i in 0..xs->size
|
|
||||||
xs->set(i, (xs->nth(i) as str)->parse_i64())
|
|
||||||
updates->push(xs)
|
|
||||||
|
|
||||||
part1(updates, rules_left, rules_right)
|
|
||||||
part2(updates, rules_left, rules_right)
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
func concat[a: i64, b: i64] : i64
|
|
||||||
ab_str := _stackalloc(50) as str
|
|
||||||
ab_str->format_into(50, "%d%d", a, b)
|
|
||||||
return ab_str->parse_i64()
|
|
||||||
|
|
||||||
func solve[ops: Array, e: Array] : i64
|
|
||||||
e_1 : Array = e->nth(1)
|
|
||||||
n := e_1->size - 1
|
|
||||||
indices := []
|
|
||||||
for i in 0..n
|
|
||||||
indices->push(0)
|
|
||||||
|
|
||||||
while true
|
|
||||||
res : i64 = e_1->nth(0)
|
|
||||||
for i in 0..n
|
|
||||||
op : str = ops->nth(indices->nth(i))
|
|
||||||
|
|
||||||
if op->equal("add")
|
|
||||||
res += e_1->nth(i + 1)
|
|
||||||
else if op->equal("mul")
|
|
||||||
res = res * e_1->nth(i + 1)
|
|
||||||
else if op->equal("concat")
|
|
||||||
res = concat(res, e_1->nth(i + 1))
|
|
||||||
if res == e->nth(0)
|
|
||||||
return res
|
|
||||||
|
|
||||||
done := true
|
|
||||||
i := n - 1
|
|
||||||
|
|
||||||
while i >= 0
|
|
||||||
indices->set(i, indices->nth(i) + 1)
|
|
||||||
if indices->nth(i) < ops->size
|
|
||||||
done = false
|
|
||||||
break
|
|
||||||
indices->set(i, 0)
|
|
||||||
i -= 1
|
|
||||||
|
|
||||||
if done
|
|
||||||
return 0
|
|
||||||
|
|
||||||
func part1[equations: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..equations->size
|
|
||||||
out += solve(["add", "mul"], equations->nth(i))
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func part2[equations: Array] : void
|
|
||||||
out := 0
|
|
||||||
|
|
||||||
for i in 0..equations->size
|
|
||||||
out += solve(["add", "mul", "concat"], equations->nth(i))
|
|
||||||
|
|
||||||
io.println_i64(out)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
equations := []
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
parts := line->split(": ")
|
|
||||||
|
|
||||||
xs := (parts->nth(1) as str)->split(" ")
|
|
||||||
for j in 0..xs->size
|
|
||||||
xs->set(j, (xs->nth(j) as str)->parse_i64())
|
|
||||||
|
|
||||||
equations->push([(parts->nth(0) as str)->parse_i64(), xs])
|
|
||||||
|
|
||||||
part1(equations)
|
|
||||||
part2(equations)
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
func part1[lines: Array] : void
|
|
||||||
password := 0
|
|
||||||
dial := 50
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
dir := line[0]
|
|
||||||
n := line->substr(1, line->len() - 1)->parse_i64()
|
|
||||||
|
|
||||||
if dir == 'L'
|
|
||||||
dial -= n
|
|
||||||
while dial < 0
|
|
||||||
dial += 100
|
|
||||||
else
|
|
||||||
dial += n
|
|
||||||
while dial >= 100
|
|
||||||
dial -= 100
|
|
||||||
|
|
||||||
if dial == 0
|
|
||||||
password += 1
|
|
||||||
|
|
||||||
io.println_i64(password)
|
|
||||||
|
|
||||||
func part2[lines: Array] : void
|
|
||||||
password := 0
|
|
||||||
dial := 50
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
dir := line[0]
|
|
||||||
n := line->substr(1, line->len() - 1)->parse_i64()
|
|
||||||
|
|
||||||
if dir == 'L'
|
|
||||||
for i in 0..n
|
|
||||||
dial -= 1
|
|
||||||
if dial == 0
|
|
||||||
password += 1
|
|
||||||
if dial == -1
|
|
||||||
dial = 99
|
|
||||||
else
|
|
||||||
for i in 0..n
|
|
||||||
dial += 1
|
|
||||||
if dial == 100
|
|
||||||
dial = 0
|
|
||||||
password += 1
|
|
||||||
|
|
||||||
io.println_i64(password)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
|
|
||||||
part1(lines)
|
|
||||||
part2(lines)
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
func part1_is_invalid_id[s: str] : bool
|
|
||||||
len := s->len()
|
|
||||||
if len % 2 != 0
|
|
||||||
return false
|
|
||||||
|
|
||||||
first := s->substr(0, len / 2)
|
|
||||||
second := s->substr(len / 2, len / 2)
|
|
||||||
|
|
||||||
return first->equal(second)
|
|
||||||
|
|
||||||
func part1[ranges: Array] : void
|
|
||||||
sum := 0
|
|
||||||
|
|
||||||
for i in 0..ranges->size
|
|
||||||
parts := (ranges->nth(i) as str)->split("-")
|
|
||||||
start := (parts->nth(0) as str)->parse_i64()
|
|
||||||
end := (parts->nth(1) as str)->parse_i64()
|
|
||||||
|
|
||||||
for n in (start)..end+1
|
|
||||||
if part1_is_invalid_id(n->to_str())
|
|
||||||
sum += n
|
|
||||||
|
|
||||||
io.println_i64(sum)
|
|
||||||
|
|
||||||
// had to cheat this one
|
|
||||||
func part2_is_invalid_id[s: str] : bool
|
|
||||||
len := s->len()
|
|
||||||
if len < 2
|
|
||||||
return false
|
|
||||||
for div in 1..(len / 2 + 1)
|
|
||||||
if len % div == 0
|
|
||||||
u := s->substr(0, div)
|
|
||||||
is_repeat := true
|
|
||||||
for k in 1..(len / div)
|
|
||||||
segment := s->substr(k * div, div)
|
|
||||||
if !segment->equal(u)
|
|
||||||
is_repeat = false
|
|
||||||
if is_repeat
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
||||||
func part2[ranges: Array] : void
|
|
||||||
sum := 0
|
|
||||||
|
|
||||||
for i in 0..ranges->size
|
|
||||||
parts := (ranges->nth(i) as str)->split("-")
|
|
||||||
start := (parts->nth(0) as str)->parse_i64()
|
|
||||||
end := (parts->nth(1) as str)->parse_i64()
|
|
||||||
|
|
||||||
for n in (start)..end+1
|
|
||||||
if part2_is_invalid_id(n->to_str())
|
|
||||||
sum += n
|
|
||||||
|
|
||||||
io.println_i64(sum)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
ranges := input->split(",")
|
|
||||||
|
|
||||||
part1(ranges)
|
|
||||||
part2(ranges)
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
func part1[lines: Array] : void
|
|
||||||
sum := 0
|
|
||||||
s := _stackalloc(3) as str
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
|
|
||||||
largest := 0
|
|
||||||
for j in 0..line->len()
|
|
||||||
for k in (j+1)..line->len()
|
|
||||||
s[0] = line[j]
|
|
||||||
s[1] = line[k]
|
|
||||||
s[2] = 0
|
|
||||||
n := s->parse_i64()
|
|
||||||
if n > largest
|
|
||||||
largest = n
|
|
||||||
|
|
||||||
sum += largest
|
|
||||||
io.println_i64(sum)
|
|
||||||
|
|
||||||
// had to cheat this one
|
|
||||||
func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
|
||||||
largest := 0
|
|
||||||
largest_idx := start
|
|
||||||
len := bank->len()
|
|
||||||
|
|
||||||
for i in (start)..(len-remaining+1)
|
|
||||||
v := (bank[i] - '0') as i64
|
|
||||||
if v > largest
|
|
||||||
largest = v
|
|
||||||
largest_idx = i
|
|
||||||
|
|
||||||
if remaining > 1
|
|
||||||
return largest * math.pow(10, remaining - 1) + part2_rec(bank, largest_idx + 1, remaining - 1)
|
|
||||||
else
|
|
||||||
return largest
|
|
||||||
|
|
||||||
func part2[lines: Array] : void
|
|
||||||
sum := 0
|
|
||||||
|
|
||||||
for i in 0..lines->size
|
|
||||||
line : str = lines->nth(i)
|
|
||||||
|
|
||||||
sum += part2_rec(line, 0, 12)
|
|
||||||
io.println_i64(sum)
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
~input, ok := io.read_text_file("input.txt")
|
|
||||||
if !ok
|
|
||||||
panic("failed to open input.txt")
|
|
||||||
|
|
||||||
lines := input->split("\n")
|
|
||||||
|
|
||||||
part1(lines)
|
|
||||||
part2(lines)
|
|
||||||
8
test.zr
8
test.zr
@@ -1,6 +1,6 @@
|
|||||||
func run_test[x: str] : void
|
func run_test[x: str] : void
|
||||||
build_blacklist := ["examples/puzzles", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
build_blacklist := ["examples/euler", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
||||||
run_blacklist := ["/aoc", "guess_number.zr", "tcp_server.zr", "udp_server.zr"]
|
run_blacklist := ["guess_number.zr", "tcp_server.zr", "udp_server.zr"]
|
||||||
|
|
||||||
for i in 0..build_blacklist->size
|
for i in 0..build_blacklist->size
|
||||||
if x->equal(build_blacklist->nth(i))
|
if x->equal(build_blacklist->nth(i))
|
||||||
@@ -18,7 +18,7 @@ func run_test[x: str] : void
|
|||||||
io.printf("%dms\n", build_end_time - build_start_time)
|
io.printf("%dms\n", build_end_time - build_start_time)
|
||||||
|
|
||||||
for i in 0..run_blacklist->size
|
for i in 0..run_blacklist->size
|
||||||
if x->find(run_blacklist->nth(i)) != -1
|
if x->contains(run_blacklist->nth(i))
|
||||||
io.printf("Skipping %s...\n", x)
|
io.printf("Skipping %s...\n", x)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@@ -49,4 +49,4 @@ func main[] : i64
|
|||||||
os.exit(1)
|
os.exit(1)
|
||||||
|
|
||||||
run_directory("examples/")
|
run_directory("examples/")
|
||||||
run_directory("examples/puzzles/")
|
run_directory("examples/euler/")
|
||||||
|
|||||||
Reference in New Issue
Block a user