Rust
Turned out alright, I am looking forward to seeing what 2d coordinate grid code I can cannibalize from last year's solutions π
Turned out alright, I am looking forward to seeing what 2d coordinate grid code I can cannibalize from last year's solutions π
Discovered a number of frustrations with this supposedly small and elegant language
(nums at: i) - (nums at: i+1)
which would be nums[i] - nums[i+1]
in most languagesPart 1
day2p1: input
^ (input lines
collect: [ :l | l substrings collect: [ :s | s asInteger ]])
count: [ :nums |
(nums = nums sorted or: nums = nums sorted reverse)
and: [
(1 to: nums size-1) allSatisfy: [ :i |
((nums at: i) - (nums at: i+1)) abs between: 1 and: 3
] ] ]
Part 2
day2p2: input
| temp |
^ (input lines
collect: [ :l | (l substrings collect: [ :s | s asInteger ]) asOrderedCollection ])
count: [ :nums |
(self day2p2helper: nums)
or: [
((1 to: nums size) anySatisfy: [ :i |
temp := nums copy.
temp removeAt: i.
self day2p2helper: temp
])
or: [(self day2p2helper: nums reversed)
or: [
(1 to: nums size) anySatisfy: [ :i |
temp := nums reversed.
temp removeAt: i.
self day2p2helper: temp
]
]]] .
]
day2p2helper: nums
^ (1 to: nums size - 1) allSatisfy: [ :i |
((nums at: i+1) - (nums at: i)) between: 1 and: 3
].
G'MIC solution
spoiler
it day2
crop. 0,0,0,{h#-1-2}
split. -,{_'\n'}
foreach { replace_str. " ",";" ({t}) rm.. }
safe_0,safe_1=0
foreach {
({h}) a[-2,-1] y
num_of_attempts:=da_size(#-1)+1
store temp
repeat $num_of_attempts {
$temp
if $> eval da_remove(#-1,$>-1) fi
eval "
safe=1;
i[#-1,1]>i[#-1,0]?(
for(p=1,p<da_size(#-1),++p,
if(!inrange(i[#-1,p]-i[#-1,p-1],1,3,1,1),safe=0;break(););
);
):(
for(p=1,p<da_size(#-1),++p,
if(!inrange(i[#-1,p-1]-i[#-1,p],1,3,1,1),safe=0;break(););
);
);
safe;"
rm
if $>
if ${} safe_1+=1 break fi
else
if ${} safe_0,safe_1+=1 break fi
fi
}
}
echo Day" "2:" "${safe_0}" :: "${safe_1}
C#
using MathNet.Numerics.LinearAlgebra;
public class Day02 : Solver
{
private ImmutableList<Vector<Double>> data;
public void Presolve(string input)
{
data = input.Trim().Split("\n")
.Select(
line => Vector<Double>.Build.DenseOfEnumerable(line.Split(' ').Select(double.Parse))
).ToImmutableList();
}
private bool IsReportSafe(Vector<Double> report) {
Vector<Double> delta = report.SubVector(1, report.Count - 1)
.Subtract(report.SubVector(0, report.Count - 1));
return (delta.ForAll(x => x > 0) || delta.ForAll(x => x < 0))
&& Vector<Double>.Abs(delta).Max() <= 3;
}
private bool IsDampenedReportSafe(Vector<Double> report) {
for (Double i = 0; i < report.Count; ++i) {
var dampened = Vector<Double>.Build.DenseOfEnumerable(
report.EnumerateIndexed()
.Where(item => item.Item1 != i)
.Select(item => item.Item2));
if (IsReportSafe(dampened)) return true;
}
return false;
}
public string SolveFirst() => data.Where(IsReportSafe).Count().ToString();
public string SolveSecond() => data.Where(IsDampenedReportSafe).Count().ToString();
}
Took me a bit longer to get this one but still quite simple overall.
Spent quite some time on getting to know the try
and assert
operators better.
Run with example input here
# Get the indices matching the ascending/
# descending criteria
CheckAsc β β‘Β°β‘β(β£(βΈβ€.ββ.)β£(βΈβ€.βββ.)0)
# Get the indices matching the distance criteria
CheckDist β β‘Β°β‘β(β£(βΈβ€.β 1β:0)0Γββ₯β€1,3β΅β§-)
Split β β(β½β 1)β½,,
PartOne β (
&rs β &fo "input-2.txt"
β(β‘βββ @ .)β @\n.
CheckAsc.
β½
CheckDist
β§»β
)
PartTwo β (
&rs β &fo "input-2.txt"
β(β‘βββ @ .)β @\n.
CheckAsc.
Split
CheckDist.
Split
β(β)
β§»
:
β(β‘(β½:Β°β)βΒ€ββ:β 1β=.β‘β§».)
β‘(β§»βCheckDistβ½CheckAsc.Β°β‘)
+β§»β΄β
)
&p "Day 2:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
An unofficial home for the advent of code community on programming.dev!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Solution Threads
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')