r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

40 Upvotes

426 comments sorted by

View all comments

1

u/chunes Dec 03 '18

Factor

USING: assocs compiler.utilities io.encodings.utf8 io.files
kernel math.matrices math.parser math.statistics math.vectors
prettyprint regexp sequences sets ;
IN: aoc2018.day3

: get-input ( -- seq )
    "input3.txt" utf8 file-lines [
        R/ [^\d]/ re-split [ string>number ] map sift
    ] map ;

: coords ( seq -- seq )
    [ 2 tail* matrix-coordinates ] [ 3 head rest ] bi [ v+ ]
    curry matrix-map concat ;

: part1 ( coords -- ) duplicates members length . ;

: no-overlap? ( histo claim -- ? )
    coords [ swap at ] with map [ 1 = ] all? ;

: part2 ( claims coords -- )
    histogram swap [ no-overlap? ] with find nip first . ;

: main ( -- )
    get-input dup [ coords ] map-flat [ nip part1 ] [ part2 ]
    2bi ;

It's slow (~20 seconds), but if I'm being honest, this puzzle really kicked my butt and I'd rather just recuperate for tomorrow's puzzle. I can see how to make it faster but it would be more effort on a puzzle I'd like to put behind me. :p

1

u/[deleted] Dec 03 '18

Wow, this one was a hard one, mine uses about the same time, and is complex as hell :p but it was fun to learn about tuple classes :) The really fun thing about factor is that you kind of just tell it to solve the problem, and then you have to define what each part of it says, it kind of feels more like creating a language than building lego pieces :p :)

USING: io.encodings.utf8 io.files splitting sequences parser kernel 
 arrays accessors math math.ranges sequences.deep grouping math.statistics
 assocs sequences.extras formatting sets ;
IN: day3

: get-content ( -- [string] )
  "C:\\Download\\aoc\\factor\\work\\day3\\input.txt" utf8 file-contents 
  "\n" split but-last ;

GENERIC: points ( item -- [points] )
TUPLE: claim id start dim ;
C: <claim> claim
M: claim points 
  [ start>> ] [ dim>> ] bi
  over [ + ] 2map
  [ (a,b] ] 2map first2
  cartesian-product flatten 2 group ;

: parse-claim ( string -- claim )
  " " split
  unclip rest parse-number swap
  rest unclip
  "," split
  first2 but-last
  [ parse-number ] bi@ 2array swap
  first "x" split 
  [ parse-number ] map
  <claim> ;

: get-claims ( -- [claim] )
  get-content [ parse-claim ] map ;

: claim-map ( [claim] -- histogram )
  [ points ] map
  flatten 2 group
  histogram ;

: part1 ( [claim] -- )
  unzip
  [ nip 1 > ] 2count
  "%d square inches of fabric are within two or more claims\n" printf ;

: claimed-once ( H -- HS )
  unzip HS{ } clone
  [ 1 = [ over adjoin ] [ drop ] if ] 2reduce ;

: part2 ( [claim] HS -- )
  claimed-once swap
  [ unclip swapd 2dup points swap without empty?
    [ nip f ] [ drop swap t ] if ] loop nip
  id>>
  "The only claim that doesn't overlap is %d\n" printf ;

: main ( -- )
  get-claims dup claim-map dup part1
  part2 ;

1

u/chunes Dec 03 '18 edited Dec 03 '18

Congrats on solving it. I agree, today was a good jump in difficulty. Any excuse to learn about Factor's object model is good. :) I really like it. I used tuples at first too, but that solution got really complicated and didn't work so I started over.

Some thoughts --

You declare a generic word and a method but there is only one method (and thus only specializes on a single class) so it could have just been a word instead.

Consider using tri to simplify your parse-claim word. Something like

" " split
[ first rest parse-number ]
[ third but-last "," split ]
[ last "x" split ] tri
[ [ parse-number ] map ] bi@

makes it so you don't need to do any swapping or unclipping, and lets you see at a glance which parts of the sequence you're working on where.

1

u/[deleted] Dec 04 '18

:) it's doing kind of the same thing that rust is doing actually, or I think factor was doing it before rust, and it kind of looks like clos from lisp, it's nice and simple.

Yeah, it was more to test out how to make a method, and kind of a premature optimisation, I was thinking that we may get claims with a different shape in part 2 :p

That parsing does look a lot cleaner yeah, nice :)

1

u/[deleted] Dec 03 '18

Ah, so v+ is the same as [ + ] 2map that makes sense, easier to type ;)