Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://blocks.programming.dev if you prefer sending it through a URL
Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
Plenty of scope for making part 2 faster, but I think simple is best here. Forgot to sort the lists in the first part, which pushed me waaay off the leaderboard.
import Data.List
main = do
[as, bs] <- transpose . map (map read . words) . lines <$> readFile "input01"
print . sum $ map abs $ zipWith (-) (sort as) (sort bs)
print . sum $ map (\a -> a * length (filter (== a) bs)) as
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)
Right IDs are directly read into a hash map counter.
use std::str::FromStr;
use std::collections::HashMap;
fn part1(input: String) {
let mut left = Vec::new();
let mut right = Vec::new();
for line in input.lines() {
let mut parts = line.split_whitespace()
.map(|p| u32::from_str(p).unwrap());
left.push(parts.next().unwrap());
right.push(parts.next().unwrap());
}
left.sort_unstable();
right.sort_unstable();
let diff: u32 = left.iter().zip(right)
.map(|(l, r)| l.abs_diff(r))
.sum();
println!("{diff}");
}
fn part2(input: String) {
let mut left = Vec::new();
let mut right: HashMap<u32, u32> = HashMap::new();
for line in input.lines() {
let mut parts = line.split_whitespace()
.map(|p| u32::from_str(p).unwrap());
left.push(parts.next().unwrap());
*right.entry(parts.next().unwrap()).or_default() += 1;
}
let similar: u32 = left.iter()
.map(|n| n * right.get(n).copied().unwrap_or_default())
.sum();
println!("{similar}");
}
util::aoc_main!();
Part 1 is a sort and a quick loop. Part 2 could be efficient with a lookup table but it was practically instant with a simple non-memoized scan so left it that way.
Iβm not sure if I understand the question correctly but for every number in the left array I count in the right array. That means duplicate work but shrug π
Iβm quite inexperienced as a programmer, I learned most of the basic concepts from playing human resource machine and 7 billion humans. After mucking about writing some CLI utilities in Perl and python, Iβve decided to give rust a go.
import kotlin.math.abs
fun part1(input: String): Int {
val diffs: MutableList<Int> = mutableListOf()
val pair = parse(input)
pair.first.sort()
pair.second.sort()
pair.first.forEachIndexed { idx, num ->
diffs.add(abs(num - pair.second[idx]))
}
return diffs.sum()
}
fun part2(input: String): Int {
val pair = parse(input)
val frequencies = pair.second.groupingBy { it }.eachCount()
var score = 0
pair.first.forEach { num ->
score += num * frequencies.getOrDefault(num, 0)
}
return score
}
private fun parse(input: String): Pair<MutableList<Int>, MutableList<Int>> {
val left: MutableList<Int> = mutableListOf()
val right: MutableList<Int> = mutableListOf()
input.lines().forEach { line ->
if (line.isNotBlank()) {
val parts = line.split("\\s+".toRegex())
left.add(parts[0].toInt())
right.add(parts[1].toInt())
}
}
return left to right
}
import kotlin.math.abs
fun main() {
fun getLists(input: List<String>): Pair<List<Int>, List<Int>> {
val unsortedPairs = input.map {
it.split(" ").map { it.toInt() }
}
val listA = unsortedPairs.map { it.first() }
val listB = unsortedPairs.map { it.last() }
return Pair(listA, listB)
}
fun part1(input: List<String>): Int {
val (listA, listB) = getLists(input)
return listA.sorted().zip(listB.sorted()).sumOf { abs(it.first - it.second) }
}
fun part2(input: List<String>): Int {
val (listA, listB) = getLists(input)
return listA.sumOf { number ->
number * listB.count { it == number }
}
}
// Or read a large test input from the `src/Day01_test.txt` file:
val testInput = readInput("Day01_test")
check(part1(testInput) == 11)
check(part2(testInput) == 31)
// Read the input from the `src/Day01.txt` file.
val input = readInput("Day01")
part1(input).println()
part2(input).println()
}
It's a bit more compact. (If you take out the part that actually calls the functions on the (test-)input.)
fyi for lines 14-22 you an use .abs instead of checking for negatives and .sum instead of doing it manually. Check my crystal solution to see what I mean
I'm late to the party, as usual. Damned timezones.
This year I'm going to tackle with a small handful of languages, but primarily Elixir and Gleam. This is my first time trying this languages in earnest, so expect some terrible, inefficient and totally unidiomatic code!
Here's day one:
I didn't realize it was december until this afternoon. I've generally chosen a new or spartan lang to challenge myself, but I'm going easy mode this year with python and just focusing on meeting the requirement.
solution
import aoc
def setup():
lines = aoc.get_lines(1)
l = [int(x.split()[0]) for x in lines]
r = [int(x.split()[1]) for x in lines]
return (l, r, 0)
def one():
l, r, acc = setup()
for p in zip(sorted(l), sorted(r)):
acc += abs(p[0] - p[1])
print(acc)
def two():
l, r, acc = setup()
for n in l:
acc += n * r.count(n)
print(acc)
one()
two()
import { readFileSync } from 'fs'
const f = readFileSync('./input.txt', 'utf-8')
const lines = f.split("\n")
let rights = {}
for (const i in lines) {
if (lines[i] == '') { continue }
const [, right] = lines[i].split(/\s+/)
if (rights[right] === undefined) {
rights[right] = 0
}
rights[right]++
}
let ans = 0
for (const i in lines) {
const [left] = lines[i].split(/\s+/)
const similarity = rights[left]
if (similarity) {
ans += (Number(left) * rights[left])
}
}
console.dir(ans)
Is it possible to get this more efficient? I would love a way that only required iterating over the list once, but I don't really have the focus to puzzle it out any less than O(2n) (probably more than that, even, if you count reading in the data...).
I think viml is a very fun language, i like weird languages lol, so this year im doing it in viml while trying to use as many of the original ed/ex commands as i can (:d, :p, :a, :g, ...)
ids = """<multiline input string>"""
ids = [pair.split(" ") for pair in ids.split("\n")]
left = [int(t[0]) for t in ids]
right = [int(t[-1]) for t in ids]
left.sort()
right.sort()
print(sum(abs(l - r) for l, r in zip(left, right)))
# 2
s = 0
for l in left:
s += right.count(l) * l
print(s)
After writing a procedural to-the-point version in C, tried a JavaScript solution too because it's just perfect for list comprehension. The part 2 search is inefficient but the data size is small.