You can put egregious amount of cheese on anything and it would taste atleast half as good as pizza.
janAkali
Nim
Solution: sort numbers using custom rules and compare if sorted == original. Part 2 is trivial.
Runtime for both parts: 1.05 ms
proc parseRules(input: string): Table[int, seq[int]] =
for line in input.splitLines():
let pair = line.split('|')
let (a, b) = (pair[0].parseInt, pair[1].parseInt)
discard result.hasKeyOrPut(a, newSeq[int]())
result[a].add b
proc solve(input: string): AOCSolution[int, int] =
let chunks = input.split("\n\n")
let later = parseRules(chunks[0])
for line in chunks[1].splitLines():
let numbers = line.split(',').map(parseInt)
let sorted = numbers.sorted(cmp =
proc(a,b: int): int =
if a in later and b in later[a]: -1
elif b in later and a in later[b]: 1
else: 0
)
if numbers == sorted:
result.part1 += numbers[numbers.len div 2]
else:
result.part2 += sorted[sorted.len div 2]
Nim
Could be done more elegantly, but I havenβt bothered yet.
proc solve(input: string): AOCSolution[int, int] =
var lines = input.splitLines()
block p1:
# horiz
for line in lines:
for i in 0..line.high-3:
if line[i..i+3] in ["XMAS", "SAMX"]:
inc result.part1
for y in 0..lines.high-3:
#vert
for x in 0..lines[0].high:
let word = collect(for y in y..y+3: lines[y][x])
if word in [@"XMAS", @"SAMX"]:
inc result.part1
#diag \
for x in 0..lines[0].high-3:
let word = collect(for d in 0..3: lines[y+d][x+d])
if word in [@"XMAS", @"SAMX"]:
inc result.part1
#diag /
for x in 3..lines[0].high:
let word = collect(for d in 0..3: lines[y+d][x-d])
if word in [@"XMAS", @"SAMX"]:
inc result.part1
block p2:
for y in 0..lines.high-2:
for x in 0..lines[0].high-2:
let diagNW = collect(for d in 0..2: lines[y+d][x+d])
let diagNE = collect(for d in 0..2: lines[y+d][x+2-d])
if diagNW in [@"MAS", @"SAM"] and diagNE in [@"MAS", @"SAM"]:
inc result.part2
Nim
From a first glance it was obviously a regex problem.
I'm using tinyre here instead of stdlib re
library just because I'm more familiar with it.
import pkg/tinyre
proc solve(input: string): AOCSolution[int, int] =
var allow = true
for match in input.match(reG"mul\(\d+,\d+\)|do\(\)|don't\(\)"):
if match == "do()": allow = true
elif match == "don't()": allow = false
else:
let m = match[4..^2].split(',')
let mult = m[0].parseInt * m[1].parseInt
result.part1 += mult
if allow: result.part2 += mult
Nim, because it's fast and expressive.
Cool to see another solution in Nim here =)
(0..<record.len).anyIt(record.dup(delete(it)).validate)
That's smart. I haven't thought of using iterators to loop over indexes (except in a for loop
).
I got stuck on part 2 trying to check everything inside a single loop, which kept getting more ugly.
Yeah I've thought of simple ways to do this and found none. And looking at the input - it's too easy to bruteforce, especially in compiled lang like Nim.
Nim
Got correct answer for part 1 on first try, but website rejected it. Wasted some time debugging and trying different methods. Only to have the same answer accepted minutes later. =(
proc isSafe(report: seq[int]): bool =
let diffs = collect:
for i, n in report.toOpenArray(1, report.high): n - report[i]
(diffs.allIt(it > 0) or diffs.allIt(it < 0)) and diffs.allIt(it.abs in 1..3)
proc solve(input: string): AOCSolution[int, int] =
let lines = input.splitLines()
var reports: seq[seq[int]]
for line in lines:
reports.add line.split(' ').map(parseInt)
for report in reports:
if report.isSafe():
inc result.part1
inc result.part2
else:
for t in 0..report.high:
var mReport = report
mReport.delete t
if mReport.isSafe():
inc result.part2
break
Hey, why isnβt China more green? Iβd think the CCP hates western spyware OSes.
I remember seeing lemmy maybe 4+ years ago on some open-source subreddit. It had practically non-existent user base, so I've ignored it. After that, I remember a first wave of people making mastodon accounts (even before elon). There I've first heard of concept of "fediverse". I liked the idea but I honestly thought it had zero chances to compete with mainstream social media.
And then everything turned to shit, making a gap between something like lemmy and reddit a lot smaller. So I've jumped the ship with everyone after the API shitstorm.
Nim
I've got my first sub-1000 rank today (998 for part 2). Yay!
Simple and straightforward challenge, very fitting for 1st day. Gonna enjoy it while it lasts.
proc solve(input: string): AOCSolution[int, int] =
var l1,l2: seq[int]
for line in input.splitLines():
let pair = line.splitWhitespace()
l1.add parseInt(pair[0])
l2.add parseInt(pair[1])
l1.sort()
l2.sort()
block p1:
for i in 0..l1.high:
result.part1 += abs(l1[i] - l2[i])
block p2:
for n in l1:
result.part2 += n * l2.count(n)
Oh, and GBA rom is included with game files.
Good old bubble sort