Xah Talk Show 2022-12-16 Advent of Code Day 4, in WolframLang, Live Coding

Xah Talk Show 2022-12-16 Advent of Code Day 4, in WolframLang, Live Coding

part 1

xinput =
"2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8";

Clear[ f ]
f[{{a_,b_},{x_, y_}}] := ( (x <= a) && (b <= y)) || ( (a <= x) && (y <= b))

x1= StringSplit[ xinput  ]
x2 = Map[ StringSplit[ #, "," ]&, x1 ]
x3 = Map[ ToExpression @ StringSplit[ #, "-" ]&, x2, {-1} ]

x4 = Map[ f, x3 ]
x5 = Cases[ x4, True ]//Length
xinput =
"2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8";

Cases[ (Map[ ToExpression @ StringSplit[ #, "-" ]&, ((StringSplit[ #, "," ]&) /@ (StringSplit@xinput)) , {-1} ] /. {{a_,b_},{x_, y_}} -> ( (x <= a) && (b <= y)) || ( (a <= x) && (y <= b))), True ]//Length

part 2

xinput =
"2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8";

Clear[ f ]
f[{{a_,b_},{x_, y_}}] := ((a <= y) && (b >= x)) || ((x <= b) && (y >= a))
(* f[{{a_,b_},{x_, y_}}] := b >= x && a <= y *)

x1= StringSplit[ xinput  ]
x2 = Map[ StringSplit[ #, "," ]&, x1 ]
x3 = Map[ ToExpression @ StringSplit[ #, "-" ]&, x2, {-1} ]

x4 = Map[ f, x3 ]
x5 = Cases[ x4, True ]//Length
advent of code 2022 day 4 2022-12-16 z7tVQ
advent of code 2022 day 4 2022-12-16 z7tVQ
advent of code 2022 day 4 2022-12-16 2hR6f
advent of code 2022 day 4 2022-12-16 2hR6f
advent of code 2022 day 4 2022-12-16 2nQjR
advent of code 2022 day 4 2022-12-16 2nQjR

2022 Advent of Code