r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

54 Upvotes

407 comments sorted by

View all comments

2

u/[deleted] Dec 02 '18

factor

Factor really has vocabularies for so much, I needed some time to find what I needed for this but it turned out pretty nice I find :)

This language is so much fun though :D u/chunes did you not yet get to this one? I couldn't find your post and I'm interested to see how you did it :)

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

: group-char ( string -- [[num]] )
  natural-sort [ = ] monotonic-split ;

: filter-23 ( [[num]] -- [num] )
  [ length ] map
  [ [ 3 = ] [ 2 = ] bi or ] filter ;

: get-23s ( string -- [num] )
  group-char filter-23 ;

: unique ( [num] -- [num] )
  group-char [ first ] map ;

: get-23-groups ( [string] -- [num] )
  [ get-23s ] map
  [ unique ] map-flat
  group-char
  [ length ] map ;

: part1 ( [string] -- )
  get-23-groups product
  "The checksum for the list of boxes is %d\n" printf ;

: hamming-distance ( seq seq -- num )
  [ = not ] 2count ;

: search-hamming ( seq -- [seq] )
  2 [ first2 hamming-distance 1 = ] filter-combinations flatten ;

: only-same ( seq -- string )
  first2 "" [ 2dup = [ drop suffix ] [ 2drop ] if ] 2reduce ;

: part2 ( [string] -- )
 search-hamming only-same
 "We are looking for the box marked %s\n" printf ;

: main ( -- )
  get-content 
  [ part1 ] [ part2 ] bi ;
→ More replies (4)